Skip to content

Commit d4a10f0

Browse files
committed
Publish part 10
1 parent 1119bf2 commit d4a10f0

6 files changed

Lines changed: 68 additions & 32 deletions

File tree

course-settings.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const courseSettings = {
2828
{ separator: true, title: "Java Programming I" },
2929
],
3030
sidebarFuturePages: [
31-
{ title: "Part 10", tba: "31.3." },
3231
{ title: "Part 11", tba: "7.4." },
3332
{ title: "Part 12", tba: "21.4." },
3433
{ title: "Part 13", tba: "28.4." },

data/part-10/1-handling-collections-as-streams.md

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
path: '/part-10/1-handling-collections-as-streams'
33
title: 'Handling collections as streams'
4-
hidden: true
4+
hidden: false
55
---
66

77

@@ -812,7 +812,7 @@ Fourth
812812

813813

814814
<!-- ### Välioperaatiot -->
815-
### Imtermediate Operations
815+
### Intermediate Operations
816816

817817
<!-- 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).
818818

@@ -883,47 +883,79 @@ We'll use the `filter` method for filtering through only those persons who were
883883
884884
885885
```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<>();
888888
889-
long lkm = henkilot.stream()
889+
long count = persons.stream()
890890
.filter(henkilo -> henkilo.getSyntymavuosi() < 1970)
891891
.count();
892-
System.out.println("Lukumäärä: " + lkm);
892+
System.out.println("Count: " + count);
893893
```
894894
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"?* -->
896896
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"?*
898898
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`. -->
899900
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
901905
// oletetaan, että käytössämme on lista henkiloita
902906
// ArrayList<Henkilo> henkilot = new ArrayList<>();
903907
904908
long lkm = henkilot.stream()
905909
.filter(henkilo -> henkilo.getEtunimi().startsWith("A"))
906910
.count();
907911
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);
908922
```
909923
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ä.* -->
911925
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*
913927

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. -->
914929

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
916934
// oletetaan, että käytössämme on lista henkiloita
917935
// ArrayList<Henkilo> henkilot = new ArrayList<>();
918936

