分组和遍历
@Testvoid testMap() {List<Student> students = new ArrayList<>();students.add(new Student(1, "zhang", 10, LocalDateTime.now()));students.add(new Student(1, "zhang2", 100, LocalDateTime.now()));students.add(new Student(2, "wang", 11, LocalDateTime.now()));students.add(new Student(3, "lin", 12, LocalDateTime.now()));Map<Integer, List<Student>> collect = students.stream().collect(Collectors.groupingBy(Student::getAge));Set<Map.Entry<Integer, List<Student>>> entries = collect.entrySet();entries.stream().forEach(resultByAge->{System.out.println(resultByAge.getKey());resultByAge.getValue().forEach(System.out::println);});}
结果如下
1Student(age=1, name=zhang, weight=10, localDateTime=2021-11-16T00:32:08.732)Student(age=1, name=zhang2, weight=100, localDateTime=2021-11-16T00:32:08.732)2Student(age=2, name=wang, weight=11, localDateTime=2021-11-16T00:32:08.732)3Student(age=3, name=lin, weight=12, localDateTime=2021-11-16T00:32:08.732)
映射拼接
项目中比较常见将数字组合的数据转换成中文后再进行拼接,往常是用split方法变为数组,再遍历数据,再拼接,比较麻烦,用流回直接一些
String jobCN = Arrays.stream(split).map(str -> codeLoader.getCodeStrToZwIfExists(str, "DM_JOB")).collect(Collectors.joining(","));
集合或者数组中有时候需要将首字母大写
Stream.of("aaa", "bbb", "ccc")// 首字母大写方法, String::toUpperCase是所有字母大写.map(StringUtils::capitalize).forEach(System.out::println);
过滤筛选
使用filter代替for循环判断筛选数据信息,使代码语义更加明确
stringObjectMap.entrySet().stream().filter(e -> Objects.nonNull(e.getValue())).forEach(entry -> {System.out.println(entry.getKey() + ":" + entry.getValue());});
List<Sku> collect = cartSkuList.stream().filter(sku -> SkuCategoryEnum.ELECTRONICS.equals(sku.getSkuCategory())).collect(Collectors.toList());
大杂烩
@Testpublic void test1() {Comparator<Integer> com = Integer::compare;TreeSet<Integer> integers = new TreeSet<>(com);integers.add(2);integers.add(3);integers.add(1);System.out.println(integers);}@Testpublic void test2() {List<EmpolyeeEntity> empolyeeEntities = initList();List<EmpolyeeEntity> collect = empolyeeEntities.stream().filter(i -> i.getAge() > 5).filter(i -> i.getSalary().compareTo(new BigDecimal(50)) > 0).collect(Collectors.toList());System.out.println(collect);// 两个字段同时降序(升序)List<EmpolyeeEntity> collect1 = empolyeeEntities.stream().sorted(comparing(EmpolyeeEntity::getAge).thenComparing(EmpolyeeEntity::getSalary).reversed()).collect(Collectors.toList());System.out.println(collect1);// 更具某个字段找最小的数据,最大同理EmpolyeeEntity obj = empolyeeEntities.stream().min(comparing(EmpolyeeEntity::getAge)).orElse(new EmpolyeeEntity());System.out.println(obj);}@Testpublic void test3() {List<EmpolyeeEntity> empolyeeEntities = initList();Integer integer = empolyeeEntities.stream().filter(i -> i.getAge() < 10).map(EmpolyeeEntity::getAge).reduce(Integer::sum).orElse(0);System.out.println(integer);Integer sum = empolyeeEntities.stream().filter(i -> i.getAge() < 10).mapToInt(EmpolyeeEntity::getAge).sum();System.out.println(sum);}@Testpublic void test4() {String name = Optional.of(new EmpolyeeEntity(null, 1, new BigDecimal(5))).orElse(new EmpolyeeEntity("1", 2, new BigDecimal(10))).getName();System.out.println(name);}@Testpublic void test5() {boolean b = Stream.of(null, "", "00", "1").allMatch(value -> StringUtils.isBlank(value) || "00".equals(value));System.out.println(b);}@Testpublic void test6() {// 合理使用contains 可代替anymatchList<String> strings = Arrays.asList(null, "", "00");System.out.println(strings.contains(null));}@Testpublic void test7() {EmpolyeeEntity empolyeeEntity = new EmpolyeeEntity();Field[] declaredFields = empolyeeEntity.getClass().getDeclaredFields();Arrays.stream(declaredFields).forEach(System.out::println);}@Testpublic void test8() {List<String> strings = Arrays.asList("张三", "王都是", "王地方是");String string = strings.stream().filter(i -> i.startsWith("王")).mapToInt(String::length).max().toString();System.out.println(string);}@Testpublic void test9() {boolean b = Stream.of("1", "2", "3").noneMatch(i -> i.equals("4"));System.out.println(b);}private List<EmpolyeeEntity> initList() {return Arrays.asList(new EmpolyeeEntity("zhang", 5, new BigDecimal(100)),new EmpolyeeEntity("li", 10, new BigDecimal(200)),new EmpolyeeEntity("wang", 10, new BigDecimal(400)),new EmpolyeeEntity("chen", 15, new BigDecimal(50)),new EmpolyeeEntity("lin", 20, new BigDecimal(10)));}
