Found 4336 Articles for Java 8

DoubleStream mapToInt() method in Java

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

104 Views

The mapToInt() function of the DoubleStream class returns an IntStream consisting of the results of applying the given function to the elements of this stream.The syntax is as followsIntStream mapToInt(DoubleToIntFunction mapper)Here, mapper is a stateless function to apply to each element. The DoubleToIntFunction is a function that accepts a double-valued argument and produces an int-valued result.To use the DoubleStream class in Java, import the following packageimport java.util.stream.DoubleStream;The following is an example to implement DoubleStream mapToInt() method in JavaExample Live Demoimport java.util.stream.IntStream; import java.util.stream.DoubleStream; public class Demo {    public static void main(String[] args) {       DoubleStream doubleStream = DoubleStream.of(45.8, ... Read More

IntStream iterator() method in Java

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

179 Views

The iterator() method of the IntStream class in Java is used to return an iterator for the elements of this stream.The syntax is as followsPrimitiveIterator.OfInt iterator()Here, PrimitiveIterator.OfInt is an Iterator specialized for int values. To work with the IntStream class in Java, import the following packageimport java.util.stream.IntStream;Create an IntStream and add some elementsIntStream intStream = IntStream.of(15, 40, 55, 70, 95, 120);To return an iterator for the stream elementsPrimitiveIterator.OfInt primIterator = intStream.iterator();The following is an example to implement IntStream iterator() method in JavaExample Live Demoimport java.util.*; import java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       ... Read More

The listIterator() method of CopyOnWriteArrayList in Java

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

85 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

774 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

81 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

Advertisements