通过Stream.of()方法创建动态集合
List<StudentInfo> collect = Stream.of(new StudentInfo(1,"aa","男",18),
new StudentInfo(2,"bb","女",19),new StudentInfo(3,"cc","女",20)).collect(Collectors.toList());
通过集合的stream()方法取出对象中的某一列
List<String> collect1 = collect.stream().map(StudentInfo::getName).collect(Collectors.toList());
通过集合的stream()方法取出对象中的某一列
List<StudentInfo> collect2 = collect.stream().filter(stu -> stu.getAge() == 18).collect(Collectors.toList());
通过集合的stream()方法的collect方法,填入对应参数进行获取
Map<String, List<StudentInfo>> collect3 = collect.stream().collect(Collectors.groupingBy(StudentInfo::getSex, Collectors.toList()));
int sum = collect.stream().mapToInt(StudentInfo::getAge).sum(); // 57
double sum1 = collect.stream().mapToDouble(StudentInfo::getAge).sum(); // 57.0
long sum2 = collect.stream().mapToLong(StudentInfo::getAge).sum(); // 57
Map<String, StudentInfo> collect4 = collect.stream().collect(Collectors.toMap(StudentInfo::getName, Function.identity()));
Map<String,Object> map = new HashMap<>();
map.put("id",1);
map.put("name", "dd");
map.put("age",23);
List<Object> collect5 = new ArrayList<>(map.values());
boolean b = collect.stream().anyMatch(stu -> stu.getAge() >= 20); // true
boolean b1 = collect.stream().allMatch(stu -> stu.getAge() >= 20); // false
long count = collect.stream().filter(stu -> stu.getAge() >=19).count(); // 2
List<StudentInfo> oneList = Stream.of(new StudentInfo(1,"aa","男",18),
new StudentInfo(2,"bb","女",19),new StudentInfo(3,"cc","女",20)).collect(Collectors.toList());
List<StudentInfo> twoList = Stream.of(new StudentInfo(1,"aa","男",18),
new StudentInfo(2,"bb","女",19),new StudentInfo(3,"cc","女",20)).collect(Collectors.toList());
List<StudentInfo> threeList = Stream.of(new StudentInfo(1,"aa","男",18),
new StudentInfo(2,"bb","女",19),new StudentInfo(3,"cc","女",20)).collect(Collectors.toList());
// 合并操作
List<StudentInfo> collect6 = Stream.of(oneList, twoList, threeList).flatMap(Collection::stream).collect(Collectors.toList());
List<List<StudentInfo>> lists = Arrays.asList(oneList, twoList, threeList);
List<StudentInfo> collect7 = lists.stream().flatMap(Collection::stream).collect(Collectors.toList());
List<StudentInfo> sorted1 = collect.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());// 正序
List<StudentInfo> sorted2 = collect.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList()); // 降序
测试数据:
Map<String,Object> one = new HashMap<>();
one.put("id",1);
one.put("name","aa");
one.put("height",180.10);
Map<String,Object> two = new HashMap<>();
two.put("id",2);
two.put("name","bb");
two.put("height",182.22);
Map<String,Object> three = new HashMap<>();
three.put("id",3);
three.put("name","cc");
three.put("height",184.45);
List<Map<String,Object>> list = Stream.of(one,two,three).collect(Collectors.toList());
List<Object> height = list.stream().map(map -> map.get("height")).collect(Collectors.toList());
List<Map<String, Object>> height1 = list.stream().filter(map -> new BigDecimal(map.getOrDefault("height", 0).toString()).compareTo(BigDecimal.valueOf(182)) > 0).collect(Collectors.toList());
为方便测试此处加了一条数据
Map<String,Object> four = new HashMap<>();
four.put("id",4);
four.put("name","cc");
four.put("height",186.66);
list.add(four);
Map<Object, List<Map<String, Object>>> name = list.stream().collect(Collectors.groupingBy(map -> map.get("name")));
double height2 = list.stream().mapToDouble(map -> Double.parseDouble(map.get("height").toString())).sum();
BigDecimal height3 = list.stream().map(map -> new BigDecimal(map.get("height").toString())).reduce(BigDecimal.ZERO, BigDecimal::add);
Map<Object, Map<String, Object>> id = list.stream().collect(Collectors.toMap(map -> map.get("id"), Function.identity()));
// true
boolean height4 = list.stream().anyMatch(map -> new BigDecimal(map.get("height").toString()).compareTo(BigDecimal.valueOf(182)) > 0);
// false
boolean height5 = list.stream().allMatch(map -> new BigDecimal(map.get("height").toString()).compareTo(BigDecimal.valueOf(182)) > 0);
// 3
long height6 = list.stream().filter(map -> new BigDecimal(map.get("height").toString()).compareTo(BigDecimal.valueOf(182)) > 0).count();
List<Map<String, Object>> height7 = list.stream().sorted(Comparator.comparing(map -> new BigDecimal(map.get("height").toString()))).collect(Collectors.toList());
List<Map<String, Object>> height8 = list.stream().sorted(Comparator.comparing((Map map) -> new BigDecimal(map.get("height").toString())).reversed()).collect(Collectors.toList());