Stream list Java
Java 8 - StreamsAdvertisements Previous Page Next Page Stream is a new abstract layer introduced in Java 8. Using stream, you can process data in a declarative way similar to SQL statements. For example, consider the following SQL statement. Show The above SQL expression automatically returns the maximum salaried employee's details, without doing any computation on the developer's end. Using collections framework in Java, a developer has to use loops and make repeated checks. Another concern is efficiency; as multi-core processors are available at ease, a Java developer has to write parallel code processing that can be pretty error-prone. To resolve such issues, Java 8 introduced the concept of stream that lets the developer to process data declaratively and leverage multicore architecture without the need to write any specific code for it. What is Stream?Stream represents a sequence of objects from a source, which supports aggregate operations. Following are the characteristics of a Stream
Generating StreamsWith Java 8, Collection interface has two methods to generate a Stream.
forEachStream has provided a new method forEach to iterate each element of the stream. The following code segment shows how to print 10 random numbers using forEach. Random random = new Random(); random.ints().limit(10).forEach(System.out::println);mapThe map method is used to map each element to its corresponding result. The following code segment prints unique squares of numbers using map. ListfilterThe filter method is used to eliminate elements based on a criteria. The following code segment prints a count of empty strings using filter. ListlimitThe limit method is used to reduce the size of the stream. The following code segment shows how to print 10 random numbers using limit. Random random = new Random(); random.ints().limit(10).forEach(System.out::println);sortedThe sorted method is used to sort the stream. The following code segment shows how to print 10 random numbers in a sorted order. Random random = new Random(); random.ints().limit(10).sorted().forEach(System.out::println);Parallel ProcessingparallelStream is the alternative of stream for parallel processing. Take a look at the following code segment that prints a count of empty strings using parallelStream. It is very easy to switch between sequential and parallel streams. CollectorsCollectors are used to combine the result of processing on the elements of a stream. Collectors can be used to return a list or a string. ListStatisticsWith Java 8, statistics collectors are introduced to calculate all statistics when stream processing is being done. List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics(); System.out.println("Highest number in List : " + stats.getMax()); System.out.println("Lowest number in List : " + stats.getMin()); System.out.println("Sum of all numbers : " + stats.getSum()); System.out.println("Average of all numbers : " + stats.getAverage());Stream ExampleCreate the following Java program using any editor of your choice in, say, C:\> JAVA. Java8Tester.javaLive Demo import java.util.ArrayList;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.Map;
public class Java8Tester {
public static void main(String args[]) {
System.out.println("Using Java 7: ");
// Count empty strings
ListVerify the ResultCompile the class using javac compiler as follows C:\JAVA>javac Java8Tester.javaNow run the Java8Tester as follows C:\JAVA>java Java8TesterIt should produce the following result Using Java 7: List: [abc, , bc, efg, abcd, , jkl] Empty Strings: 2 Strings of length 3: 3 Filtered List: [abc, bc, efg, abcd, jkl] Merged String: abc, bc, efg, abcd, jkl Squares List: [9, 4, 49, 25] List: [1, 2, 13, 4, 15, 6, 17, 8, 19] Highest number in List : 19 Lowest number in List : 1 Sum of all numbers : 85 Average of all numbers : 9 Random Numbers: -1279735475 903418352 -1133928044 -1571118911 628530462 18407523 -881538250 -718932165 270259229 421676854 Using Java 8: List: [abc, , bc, efg, abcd, , jkl] Empty Strings: 2 Strings of length 3: 3 Filtered List: [abc, bc, efg, abcd, jkl] Merged String: abc, bc, efg, abcd, jkl Squares List: [9, 4, 49, 25] List: [1, 2, 13, 4, 15, 6, 17, 8, 19] Highest number in List : 19 Lowest number in List : 1 Sum of all numbers : 85 Average of all numbers : 9.444444444444445 Random Numbers: -1009474951 -551240647 -2484714 181614550 933444268 1579250773 1627454872 1683033687 1798939493 Empty Strings: 2Useful Video CoursesVideo Java Date and Time Online Training16 Lectures 2 hours Malhar Lathkar More Detail Video Java Servlet Online Training19 Lectures 5 hours Malhar Lathkar More Detail Video JavaScript Online Training25 Lectures 2.5 hours Anadi Sharma More Detail Video Java Online TrainingMost Popular 126 Lectures 7 hours Tushar Kale More Detail Video Java Essential Training119 Lectures 17.5 hours Monica Mittal More Detail Video Java Essentials Online Training76 Lectures 7 hours Arnab Chakraborty More Detail Previous Page Print Page Next Page Advertisements |