原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12812560.html
filter
@Test public void filterTest() { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7); List<Integer> resultList = list.stream().filter(i -> i % 2 == 0).collect(Collectors.toList()); // [2, 4, 6] System.out.println(resultList); }
distinct
@Test public void distinctTest() { List<Integer> list = Arrays.asList(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6); List<Integer> resultList = list.stream().distinct().collect(Collectors.toList()); // [1, 2, 3, 4, 5, 6] System.out.println(resultList); }
skip
@Test public void skipTest() { List<Integer> list = Arrays.asList(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6); List<Integer> result = list.stream().skip(6).collect(Collectors.toList()); // [4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6] System.out.println(result); }
limit
@Test public void limitTest() { List<Integer> list = Arrays.asList(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6); List<Integer> resultList = list.stream().limit(6).collect(Collectors.toList()); // [1, 2, 2, 3, 3, 3] System.out.println(resultList); }
map
@Test public void mapTest() { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7); List<Integer> resultList = list.stream().map(i -> i * 2).collect(Collectors.toList()); // [2, 4, 6, 8, 10, 12, 14] System.out.println(resultList); }
flatMap
@Test public void flatMapTest() { List<Integer> listA = Arrays.asList(1, 2, 3); List<Integer> listB = Arrays.asList(4, 5); List<Integer> listC = Arrays.asList(6, 7, 8); List<List<Integer>> listOfList = Arrays.asList(listA, listB, listC); List<Integer> resultList1 = listOfList.stream().flatMap(Collection::stream).collect(Collectors.toList()); // [1, 2, 3, 4, 5, 6, 7, 8] System.out.println(resultList1); String[] words = {"Hello", "World"}; List<String> resultList2 = Arrays.stream(words).map(w -> w.split("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList()); // [H, e, l, o, W, r, d] System.out.println(resultList2); List<Integer> list = Arrays.asList(1, 2, 3); List<Integer> resultList = list.stream().flatMap(p -> Stream.generate(() -> p).limit(3)).collect(Collectors.toList()); // [1, 1, 1, 2, 2, 2, 3, 3, 3] System.out.println(resultList); }
optional.get
@Test public void optionalGetTest() { Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); Optional<Integer> optional = stream.filter(i -> i % 2 == 0).findAny(); // 2 System.out.println(optional.get()); }
optional.orElse
@Test public void optionalOrElseTest() { Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); Optional<Integer> optional = stream.filter(i -> i > 10).findAny(); // -1 System.out.println(optional.orElse(-1)); }
optional.ifPresent
@Test public void optionalIfPresentTest() { Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); Optional<Integer> optional = stream.filter(i -> i % 2 == 1).findFirst(); // 1 optional.ifPresent(System.out::println); }
optional.isPresent
@Test public void optionalIsPresentTest() { Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); Optional<Integer> optional = stream.filter(i -> i % 2 == 0).findFirst(); boolean result = optional.isPresent(); // true System.out.println(result); }
allMatch
@Test public void allMatchTest() { Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); boolean result = stream.allMatch(i -> i > 0); // true System.out.println(result); }
anyMatch
@Test public void anyMatchTest() { Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); boolean result = stream.anyMatch(i -> i > 6); // true System.out.println(result); }
noneMatch
@Test public void noneMatchTest() { Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); boolean result = stream.noneMatch(i -> i < 0); // true System.out.println(result); }
reduce Sum
@Test public void reduceSumTest() { Stream<Integer> stream1 = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); Integer reduce1 = stream1.reduce(0, (i, j) -> i + j); // 28 System.out.println(reduce1); Stream<Integer> stream2 = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); Integer reduce2 = stream2.reduce(0, Integer::sum); // 28 System.out.println(reduce2); Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); // 28 stream.reduce(Integer::sum).ifPresent(System.out::println); }
reduce Max
@Test public void reduceMaxTest() { Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); // 7 stream.reduce(Integer::max).ifPresent(System.out::println); }
reduce Min
@Test public void reduceMinTest() { Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); // 1 stream.reduce(Integer::min).ifPresent(System.out::println); }
reduce
@Test public void reduceTest() { Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); Integer result = stream.filter(i -> i % 2 == 0).reduce(1, (i, j) -> i * j); // 48 Optional.of(result).ifPresent(System.out::println); }