lambda-Stream

Stream 是一个支持并行得对元素进行聚合排序操作得流。

Stream< T> filter(Predicate<? super T> predicate); 接受一个Predicate 参数 对流进行筛选。
例:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9,10);
  Predicate<Integer> predicate = a->a%2==0;
  stream.filter(predicate).forEach(a->System.out.println(a));

结果:
在这里插入图片描述

< R> Stream< R> map(Function<? super T, ? extends R> mapper); 接受一个Function参数,通过给定元素返回一个流
例:

  Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9,10);
  Function<Integer,String> function = a->a+"-"+a;
  stream.map(function).forEach(a->System.out.println(a));

结果:
在这里插入图片描述

IntStream mapToInt(ToIntFunction<? super T> mapper); 接受一个ToIntFunction 参数 通过给定元素返回一个IntStream
例:

Stream<Integer> stream=Stream.of(1,9 2,3,4,5,6,7,8,9,10);
ToIntFunction<Integer> function = a->a*10;
stream.mapToInt(function).forEach(a->System.out.println(a));

结果:
在这里插入图片描述

LongStream mapToLong(ToLongFunction<? super T> mapper); 和mapInt 类似
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper); 和mapInt 类似
< R> Stream< R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper); 接受一个返回流得Function参数 将返回得流组成一个新的流
例:

    Stream<Integer> streamInt = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9,10);
    Stream<String> streamString = Stream.of("11", "12", "13", "14", "15", "16", "17", "18", "19","20");
//    将两个流作为一个流得两个元素。
//    flatMap最重要得是参数Function返回得是流,stream是什么类型不重要
    Stream.of(streamInt,streamString).flatMap(stream -> stream.map(a->String.valueOf(a))).forEach(a->System.out.println(a)); 

结果:
在这里插入图片描述

IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper); 只是要求作为参数得Function需要返回IntStream 合并得结果也是IntStream
LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper); 同理
DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper); 同理

Stream< T> distinct(); 返回一个由不同元素组成得Stream(通过equals判断是否相等)
例:

  Stream<Integer> stream = Stream.of(1, 1, 1, 1, 2, 2, 2, 3, 3, 3);
  stream.distinct().forEach(a->System.out.println(a));

结果:
在这里插入图片描述

Stream< T> sorted(); 返回一个有序得流(元素必须实现comparable)
例:

public static void main(String arg[]){
 Stream<Person> stream = Stream.of(
            new Person("person1",1),
            new Person("person2",3),
            new Person("person3",2));
    stream.sorted().forEach(a->System.out.println(a));
}

class Person implements Comparable<Person> {

  String name;

  int age;

  @Override
  public int compareTo(Person o) {
    return this.age-o.getAge();
  }

  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }
  @Override
  public String toString() {
    return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
  }
}

Stream< T> sorted(Comparator<? super T> comparator); 和sorted一样,但是提供了一个comparator参数用于自定义排序
例:

  public static void main(String[] argv) {
    Stream<Person> stream = Stream.of(
            new Person("person1",1),
            new Person("person2",3),
            new Person("person3",2));
    stream.sorted((o1,o2)->o1.getAge()-o2.getAge()).forEach(a->System.out.println(a));
  }
}


class Person {

  String name;

  int age;

  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }
  @Override
  public String toString() {
    return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
  }
}

Stream< T> peek(Consumer<? super T> action); 当流中得元素被使用时执行Comsumer操作。
例:

public static void main(String[] argv) {
  Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10);
  stream.filter(i->i%2==0)
          .peek(i->System.out.println("被过滤后得数据:"+i))
          .map(i->String.valueOf(i)).peek(i->System.out.println("被转型后得数据:"+i))
          .forEach(i->System.out.println("数据:"+i));
}
public static void main(String[] argv) {
  Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10);
  stream.filter(i->i%2==0)
          .peek(i->System.out.println("被过滤后得数据:"+i))
          .map(i->String.valueOf(i)).peek(i->System.out.println("被转型后得数据:"+i))
          //如果没有这一步forEach,peek不会执行,因为元素没有被使用。如果元素没有被使用map和filter都不会执行
          .forEach(i->System.out.println("数据:"+i));
}

Stream< T> limit(long maxSize);
例:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
stream.limit(5).forEach(i -> System.out.println(i));

