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));

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

相关推荐

  1. Stream方法详解

    2024-04-10 13:00:01       32 阅读
  2. Stream详解

    2024-04-10 13:00:01       24 阅读
  3. streamdistinct方法重写equals相关

    2024-04-10 13:00:01       53 阅读
  4. Stream API 使用的详细示例

    2024-04-10 13:00:01       49 阅读
  5. <span style='color:red;'>Stream</span><span style='color:red;'>流</span>

    Stream

    2024-04-10 13:00:01      47 阅读
  6. Stream

    2024-04-10 13:00:01       61 阅读
  7. <span style='color:red;'>Stream</span><span style='color:red;'>流</span>

    Stream

    2024-04-10 13:00:01      49 阅读
  8. <span style='color:red;'>Stream</span><span style='color:red;'>流</span>

    Stream

    2024-04-10 13:00:01      35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-10 13:00:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-10 13:00:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-10 13:00:01       82 阅读
  4. Python语言-面向对象

    2024-04-10 13:00:01       91 阅读

热门阅读

  1. 自己总结的ICT云计算题库二

    2024-04-10 13:00:01       25 阅读
  2. 自己总结的ICT云计算题库

    2024-04-10 13:00:01       25 阅读
  3. 30个商业赚钱的思考(上)

    2024-04-10 13:00:01       31 阅读
  4. 使用wangeditor富文本插件,自定义上传到七牛

    2024-04-10 13:00:01       32 阅读
  5. C# 抽象类、接口

    2024-04-10 13:00:01       38 阅读