jdk1.8流處理Dmeo
阿新 • • 發佈:2021-08-23
Stream<String> list = Stream.of("A","B","C","D"); Optional<String> first = list.findFirst(); System.out.println(first.get()); List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); //map資料流的對映處理 List<Integer> squares = numbers.stream().map(n -> n * n).collect(Collectors.toList()); System.out.println(squares); List美夢成真,變為事實。<Integer> numbers1 = Arrays.asList(1, 2, 3); List<Integer> numbers2 = Arrays.asList(3, 4); //flatMap 處理多個流的邏輯最後轉換為一個流 List<int[]> pairs = numbers1.stream().flatMap(i -> numbers2.stream().map(j -> new int[]{i, j})).collect(Collectors.toList()); System.out.println(pairs);//條件查詢匹配資料 anyMatch, allMatch,noneMatch,findFirst和findAny Predicate<Integer> predicate = a->a.equals(1); boolean b = numbers1.stream().anyMatch(predicate);//只要有元素匹配則返回true System.out.println(b); boolean a = numbers1.stream().allMatch(predicate);//所有元素都匹配則返回true System.out.println(a);boolean c = numbers1.stream().noneMatch(predicate);//所有元素都不匹配則返回true System.out.println(c); Integer d = numbers1.stream().findFirst().orElse(0);//獲取第一個元素 System.out.println(d); Integer e = numbers1.stream().findAny().orElse(0);//獲取任意一個元素 System.out.println(e); //彙總Collectors.summarizingInt, summarizingLong, summarizingDouble 資料統計 IntSummaryStatistics collect = numbers1.stream().collect(Collectors.summarizingInt(Integer::intValue)); System.out.println(collect.getSum()); System.out.println(collect.getAverage()); System.out.println(collect.getMax()); System.out.println(collect.getCount()); //# Collectors.joining Stream<String> integerStream = Stream.of("1", "2", "3", "2", "4", "5"); String s = integerStream.collect(Collectors.joining(",")); System.out.println(s); //Collectors.groupingBy 分組 Stream<String> groupingByStream = Stream.of("1", "2", "3", "2", "4", "5"); Map<Boolean, List<String>> collect2 = groupingByStream.collect(Collectors.groupingBy(as -> Integer.parseInt(as) % 2 == 0)); System.out.println(collect2); //分組: groupingBy + mapping Stream<String> groupingByStream2 = Stream.of("1", "2", "3", "2", "4", "5"); Map<Boolean, Set<String>> collect4 = groupingByStream2.collect(Collectors.groupingBy(as -> Integer.parseInt(as) % 2 == 0, Collectors.mapping(xx -> xx+ "demo", Collectors.toSet()))); System.out.println(collect4); //多級分組: groupingBy + groupingBy Stream<String> groupingByStream1 = Stream.of("1", "2", "3", "2", "4", "5"); Map<Boolean, Map<Boolean, List<String>>> collect3 = groupingByStream1.collect(Collectors.groupingBy(as -> Integer.parseInt(as) % 2 == 0, Collectors.groupingBy(xx -> Integer.parseInt(xx) > 2))); System.out.println(collect3); Stream<String> stringStream = Stream.of("1", "2", "3", "2", "4", "5"); ArrayList<Object> collect1 = stringStream.collect(ArrayList::new, List::add, List::addAll); System.out.println(JSON.toJSONString(collect1)); //reduce 歸約 T reduce(T identity, BinaryOperator<T> accumulator) identity初始化值 Stream<String> stringStream1 = Stream.of("1", "2", "3", "2", "4", "5"); String s1 = stringStream1.reduce("start", (x, y) -> x+","+y); System.out.println(s1);