|
1 | 1 | --- |
2 | 2 | path: '/part-10/1-handling-collections-as-streams' |
3 | 3 | title: 'Handling collections as streams' |
4 | | -hidden: true |
| 4 | +hidden: false |
5 | 5 | --- |
6 | 6 |
|
7 | 7 |
|
@@ -812,7 +812,7 @@ Fourth |
812 | 812 |
|
813 | 813 |
|
814 | 814 | <!-- ### Välioperaatiot --> |
815 | | -### Imtermediate Operations |
| 815 | +### Intermediate Operations |
816 | 816 |
|
817 | 817 | <!-- Virran välioperaatiot ovat metodeja, jotka palauttavat arvonaan virran. Koska palautettava arvo on virta, voidaan välioperaatioita kutsua peräkkäin. Tyypillisiä välioperaatioita ovat arvon muuntaminen muodosta toiseen `map` sekä sen erityistapaus `mapToInt` eli virran muuntaminen kokonaislukuvirraksi, arvojen rajaaminen `filter`, uniikkien arvojen tunnistaminen `distinct` sekä arvojen järjestäminen `sorted` (mikäli mahdollista). |
818 | 818 |
|
@@ -883,47 +883,79 @@ We'll use the `filter` method for filtering through only those persons who were |
883 | 883 |
|
884 | 884 |
|
885 | 885 | ```java |
886 | | -// oletetaan, että käytössämme on lista henkiloita |
887 | | -// ArrayList<Henkilo> henkilot = new ArrayList<>(); |
| 886 | +// suppose we have a list of persons |
| 887 | +// ArrayList<Person> persons = new ArrayList<>(); |
888 | 888 |
|
889 | | -long lkm = henkilot.stream() |
| 889 | +long count = persons.stream() |
890 | 890 | .filter(henkilo -> henkilo.getSyntymavuosi() < 1970) |
891 | 891 | .count(); |
892 | | -System.out.println("Lukumäärä: " + lkm); |
| 892 | +System.out.println("Count: " + count); |
893 | 893 | ``` |
894 | 894 |
|
895 | | -*Ongelma 2: Saat käyttöösi listan henkilöitä. Kuinka monen henkilön etunimi alkaa kirjaimella "A"?* |
| 895 | +<!-- *Ongelma 2: Saat käyttöösi listan henkilöitä. Kuinka monen henkilön etunimi alkaa kirjaimella "A"?* --> |
896 | 896 |
|
897 | | -Käytetään `filter`-metodia henkilöiden rajaamiseen niihin, joiden etunimi alkaa kirjaimella "A". Lasketaan tämän jälkeen henkilöiden lukumäärä metodilla `count`. |
| 897 | +*Problem 2: You'll receive a list of persons. How many person's first name starts with the letter "A"?* |
898 | 898 |
|
| 899 | +<!-- Käytetään `filter`-metodia henkilöiden rajaamiseen niihin, joiden etunimi alkaa kirjaimella "A". Lasketaan tämän jälkeen henkilöiden lukumäärä metodilla `count`. --> |
899 | 900 |
|
900 | | -```java |
| 901 | +Let's use the `filter`-method to narrow down the persons to those whose first name starts with the letter "A". Afterwards, we'll calculte the number of persons with the `count`-method. |
| 902 | +
|
| 903 | +
|
| 904 | +<!-- ```java |
901 | 905 | // oletetaan, että käytössämme on lista henkiloita |
902 | 906 | // ArrayList<Henkilo> henkilot = new ArrayList<>(); |
903 | 907 |
|
904 | 908 | long lkm = henkilot.stream() |
905 | 909 | .filter(henkilo -> henkilo.getEtunimi().startsWith("A")) |
906 | 910 | .count(); |
907 | 911 | System.out.println("Lukumäärä: " + lkm); |
| 912 | +``` --> |
| 913 | +
|
| 914 | +```java |
| 915 | +// suppose we have a list of persons |
| 916 | +// ArrayList<Person> persons = new ArrayList<>(); |
| 917 | +
|
| 918 | +long count = persons.stream() |
| 919 | + .filter(person -> persons.getFistName().startsWith("A")) |
| 920 | + .count(); |
| 921 | +System.out.println("Count: " + count); |
908 | 922 | ``` |
909 | 923 |
|
910 | | -*Ongelma 3: Saat käyttöösi listan henkilöitä. Tulosta henkilöiden uniikit etunimet aakkosjärjestyksessä.* |
| 924 | +<!-- *Ongelma 3: Saat käyttöösi listan henkilöitä. Tulosta henkilöiden uniikit etunimet aakkosjärjestyksessä.* --> |
911 | 925 |
|
912 | | -Käytetään ensin `map`-metodia, jonka avulla henkilö-olioita sisältävä virta muunnetaan etunimiä sisältäväksi virraksi. Tämän jälkeen kutsutaan metodia `distinct`, joka palauttaa virran, jossa on uniikit arvot. Seuraavaksi kutsutaan metodia `sorted`, joka järjestää merkkijonot. Lopulta kutsutaan metodia `forEach`, jonka avulla tulostetaan merkkijonot. |
| 926 | +*Problem 3: You'll receive a list of persons. Print the number of unique first names in alphabetical order* |
913 | 927 |
|
| 928 | +<!-- Käytetään ensin `map`-metodia, jonka avulla henkilö-olioita sisältävä virta muunnetaan etunimiä sisältäväksi virraksi. Tämän jälkeen kutsutaan metodia `distinct`, joka palauttaa virran, jossa on uniikit arvot. Seuraavaksi kutsutaan metodia `sorted`, joka järjestää merkkijonot. Lopulta kutsutaan metodia `forEach`, jonka avulla tulostetaan merkkijonot. --> |
914 | 929 |
|
915 | | -```java |
| 930 | +First we'll use the `map` method to change a stream containing person objects into a stream containing first names. After that we'll call the `distinct`-method, that returns a stream that only contains unique values. Next, we call the method `sorted`, which sorts the strings. Finally, we call the method `forEach`, that is used to print the strings. |
| 931 | + |
| 932 | + |
| 933 | +<!-- ```java |
916 | 934 | // oletetaan, että käytössämme on lista henkiloita |
917 | 935 | // ArrayList<Henkilo> henkilot = new ArrayList<>(); |
918 | 936 |
|
| 937 | + |
919 | 938 | henkilot.stream() |
920 | 939 | .map(henkilo -> henkilo.getEtunimi()) |
921 | 940 | .distinct() |
922 | 941 | .sorted() |
923 | 942 | .forEach(nimi -> System.out.println(nimi)); |
| 943 | +``` --> |
| 944 | + |
| 945 | +```java |
| 946 | +// suppose we have a list of persons |
| 947 | +// ArrayList<Person> persons = new ArrayList<>(); |
| 948 | + |
| 949 | +persons.stream() |
| 950 | + .map(person -> person.getFirstName()) |
| 951 | + .distinct() |
| 952 | + .sorted() |
| 953 | + .forEach(name -> System.out.println(name)); |
924 | 954 | ``` |
925 | 955 |
|
926 | | -Yllä kuvattu `distinct`-metodi hyödyntää olioiden `equals`-metodia yhtäsuuruuden tarkasteluun. Metodi `sorted` taas osaa järjestää olioita, joilla on tieto siitä, miten olio tulee järjestää -- näitä ovat esimerkiksi luvut ja merkkijonot. |
| 956 | +<!-- Yllä kuvattu `distinct`-metodi hyödyntää olioiden `equals`-metodia yhtäsuuruuden tarkasteluun. Metodi `sorted` taas osaa järjestää olioita, joilla on tieto siitä, miten olio tulee järjestää -- näitä ovat esimerkiksi luvut ja merkkijonot. --> |
| 957 | + |
| 958 | +The `distinct`-method described above uses the `equals`-method that is in all objects for comparing whether two strings are the same. The `sorted`-method on the other hand is able to sort objects that contain some kind of order -- examples of this kind of objects are for example numbers and strings. |
927 | 959 |
|
928 | 960 |
|
929 | 961 | <!-- <programming-exercise name='Luettujen arvojen tulostaminen' tmcname='part10-Part10_05.LuettujenArvojenTulostaminen'> --> |
@@ -1256,7 +1288,7 @@ The exercise template includes the probably familiar-y project "Cargo hold". How |
1256 | 1288 | <!-- Virta on myös erittäin näppärä tiedostojen käsittelyssä. Tiedoston lukeminen virtamuotoisena tapahtuu Javan valmiin <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html" target="_blank" rel="noopener">Files</a>-luokan avulla. Files-luokan metodin `lines` avulla tiedostosta voidaan luoda syötevirta, jonka avulla tiedoston rivit voidaan käsitellä yksi kerrallaan. Metodi `lines` saa patametrikseen polun, joka luodaan luokan <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/file/Paths.html" target="_blank" rel="noopener">Paths</a> tarjoamalla metodilla `get`, jolle annetaan parametrina tiedostopolkua kuvaava merkkijono. |
1257 | 1289 |
|
1258 | 1290 | Alla olevassa esimerkissä luetaan tiedoston "tiedosto.txt" kaikki rivit ja lisätään ne listaan. --> |
1259 | | -Streams are also very handy in handling files. The file is read in stream form using Java's ready-made <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html" target="_blank" rel="noopener">Files</a> class. The `lines` method in the files class allows you to create an input stream from a file, allowing you to process the rows one by one. The `lines` method gets a path as its parameter, which is created using the `get` method the <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/file/Paths.html" target="_blank" rel="noopener">Paths</a> class. The `get` method is provided a string describing the file path. |
| 1291 | +<p>Streams are also very handy in handling files. The file is read in stream form using Java's ready-made <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html" target="_blank" rel="noopener">Files</a> class. The `lines` method in the files class allows you to create an input stream from a file, allowing you to process the rows one by one. The `lines` method gets a path as its parameter, which is created using the `get` method the <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/file/Paths.html" target="_blank" rel="noopener">Paths</a> class. The `get` method is provided a string describing the file path.</p> |
1260 | 1292 |
|
1261 | 1293 | The example below reads all the lines in "file.txt" and adds them to the list. |
1262 | 1294 |
|
@@ -1309,6 +1341,7 @@ Implement the static method `public static List<String> read(String file)`, whic |
1309 | 1341 |
|
1310 | 1342 |
|
1311 | 1343 | <!-- Virran metodit tekevät määritellyn muotoisten tiedostojen lukemisesta melko suoraviivaista. Tarkastellaan tilannetta, missä tiedosto sisältää henkilöiden tietoja. Kukin henkilö on omalla rivillään, ensin tulee henkilön nimi, sitten puolipiste, sitten henkilön syntymävuosi. Tiedoston muoto on seuraava. --> |
| 1344 | +
|
1312 | 1345 | Stream methods make the reading of files that are of predefined format relatively straightforward. Let's look at a scenario where a file contains some personal information. Details of each person is on their own line, first the person's name, then the semicolon, then the person's Year of Birth. The file format is as follows. |
1313 | 1346 |
|
1314 | 1347 |
|
|
0 commit comments