最为常用的是 map方法。
具体参考下面代码示例:
package com.work.jdk8;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class CollectionStreamTest {
public static void main(String[] args) {
List<Student> studentList = initStudentList();
// 实际工作开发,常用程度依次从高到低
System.out.println("---寝室中的学生,提取'学生id'属性得到的list,如下:");
mapStudenIdList(studentList).forEach(System.out::println);
System.out.println("---寝室中[年龄等于28 且 余额大于10元]学生列表信息,如下:");
filter(studentList).forEach(student -> System.out.println(student.toString()));
System.out.println("---寝室中大于等于30岁的人数:" + filterCount(studentList));
System.out.println("---寝室中的学生,['学生id'作为key、'学生姓名name'作为value],生成的Map如下:");
mapNewMapAndSortKey(studentList).forEach((key,value) -> System.out.println(key+":"+value));
System.out.println("---寝室中的学生,所有'学生id'用逗号连接后,生成的String:" + mapStudentIdDistinctJoinDelimiter(studentList));
System.out.println("---整个寝室全部余额[Double]精确为:" + mapMoneyDoubleSum(studentList));
System.out.println("---整个寝室全部余额[BigDecimal]精确为:" + mapMoneyBigDecimalReduceSetScale(studentList));
}
private static List<Long> mapStudenIdList(List<Student> list){
return list.stream()
.map(Student::getId)
.collect(Collectors.toList());
}
private static List<Student> filter(List<Student> list){
return list.stream()
.filter(student -> {
// 这样书写好处,通过变量好的命名,使得代码可读性更高
boolean ageIs28AndMoneyMore10 = student.getAge() == 28 && student.getMoney().compareTo(new BigDecimal(10))==1;
return ageIs28AndMoneyMore10;
}).collect(Collectors.toList());
}
private static Long filterCount(List<Student> list){
return list.stream()
.filter(student -> {
boolean olderThan30 = student.getAge() >= 30;
return olderThan30;
}).count();
}
private static Map<Long, String> mapNewMapAndSortKey(List<Student> list){
return list.stream()
.collect(
Collectors.toMap(Student::getId, Student::getName, (key1, key2) -> key2));
}
private static String mapStudentIdDistinctJoinDelimiter(List<Student> list){
return list.stream()
.map(student -> student.getId().toString())
.distinct()
.collect(Collectors.joining(","));
}
private static Double mapMoneyDoubleSum(List<Student> list){
return list.stream()
.mapToDouble(student -> student.getMoney().doubleValue())
.sum();
}
private static BigDecimal mapMoneyBigDecimalReduceSetScale(List<Student> list){
return list.stream()
.map(Student::getMoney)
// reduce方法,第一个参数是初始值;第二个参数是指定'相加'操作
.reduce(BigDecimal.ZERO, BigDecimal::add)
// 最终BigDecimal保留2位小数,保留具体策略(四舍五入等)
.setScale(2, 0);
}
/**
* 私有方法:初始化list列表
* @return
*/
private static List<Student> initStudentList(){
List<Student> list = new ArrayList<>();
Student stu1 = new Student(1L, "邢华阳", 29, "稳重的大象", new BigDecimal(200.66));
Student stu2 = new Student(2L, "陈龙基", 28, "灵动的仙鹤", new BigDecimal(300.25));
Student stu3 = new Student(3L, "李阳", 31, "神秘的小熊", new BigDecimal(166.32));
Student stu4 = new Student(4L, "靳浩东", 28, "The One", new BigDecimal(24.10));
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
return list;
}
}
Student.java---学生实体类
package com.work.jdk8;
import java.math.BigDecimal;
import java.util.Objects;
public class Student {
private Long id;
private String name;
private Integer age;
private String nickName;
private BigDecimal money;
public Student(Long id, String name, Integer age, String nickName, BigDecimal money) {
this.id = id;
this.name = name;
this.age = age;
this.nickName = nickName;
this.money = money;
}
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 getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
@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(nickName, student.nickName) &&
Objects.equals(money, student.money);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age, nickName, money);
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", nickName='" + nickName + '\'' +
", money=" + money +
'}';
}
}
参考文章:
1. jdk1.8新特性Stream集合详细操作
网友评论