11. Streams
Programming Project 2021/22

11.3. IntStream

Creating int streams

public class IntStreamRange {

   public static void main(String[] args) {
      IntStream.range(0, 10)
               .forEach(x -> System.out.print(x + " "));
      System.out.println();

      IntStream.rangeClosed(0, 10)
               .forEach(x -> System.out.print(x + " "));
      System.out.println();
   }

}
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 10 

Creating int streams with iterate and generate

public class IntStreamIterateGenerate {

   public static void main(String[] args) {
      IntStream.iterate(1, x -> x * 2)
               .limit(5)
               .forEach(x -> System.out.print(x + " "));

      System.out.println();

      IntStream.iterate(0, x -> x + 3)
               .limit(5)
               .forEach(x -> System.out.print(x + " "));

      System.out.println();

      Random r = new Random();
      IntStream.generate( () -> r.nextInt(50))
               .limit(5)
               .forEach(x -> System.out.print(x + " "));
   }

}
1 2 4 8 16 
0 3 6 9 12 
36 41 28 33 24 

Creating int streams from arrays

public class IntStreamFromArray {

   public static void main(String[] args) {
      int[] values = {3, 13, 6, -1, 4, 8, 2, 5, 9};
      IntStream.of(values)
               .forEach(x -> System.out.print(x + " "));

      System.out.println();

      IntStream.of(2, 7, 8, 1)
               .forEach(x -> System.out.print(x + " "));

      System.out.println();
      
      ArrayList<Integer> list = new ArrayList<>();
      list.add(9);
      list.add(2);
      list.add(5);

      list.stream()
          .forEach(x -> System.out.print(x + " "));
   }

}
3 13 6 -1 4 8 2 5 9 
2 7 8 1 
9 2 5 

Building streams manually

public class IntStreamBuilder {

   public static void main(String[] args) {
      IntStream.builder()
              .add(0)
              .add(1)
              .add(2)
              .build()
              .forEach(x -> System.out.print(x + " "));
   }

}
0 1 2 

IntStream basic operations

public class IntStreamOperations {
   public static void main(String[] args) {
      int[] values = {3, 13, 6, -1, 4, 8, 2, 5, 9};

      // Display original values
      System.out.print("Original values: ");
      IntStream.of(values).forEach(value -> System.out.printf("%d ", value));

      // count, min, max, sum, and average of the values
      long count = IntStream.of(values).count();
      System.out.printf("%nCount: %d%n", count);

      int min = IntStream.of(values).min().getAsInt();
      System.out.printf("Min: %d%n", min);

      int max = IntStream.of(values).max().getAsInt();
      System.out.printf("Max: %d%n", max);

      int sum = IntStream.of(values).sum();
      System.out.printf("Sum: %d%n", sum);

      double average = IntStream.of(values).average().getAsDouble();
      System.out.printf("Average: %f%n", average);
   }
}
Original values: 3 13 6 -1 4 8 2 5 9 
Count: 9
Min: -1
Max: 13
Sum: 49
Average: 5,444444

IntStream summary statistics

public class IntStreamSummary {
   public static void main(String[] args) {
      int[] values = {3, 13, 6, -1, 4, 8, 2, 5, 9};

      // Display original values
      System.out.print("Original values: ");
      IntStream.of(values)
               .forEach(value -> System.out.printf("%d ", value));

      // count, min, max, sum, and average of the values
      IntSummaryStatistics stat = IntStream.of(values).summaryStatistics();

      System.out.printf("%nCount: %d%n", stat.getCount());
      System.out.printf("Min: %d%n", stat.getMin());
      System.out.printf("Max: %d%n", stat.getMax());
      System.out.printf("Sum: %d%n", stat.getSum());
      System.out.printf("Average: %f%n", stat.getAverage());
   }
}
Original values: 3 13 6 -1 4 8 2 5 9 
Count: 9
Min: -1
Max: 13
Sum: 49
Average: 5,444444

IntStream reduce

public class IntStreamReduce {

   public static void main(String[] args) {
      int[] values = {1, 2, 3, 4, 5};

      int sum = IntStream.of(values)
                         .reduce(0, (x, y) -> x + y);
      System.out.println("Sum: " + sum);

      int sumOfSquares = IntStream.of(values)
                                  .reduce(0, (x, y) -> x + y * y);
      System.out.println("Sum of squares: " + sumOfSquares);

      int product = IntStream.of(values)
                             .reduce(1, (x, y) -> x * y);
      System.out.println("Product: " + product);
   }

}
Sum: 15
Sum of squares: 55
Product: 120

Filtering, mapping, and sorting

public class IntStreamFilterSortMap {

   public static void main(String[] args) {
      int[] values = {8, 3, 1, 4, 7, 6, 5, 2};

      IntStream.of(values)
               .filter(i -> i % 2 == 0)
               .map(i -> i * i)
               .sorted()
               .forEach(i -> System.out.printf("%d ", i));
   }

}
4 16 36 64 

Finding and matching

public class IntStreamMatchFind {
  
   public static void main(String[] args) {
      boolean allEven = IntStream.range(0, 10)
              .allMatch(value -> value % 2 == 0);
      System.out.printf("All numbers are even: %b%n", allEven);

      boolean anyEven = IntStream.range(0, 10)
              .anyMatch(value -> value % 2 == 0);
      System.out.printf("Some number is even: %b%n", anyEven);

      int first = IntStream.range(0, 10)
              .findFirst()
              .getAsInt();
      System.out.printf("First number: %d%n", first);

      int any = IntStream.range(0, 10)
              .findAny()
              .getAsInt();
      System.out.printf("Any number: %d%n", any);
   }
   
}
All numbers are even: false
Some number is even: true
First number: 0
Any number: 0

Mapping int streams

public class IntStreamMapping {
   public static void main(String[] args) {
      IntStream.range(0, 4)
              .map(i -> i * i * i)
              .forEach(i -> System.out.printf("%d ", i));
      
      System.out.println();
      IntStream.range(0, 4)
              .mapToDouble(i -> i)
              .forEach(d -> System.out.printf("%f ", d));

      System.out.println();
      IntStream.range(0, 4)
              .mapToLong(i -> i)
              .forEach(l -> System.out.printf("%d ", l));

      System.out.println();
      IntStream.range(0, 4)
              .mapToObj(String::valueOf)
              .forEach(s -> System.out.printf("%s ", s));
   }
}
0 1 8 27 
0,000000 1,000000 2,000000 3,000000 
0 1 2 3 
0 1 2 3 

Collecting int streams

public class IntStreamCollect {

   public static void main(String[] args) {
      List<Integer> values = IntStream.range(0, 10)
              .boxed()
              .collect(Collectors.toList());

      System.out.println(values);

      String s = IntStream.range(0, 10)
              .mapToObj(x -> String.valueOf(x))
              .collect(Collectors.joining(" - ", "{", "}"));

      System.out.println(s);
   }

}
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
{0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9}

Exercise

  1. Without using streams, write a program that emulates throwing a dice 6.000.000 times, while keeping track of the results.
  1. Now, write an equivalent program using streams.