Post

Java Stream

Ordering

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/package-summary.html

Streams may or may not have a defined encounter order. Whether or not a stream has an encounter order depends on the source and the intermediate operations. Certain stream sources (such as List or arrays) are intrinsically ordered, whereas others (such as HashSet) are not. Some intermediate operations, such as sorted(), may impose an encounter order on an otherwise unordered stream, and others may render an ordered stream unordered, such as BaseStream.unordered(). Further, some terminal operations may ignore encounter order, such as forEach().

Stream<T> sorted(Comparator<? super T> comparator): For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.

Summing numbers

Stream.reduce()

T reduce(T identity, BinaryOperator<T> accumulator)

1
2
3
List<Integer> integers = Arrays.asList(0, 1, 2);
Integer sum = integers.stream()
  .reduce(0, (a, b) -> a + b);
1
2
3
List<Integer> integers = Arrays.asList(0, 1, 2);
Integer sum = integers.stream()
  .reduce(0, Integer::sum);

Stream.collect()

1
2
3
List<Integer> integers = Arrays.asList(0, 1, 2);
Integer sum = integers.stream()
  .collect(Collectors.summingInt(Integer::intValue));

IntStream.sum()

1
2
3
4
List<Integer> integers = Arrays.asList(0, 1, 2);
Integer sum = integers.stream()
  .mapToInt(Integer::intValue)
  .sum();

Range

1
2
3
4
5
6
List<Integer> integers = Arrays.asList(0, 1, 2);
Integer sum = integers.stream()
  .skip(1)
  .limit(1)
  .mapToInt(Integer::intValue)
  .sum();

Frequency Map

1
2
Map<Integer, Long> count = Arrays.stream(nums).boxed()
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

Max

1
2
int[] arr = {0, 1, 2};
Arrays.stream(arr).max().getAsInt()
This post is licensed under CC BY 4.0 by the author.