-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava8StreamExample.java
More file actions
161 lines (136 loc) · 5.65 KB
/
Copy pathJava8StreamExample.java
File metadata and controls
161 lines (136 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
* Created by john on 2016/1/8.
*/
public class Java8StreamExample {
public static void main(String[] args) {
System.out.println("Using Java7");
//Count empty string
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
System.out.println("List: " +strings);
long count = getCountEmptyStringUsingJava7(strings);
System.out.println("Empty string: "+count);
count = getCountLength3UsingJava7(strings);
System.out.println("String of length 3:"+count);
//Eliminate empty string
List<String> filtered = deleteEmptyStringUsingJava7(strings);
System.out.println("Filtered List: "+filtered);
//Eliminate empty string and join using comma
String mergeString = getMergedStringUsingJava7(strings,",");
System.out.println("Merged string: "+mergeString);
List<Integer> numbers = Arrays.asList(1,2,13,4,15,6,17,8,19);
System.out.println("List: "+numbers);
System.out.println("The highest number in list: "+getMax(numbers));
System.out.println("The lowest number in list: "+getMin(numbers));
System.out.println("The sum of all numbers: "+getSum(numbers));
System.out.println("The average of all list: "+getAverage(numbers));
//print ten random numbers
Random random = new Random();
for(int i=0;i<10;i++)
System.out.println(random.nextInt());
System.out.println("Using Java8");
System.out.println("List: "+strings);
count = strings.stream().filter(string->string.isEmpty()).count();
System.out.println("Empty string: "+count);
count = strings.stream().filter(string->string.length()==3).count();
System.out.println("String of length 3:"+count);
filtered = strings.stream().filter(string->!string.isEmpty()).collect(Collectors.toList());
System.out.println("Filtered List: "+filtered);
mergeString = strings.stream().filter(string->!string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("Merged string: "+mergeString);
IntSummaryStatistics stats = numbers.stream().mapToInt((x)->x).summaryStatistics();
System.out.println("List: "+numbers);
System.out.println("The highest number in list: "+stats.getMax());
System.out.println("The lowest number in list: "+stats.getMin());
System.out.println("The sum of all numbers: "+stats.getSum());
System.out.println("The average of all list: "+stats.getAverage());
random.ints().limit(10).sorted().forEach(System.out::println);
//parallel processing
count = strings.parallelStream().filter(string->string.isEmpty()).count();
System.out.println("Empty string: "+count);
}
private static int getCountEmptyStringUsingJava7(List<String> strings) {
int count =0;
for(String str : strings) {
if(str.isEmpty())
count++;
}
return count;
}
private static int getCountLength3UsingJava7(List<String> strings) {
int count =0;
for(String str : strings) {
if(str.length() == 3) {
count++;
}
}
return count;
}
private static List<String> deleteEmptyStringUsingJava7(List<String> strings) {
List<String> filteredList = new ArrayList<String>();
for(String str : strings) {
if(!str.isEmpty())
filteredList.add(str);
}
return filteredList;
}
// private static String getMergedStringUsingJava7(List<String> strings,String separator) {
// StringBuilder builder = new StringBuilder();
// for(String str : strings) {
// if(!strings.isEmpty()) {
// builder.append(str);
// builder.append(separator);
// }
// }
// String mergedString =builder.toString();
// return mergedString.substring(0,mergedString.length()-2);
// }
private static String getMergedStringUsingJava7(List<String> strings, String separator){
StringBuilder stringBuilder = new StringBuilder();
for(String string: strings){
if(!string.isEmpty()){
stringBuilder.append(string);
stringBuilder.append(separator);
}
}
String mergedString = stringBuilder.toString();
return mergedString.substring(0, mergedString.length()-2);
}
private static List<Integer> getSquares(List<Integer> numbers) {
List<Integer> squares = new ArrayList<Integer>();
for(Integer num : numbers) {
Integer sequare = new Integer(num.intValue() * num.intValue());
if(!squares.contains(sequare))
squares.add(sequare);
}
return squares;
}
private static int getMax(List<Integer> numbers) {
int max = numbers.get(0);
for(Integer num : numbers) {
if(max < num.intValue())
max = num.intValue();
}
return max;
}
private static int getMin(List<Integer> numbers) {
int min = numbers.get(0);
for(Integer num : numbers) {
if(min > num.intValue())
min = num.intValue();
}
return min;
}
private static int getSum(List numbers){
int sum = (int)(numbers.get(0));
for(int i=1;i < numbers.size();i++){
sum += (int)numbers.get(i);
}
return sum;
}
private static int getAverage(List<Integer> numbers){
return getSum(numbers) / numbers.size();
}
}