937+
919938
henkilot.stream()
920939
.map(henkilo -> henkilo.getEtunimi())
921940
.distinct()
922941
.sorted()
923942
.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));
924954
```
925955

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.
927959

928960

929961
<!-- <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
12561288
<!-- 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.
12571289

12581290
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>
12601292
12611293
The example below reads all the lines in "file.txt" and adds them to the list.
12621294
@@ -1309,6 +1341,7 @@ Implement the static method `public static List<String> read(String file)`, whic
13091341
13101342
13111343
<!-- 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+
13121345
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.
13131346

13141347

data/part-10/2-interface-comparable.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
path: '/part-10/2-interface-comparable'
33
title: 'Interface comparable'
4-
hidden: true
4+
hidden: false
55
---
66

77

@@ -10,9 +10,11 @@ hidden: true
1010
<!-- - Tunnet Javan valmiin rajapinnan Comparable ja osaat toteuttaa sen omissa luokissasi.
1111
- Osaat hyödyntää Javan valmiita välineitä sekä listojen järjestämiseen että virran alkioiden järjestämiseen.
1212
- Osaat järjestää listan alkioita useampaa kriteeriä käyttäen (esim. osaat järjestää henkilöt nimen ja iän perusteella). -->
13+
1314
- You're aware of Java's Comparable class and now how to implement it in your own classes
1415
- You know how to use Java's tools for sorting lists and stream elements.
1516
- You know how to sort list elements using multiple criteria (e.g., you know how to sort a person based on name and age).
17+
1618
</text-box>
1719

1820

@@ -21,7 +23,7 @@ hidden: true
2123
Comparable-rajapinnan vaatima compareTo-metodi saa parametrinaan olion, johon "this"-oliota verrataan. Mikäli olio on vertailujärjestyksessä ennen parametrina saatavaa olioa, tulee metodin palauttaa negatiivinen luku. Mikäli taas olio on järjestyksessä parametrina saatavan olion jälkeen, tulee metodin palauttaa positiivinen luku. Muulloin palautetaan luku 0. Tätä `compareTo`-metodin avulla johdettua järjestystä kutsutaan *luonnolliseksi järjestykseksi* (natural ordering).
2224
2325
Tarkastellaan tätä kerhossa käyvää lasta tai nuorta kuvaavan luokan Kerholainen avulla. Jokaisella kerholaisella on nimi ja pituus. Kerholaisten tulee mennä syömään pituusjärjestyksessä, joten toteutetaan kerholaisille rajapinta `Comparable`. Comparable-rajapinta ottaa tyyppiparametrinaan luokan, johon vertaus tehdään. Käytetään tyyppiparametrina samaa luokkaa `Kerholainen`. -->
24-
In the previous section, we looked at interfaces in more general terms - let's now familiarize oruselves with one of Java's ready interfaces. The <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html"> Comparable </a> interface defines the `compareTo` method used to compare objects. If a class implements the Comparable interface, objects created from that class can be sorted using Java's sorting algorithms.
26+
<p>In the previous section, we looked at interfaces in more general terms - let's now familiarize oruselves with one of Java's ready interfaces. The <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html"> Comparable </a> interface defines the `compareTo` method used to compare objects. If a class implements the Comparable interface, objects created from that class can be sorted using Java's sorting algorithms.</p>
2527

2628
The compareTo method required by the Comparable interface gets as its parameter the object to which the "this" object is compared. If the "this" object comes before the object received as a parameter in terms of sorting order, the method should return a negative number. If, on the other hand, id "this" object comes after the object received as a parameter, the method should return a positive number. Otherwise, 0 is returned. The sorting resulting from the `compareTo` method is called *natural ordering*.
2729

@@ -295,8 +297,8 @@ public class Person implements Identifiable, Comparable<Person> {
295297
}
296298

297299
@Override
298-
public int compareTo(Person toinen) {
299-
return this.getId().compareTo(toinen.getId());
300+
public int compareTo(Person another) {
301+
return this.getId().compareTo(another.getId());
300302
}
301303
}
302304
```
@@ -378,7 +380,7 @@ Sekä luokan `Collections` metodille `sort` että virran metodille `sorted` void
378380
379381
We want to sort the list without having to implement the `Comparable` interface.
380382
381-
Both the `sort` method of `Collections` class and the stream's `sorted` method accept a lambda expression as a parameter that defines the sorting criteria. More specifically, both methods can be provided with an object that implements the <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank"> Comparator </a> interface, which defines the desired order - the lambda expression is used to create this object.
383+
<p>Both the `sort` method of `Collections` class and the stream's `sorted` method accept a lambda expression as a parameter that defines the sorting criteria. More specifically, both methods can be provided with an object that implements the <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank"> Comparator </a> interface, which defines the desired order - the lambda expression is used to create this object.</p>
382384

383385
<!-- ```java
384386
ArrayList<Henkilo> henkilot = new ArrayList<>();
@@ -551,7 +553,7 @@ pieces[1] = pieces[1].trim();
551553
<!--
552554
Joskus haluamme järjestää esineitä useamman asian perusteella. Tarkastellaan seuraavaksi esimerkkiä, missä elokuvat listataan nimen ja julkaisuvuoden perusteella järjestettynä. Tässä käytämme Javan valmista <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank" norel>Comparator</a>-luokkaa, joka tarjoaa menetelmiä järjestämiseen. Oletetaan, että käytössämme on seuraava luokka `Elokuva` -->
553555

554-
We sometimes want to sort items based on a number of things. Let's look at an example in which films are listed in order of their name and year of release. We'll make use of Java's <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank" norel>Comparator</a> class here, which offers us the functionality required for sorting. Le'ts assume that we have the clas `Film` at our disposal.
556+
<p>We sometimes want to sort items based on a number of things. Let's look at an example in which films are listed in order of their name and year of release. We'll make use of Java's <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank" norel>Comparator</a> class here, which offers us the functionality required for sorting. Le'ts assume that we have the clas `Film` at our disposal.</p>
555557

556558
<!--
557559
```java

data/part-10/3-other-useful-techniques.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
path: '/part-10/3-other-useful-techniques'
33
title: 'Other useful techniques'
4-
hidden: true
4+
hidden: false
55
---
66

77

@@ -863,7 +863,8 @@ is not a spade
863863

864864
<!-- Huomaamme, että enumin tunnukset tulostuvat mukavasti! Oraclella on `enum`-tyyppiin liittyvä sivusto osoitteessa <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html" target="_blank" rel="noopener">http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html</a>.
865865
-->
866-
We see that the Enum values are outputted nicely! Oracle has a site related to the `enum` data type at <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html" target="_blank" rel="noopener"> http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html </a>.
866+
867+
<p>We see that the Enum values are outputted nicely! Oracle has a site related to the `enum` data type at <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html" target="_blank" rel="noopener"> http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html </a>.</p>
867868

