## 1.檢查

`boolean anyMatch(rule)`方法

``````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``````

``````Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
boolean result = stream.allMatch(x -> x > 0);``````
``true``
（所有大於零的元素）
``````Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.allMatch(x -> x > 0);``````
``false``
（是否有小於或等於零的元素？）
``````Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.filter(x -> x < 0).allMatch(x -> x < 0);``````
``true``
（我們保留了小於零的元素）

boolean noneMatch(rule) 方法

`noneMatch()`方法檢查流是否沒有與傳遞的規則匹配的元素。這就像方法的反面`anyMatch()`

``````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``````

4

Java Core,  等級 6課堂 5

join: at the right time in the right place
The join method is very useful and is often used to terminate a thread. Think about where and on what object you need to call this method to have the result displayed
18

Java Core,  等級 6課堂 5

Horse racing
Programming is better than gambling... Still, horse races and other races perfectly illustrate multithreading! Figure out what the code does, and implement a method that counts the number of horses that have crossed the finish line. One more thing: you need to wait for the longshots to finish the race.

## 2.實用類：`Optional`class

``````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;
}
}``````

``````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)
}``````

9

Java Core,  等級 6課堂 5

Promotion during political debates
Mr Chump is our candidate! And we'll help him a bit by making him give a speech, and then another, and then another during a political debate. We'll help him a little by making him talk until all the available time has been taken. Threads come to our rescue!

## 3.尋找元素

`Optional<T> findFirst()`方法

`findFirst()`方法只返回流中的第一個元素。這就是它所做的一切。

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

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

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

`Optional<T> findAny()`方法

`findAny()`方法返回流中的任何元素並在那裡結束。此方法類似於`findFirst()`，但它非常適用於並行操作中使用的流。

`Optional<T> min(Comparator<T>)`方法

`min()`方法使用一個`comparator`對象來比較流中的所有元素並返回最小的元素。定義比較器對象最方便的方法是使用 lambda 函數。

``````ArrayList<String> list = new ArrayList<String>();
String min = list.stream().min( (s1, s2)-> s1.length()-s2.length() ).get();``````

`Optional<T> max(Comparator<T>)`方法

`max()`方法使用一個`comparator`對象來比較流中的所有元素並返回最大元素。定義比較器對象最方便的方法是使用 lambda 函數。

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