Spaces:
Running
Running
stringList.stream().forEach(strElement -> System.out.println(strElement));
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/08A. Streams+++
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
From version 8 onwards, Java has supported so-called "streams".
|
| 2 |
+
With streams, it is easy to HANDLE ALL (OR SUITABLY LIMITED) ELEMENTS of an ENTIRE DATA STRUCTURE at once.
|
| 3 |
+
By combining streams with LAMBDA EXPRESSIONS, Java's features can be stretched towards functional programming languages.
|
| 4 |
+
|
| 5 |
+
In this section, we will look at the operation of streams through a few simple examples.
|
| 6 |
+
The basic idea is that a stream can be created from 'any Collection', such as a list, using the stream method.
|
| 7 |
+
|
| 8 |
+
Various operations can then be targeted at the stream.
|
| 9 |
+
|
| 10 |
+
Then a so-called terminal operation ends the stream, and typically produces
|
| 11 |
+
- a single VALUE or
|
| 12 |
+
- a new COLLECTION of elements as a result.
|
| 13 |
+
https://ville.utu.fi/APP/connector/0/215/source/0c9e196c-6ab5-41d6-9f2c-f4a02c05d1db.png
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
========================================================
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
Processing All Elements: forEach
|
| 21 |
+
|
| 22 |
+
Let's start with an example where we want to apply the SAME OPERATION to ALL ELEMENTS of a collection.
|
| 23 |
+
A typical example would be a situation where we have a list of strings
|
| 24 |
+
and we want to print them one by one.
|
| 25 |
+
|
| 26 |
+
An implementation using a stream would look like this:
|
| 27 |
+
|
| 28 |
+
public static void main(String[] args) {
|
| 29 |
+
ArrayList<String> strings = new ArrayList<>();
|
| 30 |
+
strings.add("first");
|
| 31 |
+
strings.add("second");
|
| 32 |
+
strings.add("third");
|
| 33 |
+
strings.add("fourth");
|
| 34 |
+
|
| 35 |
+
strings.stream().forEach(string -> System.out.println(string));
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
The program prints
|
| 40 |
+
first
|
| 41 |
+
second
|
| 42 |
+
third
|
| 43 |
+
fourth
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
========================================================
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
The forEach method takes the operation to be performed as a parameter.
|
| 59 |
+
A lambda expression is a handy way to define a single operation to be performed.
|
| 60 |
+
|
| 61 |
+
The LAMBDA EXPRESSION is in the form
|
| 62 |
+
parameter -> operation, which uses the parameter
|
| 63 |
+
|
| 64 |
+
...in the example, it's
|
| 65 |
+
string -> System.out.println(string)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
========================================================
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
In practice, it means that the print statement is executed for all elements of the stream.
|
| 80 |
+
Let's look at another example where the method printVowels is called for all elements of the list:
|
| 81 |
+
|
| 82 |
+
public static void main(String[] args) {
|
| 83 |
+
ArrayList<String> ducks = new ArrayList<>();
|
| 84 |
+
ducks.add("Huey");
|
| 85 |
+
ducks.add("Dewey");
|
| 86 |
+
ducks.add("Louie");
|
| 87 |
+
ducks.add("Scrooge");
|
| 88 |
+
ducks.add("Gyro Gearloose");
|
| 89 |
+
|
| 90 |
+
ducks.stream().forEach(duck -> printVowels(duck));
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
public static void printVowels(String string) {
|
| 94 |
+
for (int i=0; i<string.length(); i++) {
|
| 95 |
+
if ("aeiouy".contains("" + string.charAt(i))) {
|
| 96 |
+
System.out.print(string.charAt(i));
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
System.out.println();
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
The program prints:
|
| 104 |
+
ue
|
| 105 |
+
eey
|
| 106 |
+
ouie
|
| 107 |
+
ooe
|
| 108 |
+
yoeaooe
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
========================================================
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
A third example prints the names
|
| 118 |
+
|
| 119 |
+
ArrayList<Person> hlot = new ArrayList<>();
|
| 120 |
+
persons.add(new Person("Heikki Person", "heikki@example.com"));
|
| 121 |
+
persons.add(new Teacher("Oliver Teacher", "oliver@example.com", 123));
|
| 122 |
+
persons.add(new Student("Oscar Student", "oscar@example.com", 23));
|
| 123 |
+
|
| 124 |
+
persons.stream().forEach(person ->
|
| 125 |
+
System.out.println("name: " + person.getName() + ", email: " + person.getEmail()));
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
Ohjelma tulostaa:
|
| 129 |
+
Nimi: Heikki Person, email: heikki@example.com
|
| 130 |
+
Nimi: Oliver Teacher, email: oliver@example.com
|
| 131 |
+
Nimi: Oscar Student, email: oscar@example.com
|
| 132 |
+
|
| 133 |
+
|