868869

869870
<!-- <text-box variant='hint' name='Enumien vertailu'> -->
@@ -882,8 +883,8 @@ The numeric identifier of an enum field value can be found with `ordinal()`. The
882883

883884

884885
```java
885-
public enum Maa {
886-
RUUTU, PATA, RISTI, HERTTA
886+
public enum Suits {
887+
DIAMOND, CLUB, HEART, SPADE
887888
}
888889
```
889890

@@ -893,7 +894,7 @@ System.out.println(Maa.HERTTA.ordinal());
893894
``` -->
894895
```java
895896
System.out.println(Suit.DIAMOND.ordinal());
896-
System.out.println(Suit.HEARTS.ordinal());
897+
System.out.println(Suit.HEART.ordinal());
897898
```
898899

899900
<sample-output>

data/part-10/4-summary.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
path: '/part-10/4-summary'
33
title: 'Summary'
4-
hidden: true
4+
hidden: false
55
---
66

77
<!-- TODO: kerrotaan tiedon käsittelystä virtana ja mainitaan lyhyesti funktionaalinen ohjelmointi; puhutaan tiedon järjestämisestä ja järjestyksen täyrkeydestä. Kerrataan lyhyesti StringBuilder ja iteraattori. -->
88

99
Please take a moment to answer the quiz! Thank you!
1010

11-
<quiz id="f3f3b8b9-4725-5c89-9c52-a6e1ae289d81"></quiz>
11+
<quiz id="f3f3b8b9-4725-5c89-9c52-a6e1ae289d81"></quiz>

data/part-10/index.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
path: '/part-10'
33
title: 'Part 10'
44
overview: true
5-
hidden: true
5+
hidden: false
66
---
77

88
<!-- Kurssimateriaalin kymmenennessä osassa tutustutaan tietokokoelmien käsittelyyn virtojen avulla. Opit luomaan virran tietokokoelmasta, rajaamaan virran arvoja, muuntamaan virran arvoja muodosta toiseen, ja keräämään virran arvoja toiseen tietokokoelmaan. Tutustut käsitteeseen lambdalauseke ja opit käyttämään sitä ohjemissasi. Opit myös järjestämään olioita Javan valmista Comparable-rajapintaa hyödyntäen ja tutustut muutamaan yleishyödylliseen työvälineeseen kuten säännöllinen lauseke, lueteltu tyyppi, ja iteraattori. -->
99
In the tenth part of the course we introduce handling collections with streams. You'll learn how to create a stream from a collection, filter the values of a stream, transform the values of a stream, and collect values of a stream to another collection. We introduce the concept lambda expression, and you'll learn to use it in your programs. You will also learn how to order objects using the Java Comparable interface, and some other useful techniques like regular expressions, enumerate type and iterator.
1010

11-
Only the exercises for this part are currently available in English!
12-
1311
<please-login></please-login>
1412

1513
<pages-in-this-section></pages-in-this-section>
1614

17-
Yllä oleva sisällysluettelo sisältää kurssin kymmenennen osan aihealueet. Kukin kurssin osa on suunniteltu siten, että siinä on työtä yhden viikon ajaksi. Kuhunkin kurssin osaan on hyvä varata reilusti yli kymmenen tuntia aikaa, riippuen aiemmasta tietokoneen käyttökokemuksesta. Ohjelmointia aiemmin kokeilleet saattavat edetä materiaalissa aluksi nopeamminkin.
15+
<!-- Yllä oleva sisällysluettelo sisältää kurssin kymmenennen osan aihealueet. Kukin kurssin osa on suunniteltu siten, että siinä on työtä yhden viikon ajaksi. Kuhunkin kurssin osaan on hyvä varata reilusti yli kymmenen tuntia aikaa, riippuen aiemmasta tietokoneen käyttökokemuksesta. Ohjelmointia aiemmin kokeilleet saattavat edetä materiaalissa aluksi nopeamminkin. -->
16+
17+
The table of contents above lists the topics of the tenth part of the course. The tenth part has been designed to cover the tenth week of the course. You should reserve well above 10 hours for each part of the course, depending on previous experience with computers. If you've tried programming before, you might advance faster in the beginning.
18+
1819

1920
<exercises-in-this-section></exercises-in-this-section>

0 commit comments

Comments
 (0)