前言
java8的Stream 流式操作,用于对集合进行投影、转换、过滤、排序、去重等,更进一步地说,这些操作能链式串联在一起使用,类似于 SQL 语句,可以大大简化代码。下面我就将平时常用的一些stream操作做一些汇总。
通过 IntStream 构造基本类型的流
//在java8之前,遍历0-99的基本操作
for(int i = 0;i<100;i++){
System.out.println("i:"+i);
}
//使用IntStream串行遍历
IntStream.rangeClosed(0,99).forEach(i->{
System.out.println("i:"+i);
});
//使用IntStream并行遍历
IntStream.rangeClosed(0,99).parallel().forEach(i->{
System.out.println("i:"+i);
});
定义一个学生的结构体,为后续操作做准备
class Student{
private String name;
private int age;
private int score;
private String city;
public Student(String name, int age, int score, String city) {
this.name = name;
this.age = age;
this.score = score;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
score == student.score &&
Objects.equals(name, student.name) &&
Objects.equals(city, student.city);
}
@Override
public int hashCode() {
return Objects.hash(name, age, score, city);
}
@Override
public String toString() {
return "Student{" +
"name=‘" + name + ‘\‘‘ +
", age=" + age +
", score=" + score +
", city=‘" + city + ‘\‘‘ +
‘}‘;
}
}
//构造数据
List<Student> students = new ArrayList<>();
students.add(new Student("stu001",18,70,"合肥"));
students.add(new Student("stu001",18,70,"合肥"));
students.add(new Student("stu002",23,80,"郑州"));
students.add(new Student("stu003",45,90,"郑州"));
students.add(new Student("stu004",32,73,"合肥"));
students.add(new Student("stu005",66,76,"南京"));
students.add(new Student("stu006",34,79,"杭州"));
students.add(new Student("stu007",28,90,"合肥"));
students.add(new Student("stu008",19,100,"杭州"));
filter
根据特定的条件进行过滤
//filter 实现过滤操作,这边过滤出年龄大于30的学生
students.stream().filter(s->s.getAge()>30).forEach(System.out::println);
map
元素进行转化
//map 对流每一个元素进行转化,将student的age转化成流
students.stream().map(Student::getAge).forEach(System.out::println);
sort 排序
根据条件进行排序操作
//sort 按年龄排序,倒序
students.stream().sorted(Comparator.comparing(Student::getAge).reversed()).forEach(System.out::println);
distinct 去重
对流元素进行去重操作
//distinct 去重,因为实现了hashcode和equals,名称为stu001的学生重复了
students.stream().distinct().forEach(System.out::println);
将集合组成一个list
students.stream().map(Student::getAge).collect(Collectors.toList()).forEach(System.out::println);
将集合组成一个set
students.stream().map(Student::getAge).collect(Collectors.toSet()).forEach(System.out::println);
最大值
求出年龄最大的学生
System.out.println(students.stream().max(Comparator.comparingInt(Student::getAge)).get());
最大值
求出年龄最最小的学生
System.out.println(students.stream().min(Comparator.comparingInt(Student::getAge)).get());
join操作
按照逗号进行join,用[]包裹起来
//join操作,姓名join
System.out.println(students.stream().map(Student::getName).collect(Collectors.joining(",","[","]")));
toMap操作
根据指定key,value生成一个map
students.stream().collect(Collectors.toMap(Student::getName,student -> student,(x1,x2)->x2)).entrySet().forEach(System.out::println);