测试用对象:
package com.demo.lee.util;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Student {
private Integer id;
private String groupId;
private String name;
private Integer age;
}
测试用例:
package com.demo.lee.util;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.groupingBy;
/**
* @author lee
*/
public class ListCollectionTest {
@Test
public void test1() {
//1.分组计数
List<Student> list = Arrays.asList(
new Student(1, "one", "zhao",11),
new Student(2, "one", "qian",22),
new Student(3, "two", "sun",33),
new Student(4, "two", "lee",18));
//1.1根据某个属性分组计数
Map<String, Long> result1 = list.stream().collect(Collectors.groupingBy(Student::getGroupId, Collectors.counting()));
System.out.println(result1);
//1.2根据整个实体对象分组计数,当其为String时常使用
Map<Student, Long> result2 = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(result2);
//1.3根据分组的key值对结果进行排序、放进另一个map中并输出
Map<String, Long> xMap = new HashMap<>();
result1.entrySet().stream().sorted(Map.Entry.<String, Long>comparingByKey().reversed()) //reversed不生效
.forEachOrdered(x -> xMap.put(x.getKey(), x.getValue()));
System.out.println(xMap);
}
@Test
public void test2() {
List<Student> list = Arrays.asList(
new Student(1, "one", "zhao",11),
new Student(2, "one", "qian",22),
new Student(3, "two", "sun",33),
new Student(4, "two", "lee",18));
//2.分组,并统计其中一个属性值得sum或者avg:id总和
Map<String, Integer> result3 = list.stream().collect(
Collectors.groupingBy(Student::getGroupId, Collectors.summingInt(Student::getId))
);
System.out.println(result3);
}
@Test
public void test3(){
List<Student> list = Arrays.asList(
new Student(1, "one", "zhao",11),
new Student(2, "one", "qian",22),
new Student(3, "two", "sun",33),
new Student(4, "two", "lee",18));
//3.根据某个属性过滤
List<Student> list1 = list.stream().filter(a -> a.getName().contains("a")).collect(Collectors.toList());
System.out.println(list1);
}
@Test
public void test4(){
List<Student> list = Arrays.asList(
new Student(1, "one", "zhao",11),
new Student(2, "one", "qian",22),
new Student(3, "two", "sun",33),
new Student(4, "two", "lee",18));
//4.根据对象属性进行分组,自行编辑分组条件
Map<String, List<Student>> collect = list.stream().collect(groupingBy(s -> {
Integer age = s.getAge();
if (0<age && age<=10){
return "baby";
}else if (10<age && age<=20){
return "teenager";
}else if (20<age && age<=30){
return "youth";
}else {
return "old";
}
}));
System.out.println(collect);
}
@test
public void test5(){
List<Student> list = Arrays.asList(
new Student(1, "one", "zhao",11),
new Student(2, "one", "qian",22),
new Student(3, "two", "sun",33),
new Student(4, "two", "lee",18));
//对象属性拼字符串
String investor = list.stream().map(Student::getName).collect(Collectors.joining(",")
}
@test
public void test6(){
List<Student> list = Arrays.asList(
new Student(1, "one", "zhao",11),
new Student(2, "one", "qian",22),
new Student(3, "two", "sun",33),
new Student(4, "two", "lee",18));
//对象属性求和
Integer result = list.stream().collect(Collectors.summingInt(Student::getAge));
System.out.println("所有学生年龄之和 : " + reuslt);
}
}```