Spaces:
Running
Running
map then filter OR filter (directly)
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/10A. Filtering Elements+++
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The examples presented above have all targeted all operations of a collection.
|
| 2 |
+
However, filtering operations, which can be used to determine which elements we want to keep in the stream, are essential to streams.
|
| 3 |
+
|
| 4 |
+
The filter operation also takes a LAMBDA EXPRESSION as a PARAMETER.
|
| 5 |
+
Only those elements for which the operation defined in the lambda expression returns 'true' REMAIN IN THE STREAM.
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
The example calculates the sum of only the even elements:
|
| 9 |
+
ArrayList<Integer> numbers = new ArrayList<>();
|
| 10 |
+
numbers.add(1);
|
| 11 |
+
numbers.add(2);
|
| 12 |
+
numbers.add(3);
|
| 13 |
+
numbers.add(4);
|
| 14 |
+
|
| 15 |
+
int sumOfEven = numbers.stream().mapToInt(number -> number).filter(number -> number % 2 == 0).sum();
|
| 16 |
+
System.out.println("The Sum of even numbers: " + sumOfEven);
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
The program prints:
|
| 20 |
+
The sum of even numbers: 6
|
| 21 |
+
|
| 22 |
+
So after the filter, only even numbers remain in the stream - all subsequent operations only apply to them.
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
=============================================================
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
Let's look at another example where only the names with more than 4 characters are printed from the stream of STRINGS:
|
| 33 |
+
|
| 34 |
+
ArrayList<String> names = new ArrayList<>();
|
| 35 |
+
names.add("Bob");
|
| 36 |
+
names.add("Elisa");
|
| 37 |
+
names.add("Mia");
|
| 38 |
+
names.add("Marco");
|
| 39 |
+
names.add("Tia");
|
| 40 |
+
names.add("Anneli");
|
| 41 |
+
|
| 42 |
+
names.stream().filter(name -> name.length() > 4).forEach(name -> System.out.println(name));
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
Ohjelma tulostaa:i
|
| 46 |
+
|
| 47 |
+
Elisa
|
| 48 |
+
Marco
|
| 49 |
+
Anneli
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
=============================================================
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
Simple conditions are easy to define directly in the lambda expression,
|
| 61 |
+
but for more complex ones, we can write our OWN FILTER METHOD.
|
| 62 |
+
The method should return true or false depending on whether we want to keep the element in the stream or not.
|
| 63 |
+
|
| 64 |
+
public static void main(String[] args) {
|
| 65 |
+
ArrayList<String> names = new ArrayList<>();
|
| 66 |
+
names.add("Kirsi");
|
| 67 |
+
names.add("Anne");
|
| 68 |
+
names.add("Mia");
|
| 69 |
+
names.add("Aune");
|
| 70 |
+
names.add("Tina");
|
| 71 |
+
names.add("Ester");
|
| 72 |
+
|
| 73 |
+
System.out.println("Names starting with a vowel:");
|
| 74 |
+
names.stream().filter(name -> startsWithVowel(name)).forEach(name -> System.out.println(name));
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
public static boolean startsWithVowel(String name) {
|
| 79 |
+
if ("AEIOU".contains("" + name.charAt(0))) {
|
| 80 |
+
return true;
|
| 81 |
+
}
|
| 82 |
+
else {
|
| 83 |
+
return false;
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
Ohjelma tulostaa:
|
| 89 |
+
Anne
|
| 90 |
+
Aune
|
| 91 |
+
Ester
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
=============================================================
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
Let's look at one more example.
|
| 104 |
+
The method picks only those students from the list whose number of credits is less than 25.
|
| 105 |
+
The average of these points is then calculated.
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
Note how the program uses the operation mapToInt:
|
| 109 |
+
it is now used to pick the number of credits from the student object.
|
| 110 |
+
|
| 111 |
+
public static double averageCredits(ArrayList<Student> students) {
|
| 112 |
+
// Split into two lines due to length
|
| 113 |
+
return students.stream().mapToInt(s -> s.getCredits()).
|
| 114 |
+
filter(credits -> credits < 25).average().getAsDouble();
|
| 115 |
+
|
| 116 |
+
}
|
| 117 |
+
|