• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

java Stream 使用

开发技术 开发技术 2周前 (04-09) 7次浏览

在Java1.8之前还没有stream流式算法的时候,我们要是在一个放有多个User对象的list集合中,将每个User对象的主键ID取出,
组合成一个新的集合,首先想到的肯定是遍历,如下:
List<Long> userIdList = new ArrayList<>();
for (User user: list) {
userIdList.add(user.id);
}
或者在1.8有了lambda表达式以后,我们会这样写:
List<Long> userIdList = new ArrayList<>();
list.forEach(user -> list.add(user.id));
在有了stream之后,我们还可以这样写:
List<Long> userIdList = list.stream().map(User::getId).collect(Collectors.toList());  

以集合为例,一个流式处理的操作我们首先需要调用stream()函数将其转换成流,然后再调用相应的中间操作达到我们需要对集合进行的操作,比如筛选、转换等,最后通过终端操作对前面的结果进行封装,返回我们需要的形式。

这里我们先创建一个Student实体类:
package streamdemo;

import java.util.Objects;

/**
* ——–学生类—–
*/

public class Student {

private Long id; //主键id
private String name; //姓名
private Integer age; //年龄
private String school; //学校

public Student(Long id, String name, Integer age, String school) {
this.id = id;
this.name = name;
this.age = age;
this.school = school;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getSchool() {
return school;
}

public void setSchool(String school) {
this.school = school;
}

@Override
public String toString() {
return “streamdemo.Student{” +
“id=” + id +
“, name='” + name + ”’ +
“, age=” + age +
“, school='” + school + ”’ +
‘}’;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(id, student.id) &&
Objects.equals(name, student.name) &&
Objects.equals(age, student.age) &&
Objects.equals(school, student.school);
}

@Override
public int hashCode() {
return Objects.hash(id, name, age, school);
}
}

package streamdemo;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {

public static void main(String[] args) {
List<Student> list = new ArrayList<Student>(){
{
add(new Student(1l,”张三”,10, “清华大学”));
add(new Student(2l,”李四”,12, “清华大学”));
add(new Student(3l,”王五”,15, “清华大学”));
add(new Student(4l,”赵六”,12, “清华大学”));
add(new Student(5l,”田七”,25, “北京大学”));
add(new Student(6l,”小明”,16, “北京大学”));
add(new Student(7l,”小红”,14, “北京大学”));
add(new Student(8l,”小华”,14, “浙江大学”));
add(new Student(9l,”小丽”,17, “浙江大学”));
add(new Student(10l,”小何”,10, “浙江大学”));
}
};

// 1 过滤  
//我们希望过滤所有学校是清华大学的Student:
System.out.println(“学校是清华大学的Student”);
List<Student> StudentList1 = list.stream().filter(Student -> “清华大学”.equals(Student.getSchool())).collect(Collectors.toList());
StudentList1.forEach(Student -> System.out.print(Student.getName() + ‘、’));

//2 去重
//,我们希望获取所有Student的年龄(年龄不重复)
System.out.println(“所有Student的年龄集合”);
List<Integer> StudentAgeList = list.stream().map(Student::getAge).distinct().collect(Collectors.toList());
System.out.println(“StudentAgeList = ” + StudentAgeList);
//3 求和
Stream<Integer> integerStream = list.stream().map(Student::getAge);
Integer reduce = integerStream.reduce(0, (a, c) -> (a + c));
System.out.println(“所有Student的年龄的和 = ” + reduce);

//4 排序
List<Student> StudentList4 = list.stream().sorted((s1,s2) -> s2.getAge() – s1.getAge()).collect(Collectors.toList());
StudentList4.forEach(Student -> System.out.print(Student.getName() + ‘、’));

// 5.跳过n个元素后再输出
// 如输出list集合跳过前两个元素后的list
System.out.println(“跳过前面两个Student的其他所有Student”);
List<Student> StudentList5 = list.stream().skip(2).collect(Collectors.toList());
StudentList5.forEach(Student -> System.out.print(Student.getName() + ‘、’));

 

}

}


程序员灯塔
转载请注明原文链接:java Stream 使用
喜欢 (0)