public class TestIntStreamWithCollectors
1: //TestIntStreamWithCollectors.java
3: import java.util.stream.Collectors;
4: import java.util.stream.IntStream;
6: public class TestIntStreamWithCollectors
7: {
8: public static void main(String[] args)
9: {
10: //Prints the values one per line ...
11: IntStream
12: .range(1, 10)
13: .forEach(System.out::println);
15: //But what if you want them all on one line?
16: System.out.println
17: (
18: IntStream
19: .range(1, 10)
20: .boxed() //Converts the int values to Integer
21: .map(i -> i.toString())
22: .collect(Collectors.joining(" "))
23: );
25: //Note the "closed" range and the additional
26: //intermediate filter() operation ...
27: System.out.println
28: (
29: IntStream
30: .rangeClosed(1, 100)
31: .filter(i -> i / 10 + i % 10 > 12)
32: .boxed()
33: .map(i -> i.toString())
34: .collect(Collectors.joining(" "))
35: );
36: }
37: }
38: /* Output:
39: 1
40: 2
41: 3
42: 4
43: 5
44: 6
45: 7
46: 8
47: 9
48: 1 2 3 4 5 6 7 8 9
49: 49 58 59 67 68 69 76 77 78 79 85 86 87 88 89 94 95 96 97 98 99
50: */