本文主要是介绍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));
分别代表收集数组,列表,无序列表和双列集合的方法
这篇关于Stream流中方法详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!