Java8 stream, lambda expression summary

Java8 stream, lambda expression summary

Since the next site is Java 8, I will mainly summarize the code examples that I often used for review.

reference http://www.ne.jp/asahi/hishidama/home/tech/java/stream.html

  1. Basic
    1. List.forEach

      List<Integer> list = Arrays.asList(1,2,3);
      list.forEach(System.out :: println);
      

This is the same as

    ```
    List<Integer> list = Arrays.asList(1,2,3);
    for(int i: list){
    	System.out.println(i);
    }
    ```

1. Map.forEach

    ```
    Map<Integer,String> map = new HashMap<>();
    map.put(1,"a");
    map.put(2,"b");
    map.forEach((k,v) -> System.out.println(v));
    ```

This is the same as

    ```
    Map<Integer,String> map = new HashMap<>();
    map.put(1,"a");
    map.put(2,"b");
    for(Entry<Integer, String> entry : map.entrySet()) {
      System.out.println(entry.getValue());
    }
    ```
  1. Frequent

  2. Create Map from Stream.collect (Collectors.toMap ()) List

    ```
    class Employee {
      private int id;
      private String name;
    
      public Employee(int id,String name){
        this.id = id;
        this.name = name;
      }
    
      public int getId() {
        return this.id;
      }
    
      public String getName() {
        return this.name;
      }
    }
    
    List<Employee> list = Arrays.asList(new Employee(1,"Sato"),new Employee(2,"large tree"),new Employee(3,"Tanaka"));
    Map<Integer,String> map = lists.stream().collect(Collectors.toMap(Employee::getId,Employee::getName));
    ```
    
  3. Process in Stream.map () List

    ```
    List<Integer> list = Arrays.asList(1,2,3);
    List<Integer> list2 = list.stream().map(i -> i * 2).collect(Collectors.toList());
    ```
    
  4. Stream.filter () Narrow down

    ```
    List<Integer> list = Arrays.asList(1,2,3);
    list.stream().filter(i -> i < 3).forEach(System.out :: println);
    ```
    
  5. Stream.sorted Sort

    ```
    List<Integer> list = Arrays.asList(1,3,2);
    list.stream().sorted(Comparator.comparing(i -> i)).forEach(System.out :: println);
    ```
    
  6. Use so well

  7. Stream.of () Stream creation

    ```
    Stream<Integer> stream = Stream.of(1,2,3);
    ```
    
  8. Stream.anyMatch () Check if there is matching data

    ```
    List<Integer> list = Arrays.asList(1,2,3);
    boolean m = list.stream().anyMatch(i -> i == 2);
    ```
    

Similar to the above are allMatch (all values match) and noneMatch (all values do not match).

  1. List.stream (). FlatMap (l-> l.stream ()) Combine Lists in List into one

    ```
    List<List<Integer>> list = Arrays.asList(Arrays.asList(1,2,3),Arrays.asList(4,5,6));
    List<Integer> list2 = list.stream().flatMap(l -> l.stream()).collect(Collectors.toList());
    ```
    

Recommended Posts

Java8 stream, lambda expression summary
[Java] Lambda expression
Java lambda expression
java neutral lambda expression 1
Java lambda expression variations
Java 8 lambda expression Feature
java lambda expression memo
Java lambda expression [memo]
Studying Java 8 (lambda expression)
Review java8 ~ Lambda expression ~
Java lambda expression again
java regular expression summary
Java8 Stream Rough Summary
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -
[Java] Functional interface / lambda expression
[Java11] Stream Summary -Advantages of Stream-
[Java11] Stream Usage Summary -Basics-
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Java8 Lambda Expression & Stream Design Pattern Rethinking --Null Object Pattern -
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
About Lambda, Stream, LocalDate of Java8
Java basic learning content 9 (lambda expression)
What is a lambda expression (Java)
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
Java knowledge summary
Java Generics Summary
[JAVA] Stream type
Try Java 8 Stream
Nowadays Java lambda expressions and Stream API
Java Stream API
Java 8 documentation summary
Hello Java Lambda
Studying Java 8 (Stream)
Java 11 document summary
Java Stream termination
[Java] Stream processing
Java 9 Optional :: stream
[Java] Summary of how to abbreviate lambda expressions
How to use Java API with lambda expression
Java8 to start now ~ forEach and lambda expression ~
Java 12 new feature summary
[Java] Stream Collectors notes
[Summary] Java environment preparation
effective java 3rd summary
Java 13 new feature summary
java learning (conditional expression)
[Java] Stream API-Stream generation
[Java] Stream API / map
Quarkus saves Java Lambda! ??
Understand Java 8 lambda expressions
Java static [Personal summary]
Java8 Stream API practice
Thread safe summary ~ Java ~
Java8 Stream reduction operation
I've reviewed Java's lambda expression, stream API, six months after I started Java.
Java Primitive Specialization Summary
Java development link summary
About Java lambda expressions
Personal summary about Java
Introduction to lambda expression
Explain Java 8 lambda expressions