【Java】【6】JDK8 Stream操作整理

摘要:

1,List<EntityOld>转换为List<EntityNew>

2,从一个实体类List中取出某个字段组成新的list

3,从list中筛选出符合条件的数据组成新的list

4,求list集合中某个字段的和

5,根据某字段对list进行分组

6,根据某字段对list进行分组并计数

7,根据字段排序

8,多条数据取第一条

正文:

1,List<EntityOld>转换为List<EntityNew>

List<EntityOld> list = oldList;
List<EntityNew> newList = list.stream().map(EntityNew::new).collect(Collectors.toList());

2,从一个实体类List中取出某个字段组成新的list

List<EntityOld> list = oldList;
List<String> idList = list.stream().map(EntityOld::getOldId).collect(Collectors.toList());

3,从list中筛选出符合条件的数据组成新的newList

普通for循环:

List<Student> list = new ArrayList<>(); //Student1: name:张三;age:18 //Student2: name:张三;age:20
List<StudentVo> newList = new ArrayList<>();
list.stream().forEach(item -> {
if (item.getAge() > 18) {
StudentVo info = new StudentVo();
info.setName(item.getName());
newList.add(info);
}
});

加filter:

//超过18岁
List<Student> newList = list.stream().filter(i -> this.checkAgeOver18(i.getAge())).collect(Collectors.toList()); private Boolean checkAgeOver18 (int age) {
return age > 18;
} //简化版,但是条件复杂的话最好就封装成一个方法了
List<Student> newList = list.stream().filter(i -> i.getAge() > 18).collect(Collectors.toList());

4,求list集合中某个字段的和

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("平均数 : " + stats.getAverage());
List<Student> list = new ArrayList<>(); //Student1: name:张三;age:18 //Student2: name:张三;age:20
Integer result = list.stream().collect(Collectors.summingInt(Student::getAge));
System.out.println("所有学生年龄之和 : " + reuslt);

5,根据某字段对list进行分组

List<Fruit> fruitList = Lists.newArrayList(new Fruit("apple", 6),new Fruit("apple", 6),
new Fruit("banana", 7), new Fruit("banana", 7),
new Fruit("banana", 7), new Fruit("grape",8)); Map<String, List<Fruit>> groupMap = fruitList.stream().collect(Collectors.groupingBy(Fruit::getName));

6,根据某字段对list进行分组并计数

//输出结果是{banana=3, apple=2, grape=1}
Map<String, Long> map = fruitList.stream().collect(Collectors.groupingBy(Fruit::getName,Collectors.counting()));

7,根据字段排序

//根据name字段倒排
List<Fruit> list = fruitList.stream().sorted(Comparator.comparing(Fruit::getName).reversed()).collect(Collectors.toList());

8,多条数据取第一条

//equalsIgnoreCase:不考虑大小写;find.isPresent()可用来判断数据是否存在
Optional<Fruit> find = fruitList.stream().filter(i -> i.getName().equalsIgnoreCase("apple")).findFirst();

补充:

1,List<EntityOld>转换为List<EntityNew>

注意:实体类EntityNew中需要有一个EntityOld的转换方法

EntityNew.java

public class EntityNew {
private String newId; private String avatar; //转换方法*
public EntityNew (EntityOld item) {
this.newId = item.getOldId() == null ? "" : item.getOldId();
} public String getNewId() {
return newId;
} public void setNewId(String newId) {
this.newId = newId;
} public String getAvatar() {
return avatar;
} public void setAvatar(String avatar) {
this.avatar = avatar;
}
}

EntityOld.java

public class EntityOld {
private String oldId; private String oldName; public String getOldId() {
return oldId;
} public void setOldId(String oldId) {
this.oldId = oldId;
} public String getOldName() {
return oldName;
} public void setOldName(String oldName) {
this.oldName = oldName;
}
}
上一篇:Spring MVC返回json数据给Android端


下一篇:[bzoj1500 维修数列](NOI2005) (splay)