Memos for Java Stream API

less than 1 minute read

Stream.generate

Random random = new Random();
List<Integer> numbers = Stream
    .generate(() -> random.nextInt(100))
    .limit(5)
    .collect(Collectors.toList());

convert list of Integer to int array

List<Integer> list = Arrays.asList(5, 2, 1);
int[] arr = list.stream()
  .mapToInt(Integer::intValue)
  .sorted()
  .toArray();
// int[3] { 1, 2, 5 }

Updated:

Comments