πŸ‘ πŸ‘ πŸ‘ hello! Hi everyone, I am a blogger who loves to share all kinds of technology. 😍😍😍 ⭐ [Learn endless small wonder] creation purpose: every command has been personally executed, every line of code has been actually run, every method has been really practiced, every article has been conscience made. ✊✊✊ ⭐ [learn endless small wonder] blog all related to commands, codes, in addition to providing pictures for your reference, in addition to the picture will provide a plain text format command or code at the bottom of the picture for you to paste and copy directly execute commands or run codes. 🀝🀝🀝 ⭐ If you have a strong interest in technology, welcome to pay attention to “learn endless small wonder”, welcome to communicate with me. 😘😘😘 ❀️❀️ thank you for reading ❀️❀️❀️

@[TOC]

First, preliminary preparation

1. Create an object

1.1, the Student

public class Student { private int id; private String name; private String sex; private int age; public Student(int id, String name, String sex, int age) { this.id = id; this.name = name; this.sex = sex; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; }}Copy the code

2. Initialize data

2.1. Initialize the collection

Public class StreamTest {List<Student> studentList = array. asList(new Student(1," δΈ‰"," δΈ‰", 20), New Student (2, "bill", "male", 25), the new Student (3, "detective" and "female", 18), a new Student (4, "Daisy", "female", 26)); }Copy the code

Common operation mode of Stream

1, screening

1.1, the filter

Filter filters certain elements from the collection, such as data sets older than 20 in the query collection

// Query data sets older than 20. List<Student> List = studentlist.stream ().filter(s -> s.getage ()>20).collect(Collectors. ToList ());Copy the code

1.2, limit

Limit, like limit in mysql, returns a specified amount of data

List<Student> List = studentlist.stream ().filter(s -> s.age ()>20).limit(1) .collect(Collectors.toList());Copy the code

1.3, skip

Skip returns a set that skips the first n elements

Select * from age > 20; List<Student> List = studentlist.stream ().filter(s -> s.getage ()>20).skip(1).collect(Collectors. ToList ());Copy the code

1.4, distinct

Distinct, a filter that removes duplicate elements through the elements’ hashCode() and equals()

Select * from age > 20; List<Student> List = studentlist.stream ().filter(s -> s.gateage ()>20).distinct().collect(Collectors. ToList ());Copy the code

2, mapping,

2.1, the map

Map, which converts collection elements to other forms, takes as an argument a function that acts on each element and maps it to a new element

Select * from age > 20; List<String> List = studentlist.stream ().filter(s -> s.age ()>20).map(Student::getName) .collect(Collectors.toList());Copy the code

3, sorting,

3.1, sorted ()

Sorted () is sorted naturally

Select * from age > 20; select * from age > 20; List<String> List = studentlist.stream ().filter(s -> s.getage ()>20).map(Student::getName).sorted() .collect(Collectors.toList());Copy the code

Sorted (Comparator com)

Sorted (Comparator com) customizes sorting, customizes input sorting rules

Select * from age > 20; Sorted by name List<Student> List = studentlist.stream ().filter(s -> s.gage ()>20).sorted((e1,e2) -> {return e1.getName().compareTo(e2.getName()); }) .collect(Collectors.toList());Copy the code

4. Find and match

4.1, allMatch

AllMatch checks whether all elements match

Boolean flag = studentlist.stream ().allmatch ((e) -> equetName ().equals());Copy the code

4.2, anyMatch

AnyMatch Specifies whether to match at least one element

Boolean flag = studentlist.stream ().anymatch ((e) -> equetName ().equals());Copy the code

4.3, noneMatch

NoneMatch checks to see if all elements are not matched

Boolean flag = studentlist.stream ().nonematch ((e) -> equetName ().equals());Copy the code

4.4, findFirst

FindFirst returns the first element

// Return the first element in the collection Optional<Student> Student = studentlist.stream ().findfirst ();Copy the code

4.5, findAny

FindAny returns any element in the current collection

// Return any element in the collection Optional<Student> Student = studentlist.stream ().findany ();Copy the code

4.6, conut

Conut returns the total number of elements in the stream

Long num = studentlist.stream ().count();Copy the code

4.7, Max

Returns the maximum value in the stream

Optional<Student> Student = studentlist.stream ().max((e1,e2)) -> Integer.compare(e1.getAge(),e2.getAge()));Copy the code

4.8, min

Returns the minimum value in the stream

Student = studentlist.stream ().min((e1,e2)) -> Integer.compare(e1.getAge(),e2.getAge()));Copy the code