结果:
在这里插入图片描述
Stream< T> skip(long n);
例:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
stream.skip(5).forEach(i -> System.out.println(i));

结果:
在这里插入图片描述

void forEach(Consumer<? super T> action); 对元素进行操作

void forEachOrdered(Consumer<? super T> action); 按顺序对元素进行操作素进行

Object[] toArray(); 将元素流转化为数组

< A> A[] toArray(IntFunction<A[]> generator); 将元素流转化为给定类型数组

Stream<Person> stream = Stream.of(
        new Person("person1",1),
        new Person("person2",3),
        new Person("person3",2));
Person[] objects = stream.toArray(Person[]::new);

T reduce(T identity, BinaryOperator accumulator); 累计函数

Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10);
//其中identity时累计得初始值
Integer sum = stream.reduce(5,(a, b) -> a + b);

Optional< T> reduce(BinaryOperator accumulator); 累计函数,返回一个可能包含累计值得Optional

Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10);
Integer sum = stream.reduce((a, b) -> a + b).get();

< U> U reduce(U identity,BiFunction<U, ? super T, U> accumulator,BinaryOperator combiner);
和第一个累计函数相同,第三个函数是用于柱状多线程流得计算结果。平时可以忽略((a,b)-> null)

< R> R collect(Supplier supplier,BiConsumer<R, ? super T> accumulator,BiConsumer<R, R> combiner);
例:

1
Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
//第一个参数:用于在并行得时候创建一个容器。每次都会产生一条新数据。
//第二个参数:用于描述合并元素到结果得行为
//第三个参数:用于合并并行残生得多个结果
ArrayList<Integer> collect = stream.parallel().collect(ArrayList<Integer>::new, (list, a) -> list.add(a), (list1,list2)->list1.addAll(list2))));
System.out.println(collect);
2
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
ArrayList<Integer> collect = stream.parallel().collect(() -> {
            System.out.println("创建用户结果容器");
            return new ArrayList<Integer>();
        },
        (list, a) -> {
          System.out.println("开始装入元素");
            list.add(a);
        },
        (list1, list2) -> {
            System.out.println("合并最终结果");
            list1.addAll(list2);
        });
System.out.println(collect);

< R, A> R collect(Collector<? super T, A, R> collector);
例:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Integer> ins = stream.collect(Collectors.toList());

Optional< T> min(Comparator<? super T> comparator);
通过提供一个comparator方法获取最小值
Optional< T> max(Comparator<? super T> comparator);
通过提供一个comparator方法获取最大值
long count();
获取流得元素数量
boolean anyMatch(Predicate<? super T> predicate);
比较流中是否有满足条件得元素
例:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
System.out.println(stream.anyMatch(a->a==1));
//true

boolean allMatch(Predicate<? super T> predicate);
比较流中是否所有元素都满足条件
例:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
System.out.println(stream.allMatch(a->a>0));
//true

boolean noneMatch(Predicate<? super T> predicate);
流中元素是否都不满足条件
Optional findFirst();
查找流中第一条数据
Optional findAny();
查找流中任一一条数据

static< T> Builder< T> builder()
返回一个流得Builder对象
static< T> Stream< T> empty()
//返回一个空得流
static< T> Stream< T> of(T t)
//返回单个元素得有序流
static< T> Stream< T> of(T… values)
//返回多个元素得有序流
static< T> Stream< T> iterate(final T seed, final UnaryOperator< T> f)
返回一个无限长度得流。
例:

//第一个参数是初始值,第二个参数是依据上一个参数生成下一个参数得函数
Stream<Integer> integerStream = Stream.iterate(1, a -> {
  try {
    Thread.sleep(1000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  return ++a;});
integerStream.forEach(a->System.out.println(a));

static< T> Stream< T> generate(Supplier< T> s)
生成一个无限长度得流,用于产生恒定得流和随机得流
static < T> Stream< T> concat(Stream<? extends T> a, Stream<? extends T> b)
合并两个流。

Stream<Integer> stream1 = Stream.of(1,2,3);
Stream<Integer> stream2 = Stream.of(4,5,6);
Stream.concat(stream1,stream2).forEach(i->System.out.println(i));

interface Builder< T> extends Consumer< T>
通过Stream.builder()创建,通过add(),accept()添加元素,通过builder()生成Stream。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值