Java knowledge point Stream

Posted by mushroom on Wed, 13 May 2020 16:35:23 +0200


There are two very famous improvements in java8, one is Lambda expression and the other is Stream.

Stream is a stream. Its main function is to search and filter Collection data. It is similar to SQL database operation. The difference between stream and Collection is that Collection is only responsible for storing data, not for other data processing, mainly dealing with memory. But stream is mainly responsible for calculating data, mainly dealing with CPU.

Stream syntax explanation

The Stream execution process is very simple. There are three main processes. First, create a Stream, then use the Stream to operate data, and finally terminate the Stream. It's a bit like the life cycle of a Stream.

public class Student {

    private Integer id;
    private String name;
    private Integer age;
    private Double score;

    Student(Integer id, String name, Integer age, Double score){ = id; = name;
        this.age = age;
        this.score = score;


1. Create a Stream

Create Stream from collection

public class StreamTest {

    public static List<Student> getStudents() {
        List<Student> students = new ArrayList<>();
        students.add(new Student(1, "petty thief", 10, 90.0));
        students.add(new Student(2, "Xiao Zhang", 12, 95.0));
        students.add(new Student(3, "Xiao Wang", 15, 85.0));

        return students;

    public static void main(String[] args) {

        //Returns a sequence flow
        Stream<Student> stream1 = getStudents().stream();
        //Return a parallel stream
        Stream<Student> stream2 = getStudents().parallelStream();


Create a Stream from an array

int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7};
IntStream intStream =;

Through Stream.of

Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);
Stream<String> stringStream = Stream.of("1", "2", "3", "4", "5", "6");
Stream<Student> studentStream = Stream.of(new Student(1, "petty thief", 10, 90.0), 
          new Student(2, "Xiao Zhang", 12, 95.0), 
          new Student(3, "Xiao Wang", 15, 85.0));

Create an infinite flow

//Every 5 numbers, starting from 0, infinite loop
Stream.iterate(0, t -> t+5).forEach(System.out::println);
//Take one every 5 numbers, starting from 0, only take 5 numbers
Stream.iterate(0, t -> t+5).limit(5).forEach(System.out::println);
//Take a random number

2. Use Stream to operate data

Filtering and slicing

//Filtering: filtering students older than 12
getStudents().stream().filter(item -> item.getAge() > 12).forEach(System.out::println);
//Cut off flow: screen out the top 2 students
//Skip element: skip the first 2 elements
//Filter repeating elements


//map operation
List<String> list = Arrays.asList("java", "python", "c++");
Stream<String> stream =;
//In this case, each lowercase letter has an uppercase letter mapping -> str.toUpperCase()).forEach(System.out::println);

//Filter out all the students of all ages, and then filter out the students of over 12 ages
Stream<Student> stream = getStudents().stream();
Stream<Integer> ageStream =;
ageStream.filter(age -> age > 12).forEach(System.out::println);


//Natural order
List<Integer> list = Arrays.asList(1, 3,24, 5, 6, 8, 23, 45, 72, 16);
Stream<Integer> stream =;

//Object sorting: object sorting can implement the comparable interface first or directly specify
List<Student> studentList = getStudents();
//Implement the comparable interface;
//Direct designation, e2) ->, e2.getAge())).forEach(System.out::println);

3. Terminate Stream

Match and find

List<Student> list = getStudents();

//Judge whether all students are older than 12
boolean allMatch = -> item.getAge() > 12);
//Determine if there are students older than 12
boolean anyMatch = -> item.getAge() > 20);
//Judge whether there is a student named "Xiao Li"
boolean noneMatch = -> item.getName().equals("petty thief"));
//Find the first student
Optional<Student> first =;
//Find student data
long count =;
long count1 = -> item.getScore() > 90).count();
//Find elements in the current flow
Optional<Student> any =;
//Find the highest score of students: if students implement the comparable interface, they can directly compare
Stream<Double> doubleStream = -> item.getScore());


List<Integer> list = Arrays.asList(1, 3,24, 5, 6, 8, 23, 45, 72, 16);

//Calculate the sum of the totals, Integer::sum);
//Calculate the total score of students
List<Student> students = getStudents();
Stream<Double> stream =;


//Return a list
List<Student> list = getStudents().stream().filter(e -> e.getAge() > 12).collect(Collectors.toList());
//Return a set
Set<Student> set  = getStudents().stream().filter(e -> e.getAge() > 12).collect(Collectors.toSet());

Note: it is not recommended to use when the data volume is small. It can be used when the data volume is large.

Topics: Programming Lambda SQL Database Java