Stream流的创建
集合创建stream流
List<String> list = new ArrayList<>();
Stream<String> stream = list.stream();
数组创建stream流
int[] arr = {1,2,3,4,5,6,6};
IntStream intStream = Arrays.stream(arr);
Stream.of创建stream流
Stream<Integer> integerStream = Stream.of(5, 8, 3, 1, 5, 9, 6, 3);
stream流的中间操作
filter
根据传入的Predicate函数式接口过滤元素
int[] arr = {1,2,3,4,5,6,6};
IntStream intStream = Arrays.stream(arr);
intStream.filter(i->i>3).forEach(System.out::println);
map
将Function函数式接口所有的返回值封装成Stream
List<Person> people = new ArrayList<>();
people.add(new Person("alan", 20, 12554));
people.add(new Person("bob", 30, 65422));
people.add(new Person("cici", 25, 6431));
people.add(new Person("david", 69, 64731));
people.add(new Person("ele", 35, 64371));
people.add(new Person("frank", 16, 53535));
people.add(new Person("gg", 29, 3154));
people.add(new Person("hi", 34, 6786));
people.stream().map(Person::getName).forEach(System.out::println);
map的函数式表达式可以返回任意参数,返回值就是map返回的stream的泛型;
people.stream().map(p -> generateChar(p.getName()) ).forEach(System.out::println);
public Stream<Character> generateChar(String str) {
List<Character> list = new ArrayList<>();
for (char c : str.toCharArray()) {
list.add(c);
}
return list.stream();
}
flatmap
flatMap的函数式表达式只能返回stream或者是stream的子类,flatmap会把stream拆散,融合成一个大的stream作为返回值
Stream<Character> stream = people.stream().flatMap(person -> generateChar(person.getName()));
stream.forEach(System.out::println);
limit
限制元素
int[] arr = {1,2,3,4,5,6,6};
IntStream intStream = Arrays.stream(arr);
intStream2.limit(2).forEach(System.out::println);
skip
跳过元素
int[] arr = {1,2,3,4,5,6,6};
IntStream intStream = Arrays.stream(arr);
intStream3.skip(2).forEach(System.out::println);
distinct
去除重复元素
int[] arr = {1,2,3,4,5,6,6};
IntStream intStream = Arrays.stream(arr);
intStream4.distinct().forEach(System.out::println);
stream流的终止操作
归约
将元素根据传入的函数式接口进行归约
List<Person> people = get();
System.out.println(people.stream().map(Person::getAge).reduce(Integer::sum));
排序
int[] arr = {1,2,3,4,5,6,6};
Arrays.stream(arr).sorted().forEach(System.out::println);
List<Person> people = get();
people.stream().sorted(Comparator.comparingInt(Person::getAge)).forEach(System.out::println);
查找和匹配
List<Person> people = get();
// 所有元素都匹配条件
System.out.println(people.stream().allMatch(person -> person.getAge() > 20));
// 只要一个元素匹配条件
System.out.println(people.stream().anyMatch(person -> person.getAge() > 50));
// 没有元素匹配条件
System.out.println(people.stream().noneMatch(person -> person.getSalary() < 1000));
System.out.println(people.stream().findFirst());
// 获取任意一个
System.out.println(people.stream().findAny());
// 过滤后的数量
System.out.println(people.stream().filter(person -> person.getAge()>30).count());
Optional
Optional.of(T t)
创建一个Optional,如果参数为空就抛出异常
String str = "alan";
String nullStr = null;
Optional<Object> o1 = Optional.of(str);
// 抛出异常
Optional<Object> o2 = Optional.of(nullStr);
Optional.OfNullable(T t)
创建一个Optional,参数可以为空
String str = "alan";
String nullStr = null;
Optional<String> op1 = Optional.ofNullable(str);
Optional<String> op2 = Optional.ofNullable(nullStr);
Optional.empty()
创建一个空的Optional
Optional<Object> empty = Optional.empty();
ifPresent()
- 无参,返回一个boolean,Optional的值不为空就返回true,为空返回false
if (op1.isPresent()) {
System.out.println("op1不为null");
} else{
System.out.println("op1为null");
}
if (op2.isPresent()) {
System.out.println("op2不为null");
} else {
System.out.println("op2为null");
}
- ifPresent(Consumer consumer)有参,传入一个Consumer,如果Optional的值不为空就执行Consumer,反之不执行
op1.ifPresent(s-> System.out.println("consumer调用,参数 " + s + " 不为null"));
op2.ifPresent(s-> System.out.println("consumer调用,参数 " + s + " 不为null"));
orElse(T t)
- 如果为空,就返回传入的参数,如果不为空就是用原来的值
System.out.println("op1 " + op1.orElse("是空值"));
System.out.println("op2 " + op2.orElse("是空值"));
orElseGet(Supplier supper)
如果为空,就返回传入的Supplier函数返回值;不为空返回原来的值
System.out.println(op1.orElseGet(() -> "hello world"));
System.out.println(op2.orElseGet(() -> "hello world"));
orElseThrow
如果为空,就抛出异常
// orElseThrow(Supplier<? extends Throw> supplier)
op1.orElseThrow(()-> new RuntimeException("空指针"));
op2.orElseThrow(()-> new RuntimeException("空指针"));