1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| package com.hbq.codedemopersion.common.controller.test;
@Slf4j public class StreamAndLambdaTest { public static void main(String[] args) { log.info("-------------数组流------------"); int[] array = new int[]{1, 2, 3}; int first = Arrays.stream(array) .findFirst() .orElseThrow(); log.info("[取第一项] array:{} -> first:{}", array, first); log.info("");
log.info("-------------集合流------------"); List<F> list = List.of( F.builder().age(1).name("f1").build(), F.builder().age(2).name("f2").build(), F.builder().age(3).name("f1").build()); log.info("↓↓↓↓↓原集合:{}↓↓↓↓↓", list); list.forEach(e -> log.info("[遍历]{} {}", e.getAge(), e));
F f = list.stream() .max(Comparator.comparing(F::getAge)) .orElseThrow(); log.info("[取Age最大项] maxModel:{}", f);
List<String> nameList = list.stream() .map(F::getName) .collect(Collectors.toList()); log.info("[取值转换] nameList:{}", nameList);
String listString = list.stream() .map(F::getName) .collect(Collectors.joining(",", "[", "]")); log.info("[转换后数据处理] listString:{}", listString);
List<F> filterList = list.stream() .filter(e -> e.getAge() != 2) .collect(Collectors.toList()); log.info("[过滤出Age不等于2] filterList:{}", filterList);
Map<String, List<F>> groupMap = list.stream() .collect(Collectors.groupingBy(F::getName)); log.info("[分组] groupMap:{}", groupMap); log.info("");
log.info("-------------map流------------"); Map<Integer, String> map = new HashMap<>(8) {{ put(1, "HUI"); put(2, "XUE"); }}; Map<Integer, String> newMap = map.entrySet() .stream() .collect(Collectors.toMap(e -> e.getKey() + 1, e -> e.getValue() + 1)); log.info("map:{} -> newMap:{}", map, newMap); String collect = map.entrySet().stream() .map(e -> new AutoModel(e.getKey(), e.getValue())) .filter(e -> e.getId() < 100) .limit(2) .distinct() .sorted(Comparator.comparing(AutoModel::getId)) .max(Comparator.comparing(AutoModel::getId)) .stream().map(AutoModel::getName) .collect(Collectors.joining(",", "[", "]")); log.info("[组合转换]collect:{}", collect); log.info("");
log.info("-------------对象list流按照(字段)字段去重并处理其他字段------------"); List<F.S> s1 = new ArrayList<>(); s1.add(F.S.builder().name("s1").build()); s1.add(F.S.builder().name("s2").build()); List<F.S> s2 = new ArrayList<>(); s2.add(F.S.builder().name("s2").build()); s2.add(F.S.builder().name("s3").build()); List<F.S> s3 = new ArrayList<>(); s3.add(F.S.builder().name("s2").build()); s3.add(F.S.builder().name("s3").build());
F f1 = F.builder().name("f1").password("p1").age(500).s(s1).build(); F f2 = F.builder().name("f1").password("p1").age(600).s(s2).build(); F f3 = F.builder().name("f2").age(500).s(s3).build(); List<F> orgList = List.of(f1, f2, f3); log.info("[原集合orgList] {}", orgList); List<F> nowList = new ArrayList<>(new ArrayList<>(orgList.stream().collect( Collectors.toMap(F::fetchGroupKey, Function.identity(), (o1, o2) -> { o1.setAge(o1.getAge() + o2.getAge()); CollUtil.addAll(o1.getS(), o2.getS()); o1.setS(o1.getS().stream().distinct().collect(Collectors.toList())); return o1; })).values())); log.info("[现集合nowList] {}", nowList); log.info("");
log.info("-------------FlatMap双层遍历------------"); String names = "f1,f2"; String ages = "1,2"; log.info("[原集合] names:{} ages:{}", names, ages); List<F> nowFlatList = Arrays.stream(names.split(",")) .map(name -> Arrays.stream(ages.split(",")) .map(Integer::valueOf) .map(age -> F.builder().age(age).name(name).build()) .collect(Collectors.toList())) .flatMap(Collection::stream) .collect(Collectors.toList()); log.info("[新集合nowFlatList] {}", nowFlatList); } }
Map<Integer, String> idToNameMap = resultList.stream().collect(Collectors.toMap(Grouping::getId, Grouping::getName));
|