## 1. Priksa

Aku mikir sampeyan bisa uga wis bosen sinau carane chain bebarengan aliran data. Sampeyan pengin pungkasanipun nindakake soko karo data.

Kelas `Stream`wis telung cara standar sing ora mbangun lepen, nanging mriksa apa jenis data ing wong. Cara kasebut yaiku: `anyMatch()`, `allMatch()`, lan `noneMatch()`.

`boolean anyMatch(rule)`cara

Cara iki mriksa apa stream wis paling siji unsur sing gawe marem aturan sing liwati kanggo cara. Yen ana unsur kasebut, metode kasebut bali `true`, yen ora `false`.

Kode Cathetan
``````Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
boolean result = stream.anyMatch(x -> x > 0);``````
``````
true``````
``````Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.anyMatch(x -> x > 0);``````
``````
true``````
``````Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.filter(x -> x < 0).anyMatch(x -> x > 0);``````
``````
false``````

Ing conto pungkasan, kita mung nahan unsur sing kurang saka nol, banjur mriksa asil kanggo ndeleng apa ana unsur sing disaring luwih gedhe tinimbang nol. Mesthine, unsur kasebut ora ana maneh.

metode boolean allMatch(rule).

Cara iki mriksa apa kabeh unsur ing stream cocog karo aturan (uga dikenal minangka predikat). Aturan kasebut diterusake minangka argumen kanggo metode:

Kode Cathetan
``````Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
boolean result = stream.allMatch(x -> x > 0);``````
``true``
(kabeh unsur luwih saka nol)
``````Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.allMatch(x -> x > 0);``````
``false``
(Apa ana unsur sing kurang saka utawa padha karo nol?)
``````Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.filter(x -> x < 0).allMatch(x -> x < 0);``````
``true``
(kita nahan unsur sing kurang saka nol)

Ing conto pungkasan, pisanan kita mung ngidini unsur sing kurang saka nul kanggo nembus Filter, banjur kita mriksa apa kabeh unsur disimpen kurang saka nul. Priksa ngasilake asil positif.

metode boolean noneMatch(rule).

Cara `noneMatch()`mriksa apa stream ora ana unsur sing cocog karo aturan liwati. Iku kaya ngelawan saka cara `anyMatch()`.

Kode Cathetan
``````Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
boolean result = stream.noneMatch(x -> x > 0);``````
``````
false``````
``````Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.noneMatch(x -> x > 0);``````
``````
false``````
``````Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.filter(x -> x < 0).noneMatch(x -> x > 0);``````
``````
true``````

## 2. Kelas Utilitas: `Optional`kelas

Kadhangkala ora trep kanggo programer nggarap `null`referensi. Contone, umpamane sampeyan mbandhingake rong senar. Yen loro variabel ora `null`, sampeyan mung bisa nelpon `s1.equals(s2)`, lan kabeh bakal bisa. Nanging yen `s1`bisa `null`, sampeyan kudu nulis kode sing nangani kahanan iki supaya ora a `NullPointerException`.

Mulane programer teka karo `Optional<T>`kelas sarana. Kode kasebut katon kaya iki:

Kode Cathetan
``````class Optional<Type>
{
private final Type value;
private Optional() { this.value = null;}
private Optional(value) { this.value = value;}
public static <Type> Optional<Type> of(Type value)
{
return new Optional<Type>(value);
}

public boolean isPresent()
{
return value != null;
}

public boolean isEmpty()
{
return value == null;
}

public Type get()
{
if (value == null)
{
throw new NoSuchElementException();
}
return value;
}

public Type orElse(Type other)
{
return value != null ? value : other;
}

public Type orElseThrow()
{
if (value == null)
{
throw new NoSuchElementException();
}
return value;
}
}``````

Priksa manawa regane ora `null`

Priksa manawa nilai kasebut `null`

Ngasilake nilai sing disimpen. Mbuwang pangecualian yen nilai null.

