Python 官方文档:入门教程 => 点击学习
目录前言forEach()map()map()源码:filter()filter()源码:sorted()sorted()源码:collect()collect()源码:总结前言 S
Stream是一个来自数据源的元素队列并支持聚合操作,其中具有以下特性:
本文总结了部分在日常开发中经常接触到的一些Stream流相关的方法,不足之处或有错误欢迎留评,总结的几个方法如下:
forEach()源码:
void forEach(Consumer<!--? super T--> action);
Stream().forEach() : 迭代流中的数据
forEach() 的返回类型为void,不会产生新的流
举个栗子:
public void testForEach() {
Random random = new Random();
random.ints().limit(10).forEach(System.out::println);
}
运行结果:
再举个灵活一点的栗子:
public void testForEach() {
AtomicInteger i = new AtomicInteger();//原子类型
Map<Integer,String> map = new HashMap<>();
List<String> list = new ArrayList<>(Arrays.asList("Hello",",","world"));
list.stream().forEach(s->{
map.put(i.getAndIncrement(),s);
});
}
对于forEach()方法在本人日常开发中常用于:
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
Stream().map() 用于映射每个元素到对应的结果
返回类型为Stream,map()会产生新的流并返回
举个栗子:
public void testMap(){
List<Integer> list = Arrays.asList(1,2,3);
list.stream().map(num -> num * num)
.distinct()
.collect(Collectors.toList())
.forEach(System.out::println);
}
Stream<T> filter(Predicate<? super T> predicate);
Stream().filter()为条件过滤器
举个栗子:
public void testFilter(){
List<String> list = new ArrayList<>(Arrays.asList("ab","","abc","","acd"));
long count = list.stream()
.filter(str -> str.isEmpty())
.count();
}
Stream<T> sorted();
Stream<T> sorted(Comparator<? super T> comparator);
Stream支持两种方式的排序:
无参方法默认为自然排序sorted(Comparator comp) 按自定义比较器进行排序
仍然是举个栗子:
1)无参方法
public void testSorted(){
List<String> list = new ArrayList<>(Arrays.asList("aaa","ccc","bbb"));
list.stream().sorted().forEach(System.out::println);
}
执行结果:
2)带参方法(User类由name和age组成)
public void testSorted(){
List<User> userList = new ArrayList<>(Arrays.asList(
new User("zhangsan",22),
new User("wangwu",22),
new User("badao",32),
new User("kongfu",16)
));
Stream<User> sorted = userList.stream().sorted((x, y) -> {
if (x.getAge() == y.getAge()) { // 使用流中的序列两两进行比较
return x.getName().compareTo(y.getName());
} else {
return x.getAge() - y.getAge();//顺序
// y.getAge() - x.getAge() 为逆序
}
});
sorted.forEach(System.out::println);
}
执行结果:
<R> R collect(Supplier<R> supplier,
BiConsumer<R, ? super T> accumulator,
BiConsumer<R, R> combiner);
stream().collect() 由三个参数构成 :
1.Supplier 生产者,返回最终
2.结果BiConsumer<R, ? super T> accumulator,累加器 :
其中 R 为要返回的集合, ? super T 为遍历过程中的每个参数,相当于add操作
3.BiConsumer<R,R> combiner,合并器 :
有并行流时才会使用,相当于将第二部操作形成的list添加到最终的list,addAll操作
举个栗子:
1)new()
public void testCollect(){
Stream<String> stream = Stream.of("hello","world","hello,world");
// new()
List<String> list = stream.collect(Collectors.toList());//List
// 指定集合类型,如ArrayList
ArrayList<String> arrayList = stream.collect(Collectors.toCollection(ArrayList::new));
//Set
stream.collect(Collectors.toSet());
// 指定HashSet
HashSet<String> hashSet = stream.collect(Collectors.toCollection(HashSet::new));
// 拼接字符串
String str = stream.collect(Collectors.joining());
2)new() -> add() -> addAll()
public void testCollect(){
Stream<String> stream = Stream.of("hello","world","hello,world");
// new() -> add() -> addAll()完整演示
HashMap<String,String> map = stream.collect(HashMap::new,(x,y)->{
x.put(y,y); // x 为集合,y 为当前遍历元素,以当前遍历元素作为kv
},HashMap::putAll);
map.forEach((x,y)->{
System.out.println(x+" : "+y);
});
执行结果:
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注编程网的更多内容!
--结束END--
本文标题: 手把手带你了解Java-Stream流方法学习及总结
本文链接: https://www.lsjlt.com/news/133055.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模 安全指南 Osprey 游戏分析 游戏调试 游戏图形 游戏物理 开源库
0