Found 4335 Articles for Java 8

LongStream map() method in Java

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

83 Views

The map() method of the LongStream class in Java returns a stream consisting of the results of applying the given function to the elements of this stream.The syntax is as follows:LongStream map(LongUnaryOperator mapper)Here, the parameter mapper is a stateless function to apply to each element. The LongUnaryOperator represents an operation on a single long-valued operand that produces a long-valued result.To use the LongStream class in Java, import the following package:import java.util.stream.LongStream;Create a LongStream and add some elements:LongStream longStream1 = LongStream.of(15L, 30L, 45L, 67L, 80L);Now, create another LongStream and map it to a condition set for the elements of longStream1:LongStream longStream2 ... Read More

Generate Infinite Stream of Integers in Java using IntStream.generate()

Nancy Den
Updated on 30-Jul-2019 22:30:25

119 Views

You can also generate Infinite Stream of Integers in Java with IntStream.generate() method. Here, we have used the Random class to get the list of random integers:Random r = new Random();After that use IntStream.generate() and the nextInt() method gets the next random integer:IntStream.generate(r::nextInt)The following is an example displaying how to generate Infinite Stream of Integers with IntStream.generate() in Java:import java.util.stream.*; import java.util.*; public class Main {    public static void main(String[] args) {       Random r = new Random();       IntStream.generate(r::nextInt).forEach(System.out::println);    } }Here is the output:565757777 3535363636 9879879879 -686549988Read More

IntStream builder() method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

83 Views

The builder() method in IntStream class is used to return a builder for an IntStream.The syntax is as follows:static IntStream.Builder builder()Here, we have created an IntStream and used the builder() method. An element is added using add() method:IntStream intStream = IntStream.builder().add(25).build();The following is an example to implement IntStream builder() method in Java −Example Live Demoimport java.util.*; import java.util.stream.Stream; import java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       IntStream intStream = IntStream.builder().add(25).build();       intStream.forEach(System.out::println);    } }output25

Create KeyValue Tuple using with() method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

69 Views

To create a KeyValue tuple in Java, you can use the with() 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 package:import 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 JavaTuples:Steps: How to run JavaTuples program in EclipseThe following is an example to create KeyValue tuple using ... Read More

LongStream generate() method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

86 Views

The generate() method of the LongStream class returns an infinite sequential unordered stream where each element is generated by the provided LongSupplier.The syntax is as follows:static LongStream generate(LongSupplier s)Here, s is the LongSupplier for generate elements. The LongSupplier is the supplier of long-valued results.To use the LongStream class in Java, import the following package:import java.util.stream.LongStream;The following is an example to implement LongStream generate() method in Java −Example Live Demoimport java.util.stream.LongStream; public class Demo {    public static void main(String[] args){       LongStream longStream = LongStream.generate(()          -> { return (long)(Math.random() * 100); });       ... Read More

Create Decade Tuple from an array in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

68 Views

To create Decade Tuple from array in Java, use the fromArray() method. Here, we will see how to create a Decade Tuple using the fromArray() method. Let us first see what we need to work with JavaTuples. To work with Decade class in JavaTuples, you need to import the following package:import org.javatuples.Decade;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 JavaTuples:Steps: How to ... Read More

LongStream mapToDouble() method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

101 Views

The mapToDouble() method returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.The syntax is as follows:DoubleStream mapToDouble(LongToDoubleFunction mapper)The parameter mapper is a stateless function to apply to each element.To use the LongStream class in Java, import the following package:import java.util.stream.LongStream;The following is an example to implement LongStream mapToDouble() method in JavaExampleimport java.util.stream.LongStream; import java.util.stream.DoubleStream; public class Demo {    public static void main(String[] args) {       LongStream longStream = LongStream.of(5000L, 12000L, 15000L, 20000L, 25000L);       DoubleStream s = longStream.mapToDouble(a → (double)a);       System.out.println("Elements of DoubleStream..."); ... Read More

LongStream mapToObj() method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

117 Views

The LongStream mapToObj() method is used in Java to return an object-valued Stream consisting of the results of applying the given function to the elements of this stream.The syntax is as follows: StreammapToObj(LongFunction

LongStream parallel() method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

92 Views

The parallel() method of the LongStream class in Java returns an equivalent stream that is parallel.The syntax is as follows:LongStream parallel()To use the LongStream class in Java, import the following packageExample Live Demoimport java.util.stream.LongStream;The following is an example to implement LongStream parallel() method in Java:import java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       LongStream longStream = LongStream.range(10L, 20L);       System.out.println("Parallel LongStream = ");       longStream.parallel().forEach(System.out::println);    } }outputParallel LongStream = 16 15 10 17 11 13 12 19 18 14

IntStream parallel() method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

410 Views

The parallel() method of the IntStream class in Java returns an equivalent parallel stream. The method may return itself, either because the stream was already parallel, or because the underlying stream state was modified to be parallel.The syntax is as follows:IntStream parallel()Create an IntStream and you can use the range() method as well to set the range of elements:IntStream intStream = IntStream.range(20, 35);Now, use the parallel() method:intStream.parallel()The following is an example to implement IntStream parallel() method in JavaExample Live Demoimport java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       IntStream intStream = IntStream.range(20, 35);   ... Read More

Advertisements