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

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

## 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>();
`Optional<T> max(Comparator<T>)`方法
`max()`方法使用一个`comparator`对象来比较流中的所有元素并返回最大元素。定义比较器对象最方便的方法是使用 lambda 函数。
``````ArrayList<String> list = new ArrayList<String>();