Ngasilake nilai non-null sing disimpen. Utawa yen nilai sing disimpen `null`, banjur ngasilake nilai sing dilewati minangka argumen metode

Ngasilake nilai non-null sing disimpen utawa mbuwang pengecualian yen nilai kasebut null.

Tujuan saka kelas iki mung kanggo nyimpen obyek T (referensi kanggo obyek kang jinis T). Referensi obyek ing njero `Optional<T>`obyek bisa `null`.

Kelas iki ngidini programer nulis kode sing luwih apik. Ayo mbandhingake:

Nggunakake Opsional Ora nggunakake Opsional
``````public void printString(String s)
{
Optional<String> str = Optional.ofNullable(s);
System.out.println(str.orElse(""));
}``````
``````public void printString(String s)
{
String str = s != null ? s : "";
System.out.println(str)
}``````

Siji `Optional`obyek tansah bisa dibandhingake karo `Optional`obyek liyane nggunakake `equals`cara, sanajan padha nyimpen `null`referensi.

Cukup ngomong, `Optional`kelas ngijini sampeyan nulis "ayu" mriksa `null`lan "ayu" tumindak ing acara sing `Optional`obyek nyimpen `null`nilai.

## 3. Nemokake unsur

Ayo bali menyang `Stream`kelas. Kelas kasebut `Stream`duwe 4 cara liyane sing ngidini sampeyan nggoleki unsur ing aliran. Cara kasebut yaiku `findFirst()`, `findAny()`, `min()`lan `max()`.

`Optional<T> findFirst()`cara

Cara kasebut `findFirst()`mung ngasilake unsur pisanan ing aliran kasebut. Sing kabeh iku ora.

Sing luwih menarik kanggo dicathet ing kene yaiku metode kasebut ora ngasilake obyek `T`, nanging `Optional<T>`obyek bungkus. Iki mesthekake yen cara ora bakal bali `null`sawise gagal nemokake obyek.

``````ArrayList<String> list = new ArrayList<String>();
String str = list.stream().findFirst().get(); // Hello``````

Kanggo luwih njlentrehake, ayo dipecah baris pungkasan dadi sawetara baris:

``````ArrayList<String> list = new ArrayList<String>();

Stream<String> stream = list.stream();
Optional<String> result = stream.findFirst();
String str = result.get(); // Hello``````

Cara pungkasan `get()`mung njupuk nilai sing disimpen ing `Optional`obyek kasebut.

`Optional<T> findAny()`cara

Cara kasebut `findAny()`ngasilake unsur apa wae saka stream lan rampung ing kana. Cara iki padha karo `findFirst()`, nanging apik kanggo stream digunakake ing operasi podo.

Nalika ngolah aliran kanthi podo karo, bisa uga ana unsur sing wis ditemokake ing sawetara bagean saka aliran, nanging durung jelas apa sing pisanan utawa ora.

Yen akeh unsur wis cocog kabeh saringan, lan iku penting kanggo programmer kanggo njaluk persis pisanan saka wong-wong mau, banjur cara `findFirst()`apa kudu disebut. Yen programmer ngerti manawa 0 utawa 1 unsur bakal cocog karo kabeh saringan, mula cukup nelpon `findAny()`- lan iki bakal luwih cepet.

`Optional<T> min(Comparator<T>)`cara

Cara iki `min()`nggunakake `comparator`obyek kanggo mbandhingake kabeh unsur ing stream lan ngasilake unsur minimal. Cara sing paling trep kanggo nemtokake obyek komparator yaiku kanthi fungsi lambda.

Conto nggoleki string paling cendhak:

``````ArrayList<String> list = new ArrayList<String>();
`Optional<T> max(Comparator<T>)`cara
Cara `max()`nggunakake `comparator`obyek kanggo mbandhingaké kabeh unsur ing stream lan ngasilake unsur maksimum. Cara sing paling trep kanggo nemtokake obyek komparator yaiku kanthi fungsi lambda.
``````ArrayList<String> list = new ArrayList<String>();