public class SomeStreamExamples
1: //SomeStreamExamples.java
2: //Based on some examples from this website:
3: //https://www.tutorialspoint.com/java8/java8_streams.htm
5: import java.util.Arrays;
6: import java.util.IntSummaryStatistics;
7: import java.util.List;
8: import java.util.Random;
9: import java.util.stream.Collectors;
11: public class SomeStreamExamples
12: {
13: public static void main(String[] args)
14: {
15: System.out.println("=====1==============================");
16: //Output just ten random integers, sorted, one per line:
17: new Random()
18: .ints(20, 25)
19: .limit(10)
20: .sorted()
21: .forEach(System.out::println);
22: System.out.println();
24: Random randGen = new Random();
25: randGen.setSeed(100); //Will give same values each time
26: randGen
27: .ints(20, 25)
28: .limit(10)
29: .sorted()
30: .forEach(System.out::println);
32: System.out.println("=====2==============================");
33: List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
34: //You can generate and save a list of unique squares:
35: List<Integer> squaresList = numbers
36: .stream()
37: .map(i -> i * i)
38: .distinct()
39: .collect(Collectors.toList());
41: //But, to display them do this ...
42: numbers
43: .stream()
44: .map(i -> i * i)
45: .distinct()
46: .collect(Collectors.toList())
47: .forEach(System.out::println);
48: //and note that we can't just tack on the forEach()
49: //to the preceding code segment because it's void
50: //and in that segment we're assigning the list.
52: System.out.println("=====3==============================");
53: List<String> strings
54: = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
55: //Get the count of empty strings:
56: long count = strings //Note: count must be "long", not "int"
57: .stream()
58: .filter(string -> string.isEmpty())
59: .count();
60: System.out.println(count);
62: //Display all non-empty strings:
63: strings
64: .stream()
65: .filter(string -> !string.isEmpty())
66: .collect(Collectors.toList())
67: .forEach(System.out::println);
69: System.out.println("=====4==============================");
70: //Filter out the non-empty strings, then combine them into
71: //a single string in which the original strings are separated
72: //by a comma followed by a blank space:
73: List<String> filtered = strings
74: .stream()
75: .filter(string -> !string.isEmpty())
76: .collect(Collectors.toList());
77: System.out.println("Filtered List: " + filtered);
78: String mergedString = strings
79: .stream()
80: .filter(string -> !string.isEmpty())
81: .collect(Collectors.joining(", "));
82: System.out.println("Merged String: " + mergedString);
84: System.out.println("=====5==============================");
85: //Compute some "statistics" on a list of integers:
86: List<Integer> integers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
87: IntSummaryStatistics stats = integers
88: .stream()
89: .mapToInt(x -> x)
90: .summaryStatistics();
91: System.out.println("Highest number in List...... "
92: + stats.getMax());
93: System.out.println("Lowest number in List....... "
94: + stats.getMin());
95: System.out.println("Sum of numbers in List...... "
96: + stats.getSum());
97: System.out.println("Average of numbers in List.. "
98: + stats.getAverage());
99: }
100: }
101: /* Output:
102: =====1==============================
103: 20
104: 20
105: 21
106: 22
107: 23
108: 23
109: 23
110: 24
111: 24
112: 24
114: 20
115: 20
116: 21
117: 21
118: 21
119: 23
120: 23
121: 23
122: 23
123: 24
124: =====2==============================
125: 9
126: 4
127: 49
128: 25
129: =====3==============================
130: 2
131: abc
132: bc
133: efg
134: abcd
135: jkl
136: =====4==============================
137: Filtered List: [abc, bc, efg, abcd, jkl]
138: Merged String: abc, bc, efg, abcd, jkl
139: =====5==============================
140: Highest number in List...... 7
141: Lowest number in List....... 2
142: Sum of numbers in List...... 25
143: Average of numbers in List.. 3.5714285714285716
144: */