Found 33676 Articles for Programming

DoubleStream summaryStatistics() method in Java

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

143 Views

The summaryStatistics() method of the DoubleStream class returns a DoubleSummaryStatistics describing various summary data about the elements of this stream. This is a special case of a reduction.The syntax is as followsDoubleSummaryStatistics summaryStatistics()Here, DoubleSummaryStatistics is a state object for collecting statistics such as count, min, max, average, etc. It works with streams. To use the DoubleStream class in Java, import the following packageimport java.util.stream.DoubleStream;First, create a DoubleStream and add some elementsDoubleStream doubleStream = DoubleStream.of(20.5, 35.8, 45.9, 50.8, 80.7);Now, get the statisticsDoubleSummaryStatistics details = doubleStream.summaryStatistics();The following is an example to implement DoubleStream summaryStatistics() methodExample Live Demoimport java.util.stream.DoubleStream; import java.util.DoubleSummaryStatistics; public class Demo ... Read More

LongStream map() method in Java

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

128 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

170 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

151 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

106 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

144 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

120 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

163 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

191 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

148 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

Advertisements