Java8 Stream usage

1. Introduction to Stream

As a highlight of Java 8, Stream is a completely different concept from InputStream and OutputStream in the java.io package. Stream in Java 8 is an enhancement to the function of Collection objects. It focuses on various very convenient and efficient aggregate operations or bulk data operations on collection objects. Stream API relies on the newly emerging Lambda expressions to greatly improve programming efficiency and program readability. At the same time, it provides serial and parallel modes for converging operations. The concurrent mode can make full use of the advantages of multi-core processors, using fork/join parallel methods to split tasks and accelerate processing. Usually writing parallel code is difficult and error-prone, but you can easily write high-performance concurrent programs without writing a line of multi-threaded code using Stream API. Therefore, the first appearance of java.util.stream in Java 8 is the product of a functional language + multi-core era.

2. What is Stream?

Stream is not a collection element. It is not a data structure and does not store data. It is about algorithms and calculations. It is more like an advanced version of Iterator. In the original version of Iterator, users can only explicitly traverse the elements one by one and perform certain operations on them; in the advanced version of Stream, users only need to specify what operations need to be performed on the contained elements, such as "filter out elements with a length greater than 10. String", "Get the first letter of each string", etc., Stream will implicitly traverse internally and make the corresponding data conversion.

3.Stream usage collection

Before java8, the implementation of loop traversal, sorting, conditional filtering, etc. of the collection was very redundant and complicated. After java8, the collection is converted to implement the previous similar operations, which is very concise and the code looks a lot fresher.

3.1 stream foreach
 String[] egs = {
    
    "hello", "world", "老张家的独苗"};
 Arrays.asList(egs).stream().forEach(s -> System.out.println(s));
3.2 stream filter

The returned stream only contains data that meets the predicate

  String[] egs = {
    
    "hello", "world", "老张家的独苗"};
  Arrays.asList(egs).stream().filter(s -> s.length() > 5).forEach(m -> System.out.println(m));
3.3 stream sort
  String[] egs = {
    
    "hello", "world", "老张家的独苗"};
  Arrays.asList(egs).stream().sorted((o1, o2) -> o1.compareTo(o2)).forEach(s-> System.out.println(s));
3.4 stream collect
  String[] egs = {
    
    "hello", "hello", "老张家的独苗"};
  Arrays.asList(egs).stream().collect(Collectors.toSet()).forEach(e -> System.out.println(e));
  Arrays.asList(egs).stream().collect(Collectors.toList()).forEach(e -> System.out.println(e));
3.5 stream map

The method maps the elements in the stream to other values. The new value type can be different from the original element type

  String[] egs = {
    
    "hello", "world", "老张家的独苗"};
  Arrays.asList(egs).stream().map(e -> e.length()).forEach(s -> System.out.println(s.getClass()));
3.6 stream mapToDouble
 String[] egs = {
    
    "1", "211", "3"};
 double test = Arrays.asList(egs).stream().mapToDouble(s -> s.length()).max().orElse(0);
 System.out.println(test);
3.7 stream grouppingby
 Map<String, List<Student>> nameMap = tableDtlEntities.stream().collect(Collectors.groupingBy(Student::getName));
3.8 stream SummaryStatistics
 LongSummaryStatistics moneySummary= studentList.stream().collect(Collectors.summarizingLong(Student::getMoney));
3.9 stream distinct

Ensure that the output stream contains a unique element, it is checked by Object.equals(Object) whether it contains the same element

Stream.of("a","b","c","b").distinct().collect(Collectors.toList()).forEach(s -> System.out.println(s));
3.10 stream reduce

The reduce operation can generate a value from the Stream. The value generated is not arbitrary, but based on a specified calculation model. For example, the count, min, and max methods mentioned earlier are included in the standard library because they are commonly used. In fact, these methods are reduce operations

  Optional<Integer> total = Stream.of(1,2,3,4,5).reduce( (x, y) -> x * y);
  System.out.println(total.get());
  Integer total2 = Stream.of(1,2,3,4,5).reduce(0, (x, y) -> x +y);
  System.out.println(total2);

Okay, that's all about the Stream of java8. Those who are interested can try to use Stream, it is very cool to use!

Guess you like

Origin blog.csdn.net/zhangxing52077/article/details/108514293