Spaces:
Running
Running
OptionalInt; chk ArrayList not empty -> then run getAsInt()
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/09A. sum/average/min/max(), OptionalInt+++
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
A typical example of an operation applied to a collection is calculating the SUM of all elements.
|
| 2 |
+
If we have a list containing numbers as strings, we can easily calculate the sum of its elements by forming a stream from the list:
|
| 3 |
+
|
| 4 |
+
ArrayList<String> numbers = new ArrayList<>();
|
| 5 |
+
|
| 6 |
+
numbers.add("23");
|
| 7 |
+
numbers.add("9");
|
| 8 |
+
numbers.add("7");
|
| 9 |
+
numbers.add("3");
|
| 10 |
+
|
| 11 |
+
int sum = numbers.stream().mapToInt(number -> Integer.valueOf(number)).sum();
|
| 12 |
+
System.out.println("The sum of the numbers: " + sum);
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
The program prints:
|
| 16 |
+
42
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
In this stream, we need the operation 'mapToInt', which converts the pieces into int type.
|
| 20 |
+
Here again, we use a lambda expression and give the operation as Integer's valueOf.
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
==========================================================
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
Somewhat confusingly, the operation is also needed when the list is already of integer type.
|
| 31 |
+
This is because certain terminal operations only work when the elements are of type 'int' ('Integer' is not acceptable):
|
| 32 |
+
|
| 33 |
+
ArrayList<Integer> numbers = new ArrayList<>();
|
| 34 |
+
for (int i=1; i<20; i+=2) {
|
| 35 |
+
numbers.add(i);
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
System.out.println(numbers);
|
| 39 |
+
|
| 40 |
+
// The confusing-looking "change" number -> number converts
|
| 41 |
+
// an Integer stream into an int stream
|
| 42 |
+
System.out.println("The sum of the numbers: " + numbers.stream().mapToInt(number -> number).sum());
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
The program prints:
|
| 46 |
+
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
|
| 47 |
+
The sum of the numbers: 100
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
==========================================================
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
Other handy operations include average, min, and max,
|
| 55 |
+
which return the average, minimum, and maximum of a list of numbers:
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
ArrayList<Integer> numbers = new ArrayList<>();
|
| 60 |
+
for (int i=1; i<10; i++) {
|
| 61 |
+
numbers.add(i);
|
| 62 |
+
}
|
| 63 |
+
System.out.println(numbers);
|
| 64 |
+
|
| 65 |
+
System.out.println("Maximum:");
|
| 66 |
+
System.out.println(numbers.stream().mapToInt(number -> number).max().getAsInt());
|
| 67 |
+
|
| 68 |
+
System.out.println("Minimum:");
|
| 69 |
+
System.out.println(numbers.stream().mapToInt(number -> number).min().getAsInt());
|
| 70 |
+
|
| 71 |
+
System.out.println("Average:");
|
| 72 |
+
System.out.println(numbers.stream().mapToInt(number -> number).average().getAsDouble());
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
The program prints:
|
| 76 |
+
[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
| 77 |
+
Maximum:
|
| 78 |
+
9
|
| 79 |
+
Minimum:
|
| 80 |
+
1
|
| 81 |
+
Average:
|
| 82 |
+
5.0
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
Now, a terminal operation is needed to convert the result into a number, for example, getAsInt().
|
| 90 |
+
This is because the output of the preceding max operation is not an integer but a value of type 'OptionalInt'.
|
| 91 |
+
|
| 92 |
+
An 'extended type' from a normal integer (such as 'OptionalInt') is needed because the list might not contain any values
|
| 93 |
+
- in which case the maximum value of the list cannot be presented as an integer.
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
If the stream is empty when it comes to the max operation, the getAsInt() operation will throw an exception:
|
| 103 |
+
|
| 104 |
+
ArrayList<Integer> numbers = new ArrayList<>();
|
| 105 |
+
|
| 106 |
+
// In an empty stream, there is no maximum...
|
| 107 |
+
int max = numbers.stream().mapToInt(number -> number).max().getAsInt();
|
| 108 |
+
|
| 109 |
+
The programs throws an exception:
|
| 110 |
+
Exception in thread "main" java.util.NoSuchElementException: No value present
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
The problem can be circumvented either by
|
| 119 |
+
1- CHECKING that the LIST is NOT EMPTY or
|
| 120 |
+
2- by performing the 'getAsInt' operation only AFTER it has been confirmed that the VALUE EXISTS:
|
| 121 |
+
|
| 122 |
+
OptionalInt max = numbers.stream().mapToInt(number -> number).max();
|
| 123 |
+
|
| 124 |
+
if (!max.isEmpty()) {
|
| 125 |
+
System.out.println("Maximum: " + max.getAsInt());
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|