Stream流中方法详解

中间方法和终结方法

以下传入参数均表示lambda表达式中的参数

forEach():遍历集合,对流中的每个元素执行指定的操作
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().forEach(s-> System.out.println());

void forEach(Consumer<? super T> action);//接口中的抽象方法

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);
    
}

传入集合元素,无返回值,方法体内对元素做操作

peek():和forEach类似,但不会消耗和结束流,通常用于调试和观察流的元素
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().peek(s -> System.out.println(s));

 Stream<T> peek(Consumer<? super T> action);

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);
    
}

传入集合元素,无返回值,方法体内对元素做操作

filter(Predicate):根据指定的条件过滤流中的元素
 ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
 list.stream().filter(s -> s.equals("a"));

 Stream<T> filter(Predicate<? super T> predicate);

@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
}

传入集合元素,返回布尔值,方法体内写元素的条件,符合条件保留

distinct():去除重复的元素
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().distinct();

Stream<T> distinct();

不用传入,保留第一次出现的元素,去除流中的重复元素

limit()限制元素数量
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().limit(3);

Stream<T> limit(long maxSize);

传入long类型数字,只保留流的前n个元素,n为传入参数

anyMatch(Predicate)判断是否有元素满足条件,终结流
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().anyMatch(s -> s.equals("a"));

boolean anyMatch(Predicate<? super T> predicate);

@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
}

传入集合元素,方法体中写对元素的判断条件,返回布尔值

allMatch(Predicate)判断是否都满足条件,终结流
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().allMatch(s -> s.equals("a"));

boolean allMatch(Predicate<? super T> predicate);

@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
}

传入集合元素,方法体中写对元素的判断条件,返回布尔值

noneMatch(Predicate)判断是否没有元素满足条件,终结流
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().noneMatch(s -> s.equals("a"));

boolean noneMatch(Predicate<? super T> predicate);

@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
}

传入集合元素,方法体中写对元素的判断条件,返回布尔值

map(Function)将函数结果映射到新的流中
ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
list.stream().map(s -> "t");

<R> Stream<R> map(Function<? super T, ? extends R> mapper);

@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);
}

传入集合元素,方法体中写映射的元素,返回这个类型的元素

最终返回的是一个只含映射后的新元素的流

sort()从小到大排序
ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
list.stream().sorted();

Stream<T> sorted();

对流进行排序

sort(Comparator)构造器排序
ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
list.stream().sorted((o1,o2)->o2.compareTo(o1));

Stream<T> sorted(Comparator<? super T> comparator);

@FunctionalInterface
public interface Comparator<T> {//熟悉的构造器接口
    int compare(T o1, T o2);
}

同上,只不过传入的是构造器自定义排序

收集方法

ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
list.stream().toArray();
list.stream().collect(Collectors.toList());
list.stream().collect(Collectors.toSet());
list.stream().collect(Collectors.toMap(s -> s.charAt(0),s->s));

分别代表收集数组,列表,无序列表和双列集合的方法

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值