This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

preface

With the continuous update iteration of Java version, Java development can also become sweet, the latest version of Java 11, but the later version is no longer provided with commercial support, need to charge, but JavA8 is still a continuous free update use, the later version is also updated quickly dazzling, So stable use or java8 can experience the new features, and do not need to worry about the bugs brought by the upgrade

New features

Newer features are streams, and lambda expressions

The figure above is some common methods, delay method, which is the intermediate method of data processing, finalization method is the data ending method which accords with good processing

The lazy method works the same way as the lazy loading of lambda expressions. It can be programmed in a chain like list.().filter().map().limit(), and you can click all the way down to the last step to load the data with the count or foreach method

Stream collection traverses forEach

Iterating through the list is done directly by list.stream().foreach

        List<String> a=new ArrayList<>();
        a.add("judy");
        a.add("Tom");
        / / lambda expressions
        a.stream().forEach(s -> System.out.println(s));
        
        a.stream().forEach(s -> {
            System.out.println(s);
            System.out.println(2);
        });

Copy the code

Set complex object traversal

        Student student = new Student();
        student.setAddress("Lujiabang Road, Huangpu District, Shanghai");
        student.setName("judy");
        student.setAge(26);
        List<Student> students = new ArrayList<>();
        students.add(student);

        / / lambda expressions
        students.stream().forEach(student1 -> System.out.println(student1.name));
        
        students.stream().forEach(student1 -> {
            System.out.println(student1.name);
            System.out.println(student1.age);
            System.out.println(student1.address);
        });
Copy the code

Stream Indicates the filter condition

Use list.stream().filter() to filter. If true, the following statement is executed

        List<String> a = new ArrayList<>();
        a.add("judy");
        a.add("Tom");
        a.add("");
        // The lambda expression is printed only when Judy
        a.stream().filter(s -> s.equals("judy")).forEach(s -> System.out.println(s));
        // Print only if it is not emptya.stream().filter(s -> ! s.isEmpty()).forEach(s -> System.out.println(s));Copy the code

Complex objects are filtered by multiple criteria

        Student student = new Student();
        student.setAddress("Lujiabang Road, Huangpu District, Shanghai");
        student.setName("judy");
        student.setAge(26);
        List<Student> students = new ArrayList<>();
        students.add(student);

        // Lambda expressions are printed when students are older than 28
        students.stream().filter(student1 -> student1.getAge()>28).forEach(student1 -> System.out.println(student1.name));

        // Multiple criteria are filtered, and the student name starts with a sheet, and the length is equal to 3, and the lambda expression is printed
        students.stream().filter(s->s.getName().startsWith("Zhang")).filter(s-> s.getName().length() == 3).forEach(System.out::println);

        students.stream().forEach(student1 -> {
            System.out.println(student1.name);
            System.out.println(student1.age);
            System.out.println(student1.address);
        });
Copy the code

The stream element maps to a map

Mapping an element from A stream to another stream is often used later, when A method receives A return value but receives B

  1. Converts a stream of type String to an Integer
  Stream<String> stringStream = Stream.of("1"."2"."3"."4"."5"."6");
  stringStream.map(str->Integer.parseInt(str)).forEach(System.out::println);
Copy the code
  1. Collection conversion

To do permission processing requires that permissions cannot be repeated, so the list set is converted to the set set

      List<String> a = new ArrayList<>();
        a.add("judy");
        a.add("Tom");
        a.add("");
        a.stream().map(s -> s).collect(Collectors.toSet()).forEach(s -> System.out.printf(s));
Copy the code

The method needs to return List, but there’s only List, so think stream().map

    public  List<String> queryNamesByIds(List<Long> ids){
        List<Category> categories = this.categoryMapper.selectByIdList(ids);
       return  categories.stream().map(category -> category.getName()).collect(Collectors.toList());
    }

Copy the code

Listis converted to List

, where the Object in List
is the JSON Object of Cart

        // Query the shopping Cart data (List becomes List
       
        )
       
        return carts.stream().map(o -> JsonUtils.parse(o.toString(),Cart.class)).collect(Collectors.toList());

Copy the code

Stream Two streams are merged into a single stream contract

       Stream<String> streamA = Stream.of("Zhang Wuji"."Zhang Cuishan");
        Stream<String> streamB = Stream.of("Beautiful Goat"."Pleasant Goat");
        / / write 1
        //Stream.concat(streamA, streamB).forEach(System.out::println);
        
        / / write 2
       Stream<String> result=  Stream.concat(streamA, streamB);
       result.forEach(name-> System.out.println(name));

Copy the code

Refer to the article