Java Study Notes (14)--New Features of Java8 Class Library

foreword

The java8 version is the version with the biggest changes. The emergence of the Java virtual machine, a highly efficient compiler, allows programmers to focus more on writing clean and easy-to-maintain code, instead of thinking about how to convert each CPU clock cycle to each Bytes of memory make the best use of it. In the face of large data sets, java still lacks efficient parallel operation. Java8 provides some new features that can support code to run efficiently on multi-core CPUs. Before understanding Java's Stream operations, we must first understand two concepts: lazy evaluation and early evaluation.

Lazy evaluation: no new collection will be generated from the stream, the return value is still a Stream type

Evaluate early: a new set will be generated from the stream

How to judge whether it is lazy evaluation or early evaluation? When the return value is a Stream, it is lazy evaluation, and when the return value is another value or empty, it is early evaluation.

Stream stream operations

  1. First create a Student entity
public class Student implements Serializable {
    private int no;
    private String id;

    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                '}';
    }

    public Student(String id, int no) {
        this.id = id;
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

}

2. Create five objects and a list

        Student stuA = new Student("1",1);
        Student stuB = new Student("2",2);
        Student stuC = new Student("3",3);
        Student stuD = new Student("4",4);
        Student stuE = new Student("5",5);
        List<Student> list = new ArrayList<>();
        list.add (stuA);
        list.add(stuB);
        list.add(stuC);
        list.add(stuD);
        list.add (stuE);

3. Various types of stream operations

  • Stream operations that only use lazy evaluation will not generate a new stream and will not output any information
        list.stream()
                .filter(student -> {
                    System.out.println(student.getNo());
                    return student.getNo() == 1;
                });

Execute the result, inside the stream operation, I output a student.getNo(), but there is no output information on the console, filter is a lazy evaluation method, so there will be no output information.

  • When a stream has a terminating operation, a new value is generated, and information is also output.
            list.stream()
                .filter(student -> {
                    System.out.println(student.getNo());
                    return student.getNo() == 1;
                }).count();

The execution result is as follows:


  • collect generates a new list from the Stream, which belongs to early evaluation
List<String> collected = Stream.of("a","b","c")
                .collect(Collectors.toList());
        System.out.println(collected.toString());

The result of execution is [a, b, c]

  • map converts one type to another
List<String> collect = Stream.of("a", "b", "hello")
                .map(string -> string.toUpperCase())
                .collect(Collectors.toList());
        System.out.println(collect.toString());

The execution result is [A, B, HELLO]

  • flatMap connects multiple streams into a Stream stream, and then performs stream operations
List<Integer> collect1 = Stream.of(Arrays.asList(1, 2), Arrays.asList(3, 4))
                .flatMap(numbers -> numbers.stream())
                .collect(Collectors.toList());
        System.out.println(collect1.toString());

The execution result is [1, 2, 3, 4]

  • max and min find the maximum and minimum values, and return an Optional object, which is retrieved by the get method, and the Optional will be analyzed later
Integer integer = Stream.of(1, 2, 3, 4)
                .min(Comparator.comparing(r -> r.intValue()))
                .get();
        System.out.println(integer);

The execution result is 1, that is, the minimum value obtained is 1

  • reduce performs operations such as addition, subtraction, multiplication and division
Integer reduce = Stream.of(1, 2, 3)
                .reduce(0, (acc, element) -> acc - element);
        System.out.println(reduce);

The execution result is -6, which means that the calculation starts from 0, let the previous value - the next value, and get the final result

For example, optimizing code

Let me explain first, I haven't given the entities used in the code below, just understand which is simpler and more efficient when using the new features or using the new features.

public Set<String> findLongTracks(List<Album> albums){
        Set<String> trackNames = new HashSet<>();
        for (Album album : albums) {
            for (Track track:album.getTrackList()){
                if(track.getLength()>60){
                    String name = track.getName();
                    trackNames.add(name);
                }
            }
        }
    }

The above code has two layers of loops, optimized using the new features of java8:

public Set<String> findLongTracks(List<Album> albums){
        return albums.stream()
                .flatMap(album -> album.getTracks())
                .filter(track->track.getLength()>60)
                .map(track->track.getName())
                .collect(Collectors.toSet());
    }

Optional

The Optional<T> class is a container class that represents the existence or non-existence of a value. It is not uncommon for developers to deal with null pointer exceptions. Optional usage can avoid null pointer exceptions. Common methods are as follows:

  •  Optional.of(T t) creates an Optional object
  • Optional.empty(T t) creates an empty Optional object
  • Optional.ofNullable(T t) If t is not null, create an Optional instance, otherwise create an empty instance
  • isPresent() determines whether the object contains a value and returns true or false
  • orElse(T t) Returns the value if the object contains a value, otherwise returns t
  • orElseGet(Supplier other) If the object contains a value, return the value, otherwise return the value obtained by other
  • map(Function f) If there is a value, process the value and return the result Optional object, otherwise return Optional.empty()
  • flatMap(Function mapper) requires the return value to be Optional

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325935874&siteId=291194637