学会Stream 流 处理数据,再难我们也不用怕
Collectors.summingInt()分组求和
public class Test {
public static void main(String[] args) {
Student student1 = new Student(1, 1);
Student student2 = new Student(1, 1);
Student student3 = new Student(2, 2);
Student student4 = new Student(2, 3);
Student student5 = new Student(3, 3);
Student student6 = new Student(3, 4);
Student student7 = new Student(4, 1);
List<Student> list = Arrays.asList(student1, student2, student3, student4, student5, student6, student7);
Map<Integer, Integer> collect = list.stream().collect(Collectors.groupingBy(Student::getId, Collectors.summingInt(Student::getScore)));
System.out.println(collect);
}
}
计算一个List对象中某个字段总和
List<User> userList = new ArrayList<>();
User user1 = new User();
user1.setAge(10);
userList.add(user1);
User user2 = new User();
user2.setAge(20);
userList.add(user2);
User user3 = new User();
user3.setAge(25);
userList.add(user3);
int ageSum = userList.stream().collect(Collectors.summingInt(User::getAge));
System.out.println("年龄总和 :" + ageSum);
//下面一位网友提供了一种更加简洁的方式,非常感谢
int ageSumTwo = userList.stream().mapToInt(User::getAge).sum();
System.out.println("年龄总和2: " + ageSumTwo);
两个List 对象 合并成 一个List对象
public static void main(String[] args) {
//构建List集合1
List<Map<String,Object>> list1 = new ArrayList<>();
Map<String,Object> data=new HashMap<>();
data.put("userId","100001");
data.put("userName","唐僧");
list1.add(data);
//构建List集合2
List<Map<String,Object>> list2 = new ArrayList<>();
data=new HashMap<>();
data.put("userId","100001");
data.put("gender","男");
data.put("age",20);
list2.add(data);
//使用stream流把list1和list2根据属性userId合并一个list集合
List<Map<String, Object>> list = list1.stream().map(m -> {
list2.stream().filter(m2-> Objects.equals(m.get("userId"),m2.get("userId"))).forEach(m2-> {
m.put("gender",m2.get("gender"));
m.put("age",m2.get("age"));
});
return m;
}).collect(Collectors.toList());
System.out.println(list);
}
分组 把k转换成字符串
String strYsjl2k = listYsjl.stream().filter(map -> "2000".equals(map.get("shebeizhongleidaima")))
.collect(Collectors.groupingBy(map -> map.get("jianyanxiangmu"))).keySet().toString().replace("[","").replace("]","");
排序、
List<SelectVO> selectVOLista = selectVOList.stream().sorted(Comparator.comparing(SelectVO::getValue, Comparator.naturalOrder())).collect(Collectors.toList());
// 第二种方法
List<SelectVO> selectVOLista = voList.sort(Comparator.comparing(o -> String.valueOf(o.getDepartmentId())));
去重
List<DepartmentVO> uniqueByName = count.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(DepartmentVO::getDepartmentId))), ArrayList::new));
取交集
ArrayList<Integer> list1 = Lists.newArrayList(1,2,3);
ArrayList<Integer> list2 = Lists.newArrayList(3,4,5);
// 取交集
Collection intersection = CollectionUtils.intersection(list1, list2);
System.out.println(intersection);
取并集
HashSet<Integer> set = Sets.newHashSet();
set.addAll(list1);
set.addAll(list2);
System.out.println("unionTest1 并集 去重 :" + set);
// 下面也可以
Collection union = CollectionUtils.union(list1, list2);
System.out.println("unionTest2 并集 :" + union);