Found 4332 Articles for Java 8

The listIterator() method of CopyOnWriteArrayList in Java

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

86 Views

The listIterator() method of the CopyOnWriteArrayList class in Java is used to return a list iterator over the elements in this list.The syntax is as followspublic ListIterator listIterator()To work with CopyOnWriteArrayList class, you need to import the following packageimport java.util.concurrent.CopyOnWriteArrayList;The following is an example to implement CopyOnWriteArrayList class listIterator() method in JavaExample Live Demoimport java.util.Iterator; import java.util.concurrent.CopyOnWriteArrayList; public class Demo {    public static void main(String[] args) {       CopyOnWriteArrayList arrList = new CopyOnWriteArrayList();       arrList.add(30);       arrList.add(40);       arrList.add(60);       arrList.add(70);       arrList.add(90);       arrList.add(100);   ... Read More

IntStream max() method in Java

George John
Updated on 30-Jul-2019 22:30:25

778 Views

The IntStream max() method in the Java IntStream class is used to get the maximum element from the stream. It returns an OptionalInt describing the maximum element of this stream, or an empty optional if this stream is empty.The syntax is as followsOptionalInt max()Here, OptionalInt is a container object which may or may not contain an int value.Create an IntStream and add some elements in the streamIntStream intStream = IntStream.of(89, 45, 67, 12, 78, 99, 100);Now, get the maximum element from the streamOptionalInt res = intStream.max();The following is an example to implement IntStream max() method in Java. The isPresent() method ... Read More

LongStream.Builder accept() method in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:25

71 Views

The accept() method of the LongStream.Builder class adds an element to the stream being built.The syntax is as followsvoid accept(long i)Here, i is the input.To use the LongStream.Builder class in Java, import the following packageimport java.util.stream.LongStream;Create a LongStreamLongStream.Builder builder = LongStream.builder();Add some elementsbuilder.accept(200L); builder.accept(600L); builder.accept(400L);The following is an example to implement LongStream.Builder accept() method in JavaExample Live Demoimport java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       LongStream.Builder builder = LongStream.builder();       builder.accept(200L);       builder.accept(600L);       builder.accept(400L);       builder.accept(350L);       builder.accept(900L);       builder.accept(850L); ... Read More

LongStream.Builder add() method in Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

96 Views

The add() method of the LongStream.Builder class in Java adds an element to the stream being built. The method returns this builder.The syntax is as followsdefault LongStream.Builder add(long i)Here, i is the input.To use the LongStream.Builder class in Java, import the following packageimport java.util.stream.LongStream;Create LongStream.Builder and add some elementsLongStream.Builder builder = LongStream.builder();Add some elements in the streambuilder.add(150L); builder.add(200L); builder.add(500L); builder.add(250L);The following is an example to implement LongStream.Builder add() method in JavaExample Live Demoimport java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       LongStream.Builder builder = LongStream.builder();       builder.add(150L);       builder.add(200L);   ... Read More

Create KeyValue Tuple from an array in Java

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

80 Views

To create KeyValue tuple from an array in Java, you need to use the fromArray() method. Let us first see what we need to work with JavaTuples. To work with KeyValue class in JavaTuples, you need to import the following packageimport org.javatuples.KeyValue;Note Download JavaTuples Jar library to run JavaTuples program. If you are using Eclipse IDE, then Right Click Project -> Properties -> Java Build Path -> Add External Jars and upload the downloaded JavaTuples jar file. Refer the below guide for all the steps to run JavaTuplesSteps − How to run JavaTuples program in EclipseThe following is an example ... Read More

DoubleStream iterator() method in Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

83 Views

The iterator() method of the DoubleStream class in Java returns an iterator for the elements of this stream.The syntax is as followsPrimitiveIterator.OfDouble iterator()Here, PrimitiveIterator.OfDouble is an Iterator specialized for double values.To use the DoubleStream class in Java, import the following packageimport java.util.stream.DoubleStream;The following is an example to implement DoubleStream iterator() method in JavaExample Live Demoimport java.util.*; import java.util.stream.DoubleStream; public class Demo {    public static void main(String[] args) {       DoubleStream doubleStream = DoubleStream.of(50.6, 69.8, 81.8, 95.6, 106.9);       PrimitiveIterator.OfDouble res = doubleStream.iterator();       while (res.hasNext()) {          System.out.println(res.nextDouble());       }    } }Output50.6 69.8 81.8 95.6 106.9

LongStream sum() method in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:25

47 Views

The sum() method of the LongStream class returns the sum of elements in this stream.The syntax is as followslong sum()To use the LongStream class in Java, import the following packageimport java.util.stream.LongStream;First, create an IntStream and add some elementsLongStream longStream = LongStream.of(100L, 30000L, 45000L, 55000L, 70000L);Now, add the elements of the streamlong res = longStream.sum();The following is an example to implement LongStream sum() method in JavaExample Live Demoimport java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       LongStream longStream = LongStream.of(100L, 30000L, 45000L, 55000L, 70000L);       long res = longStream.sum();       System.out.println("The ... Read More

IntStream distinct() method in Java

George John
Updated on 30-Jul-2019 22:30:25

115 Views

The distinct() method in the IntStream class in Java returns a stream consisting of the distinct elements of this stream.The syntax is as followsIntStream distinct()Let’s say we have the following elements in the stream. Some of them are repeatedIntStream intStream = IntStream.of(10, 20, 30, 20, 10, 50, 80, 90, 100, 80);To get the distinct elements, use the IntStream distinct() method.The following is an example to implement IntStream distinct() method in JavaExample Live Demoimport java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       IntStream intStream = IntStream.of(10, 20, 30, 20, 10, 50, 80, 90, 100, 80); ... Read More

IntStream empty() method in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:25

114 Views

The empty() method of the IntStream class returns an empty sequential IntStream.The syntax is as followsstatic IntStream empty()This is how you can create an empty IntStreamIntStream intStream = IntStream.empty();Now, using the count() method you can check the count of elements in the stream, which is 0 since we created an empty streamIntStream intStream = IntStream.empty();The following is an example to implement IntStream empty() method in JavaExample Live Demoimport java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       IntStream intStream = IntStream.empty();       System.out.println("The number of elements in the stream = "+intStream.count());    } ... Read More

DoubleStream sequential() method in Java

George John
Updated on 30-Jul-2019 22:30:25

48 Views

The sequential() method of the DoubleStream class returns an equivalent stream that is sequential.The syntax is as followsDoubleStream sequential()To use the DoubleStream class in Java, import the following packageimport java.util.stream.DoubleStream;Create a DoubleStream and add some elementsDoubleStream doubleStream1 = DoubleStream.of(45.8, 67.9, 78.5, 90.6, 97.4);Now create an equivalent stream that is sequentialDoubleStream doubleStream2 = doubleStream1.sequential();The following is an example to implement DoubleStream sequential() method in JavaExample Live Demoimport java.util.*; import java.util.stream.DoubleStream; public class Demo {    public static void main(String[] args) {       DoubleStream doubleStream1 = DoubleStream.of(45.8, 67.9, 78.5, 90.6, 97.4);       DoubleStream doubleStream2 = doubleStream1.sequential();       ... Read More

Advertisements