java8中的Stream
2015-06-15 12:03
627 查看
Collection.stream() / parallelStream()
1. Stream
1)Filter
stringCollection .stream().filter((s) -> s.startsWith("a")) .forEach(System.out::println);
2)Sorted
stringCollection .stream().sorted()
3)Map
stringCollection .stream().map(String::toUpperCase) .sorted((a, b) -> b.compareTo(a))
4)Matches
boolean anyStartsWithA = stringCollection.stream().anyMatch((s) -> s.startsWith("a"));
boolean allStartsWithA = stringCollection.stream().allMatch((s) -> s.startsWith("a"));
boolean noneStartsWithZ = stringCollection.stream().noneMatch((s) -> s.startsWith("z"));
5)Count
long startsWithB = stringCollection.stream().filter((s) -> s.startsWith("b")) .count();
6)Reduce
stringCollection .stream().sorted().reduce((s1, s2) -> s1 + "#" + s2);
2. Parallel stream
1) sorted
long t0 = System.nanoTime();
long count = values.parallelStream().sorted().count(); System.out.println(count);
long t1 = System.nanoTime();
long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); System.out.println(String.format("parallel sort took: %d ms", millis)); // parallel sort took: 472 ms
2) MAP
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < 10; i++) { map.putIfAbsent(i, "val" + i);}
map.forEach((id, val) -> System.out.println(val));
map.computeIfPresent(3, (num, val) -> val + num); map.get(3); // val33
map.computeIfPresent(9, (num, val) -> null); map.containsKey(9); // false
map.computeIfAbsent(23, num -> "val" + num); map.containsKey(23); // true
map.computeIfAbsent(3, num -> "bam"); map.get(3); // val33
map.getOrDefault(42, "not found"); // not found
map.merge(9, "val9", (value, newValue) -> value.concat(newValue)); map.get(9); // val9
map.merge(9, "concat", (value, newValue) -> value.concat(newValue)); map.get(9); // val9concat
1. Stream
1)Filter
stringCollection .stream().filter((s) -> s.startsWith("a")) .forEach(System.out::println);
2)Sorted
stringCollection .stream().sorted()
3)Map
stringCollection .stream().map(String::toUpperCase) .sorted((a, b) -> b.compareTo(a))
4)Matches
boolean anyStartsWithA = stringCollection.stream().anyMatch((s) -> s.startsWith("a"));
boolean allStartsWithA = stringCollection.stream().allMatch((s) -> s.startsWith("a"));
boolean noneStartsWithZ = stringCollection.stream().noneMatch((s) -> s.startsWith("z"));
5)Count
long startsWithB = stringCollection.stream().filter((s) -> s.startsWith("b")) .count();
6)Reduce
stringCollection .stream().sorted().reduce((s1, s2) -> s1 + "#" + s2);
2. Parallel stream
1) sorted
long t0 = System.nanoTime();
long count = values.parallelStream().sorted().count(); System.out.println(count);
long t1 = System.nanoTime();
long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); System.out.println(String.format("parallel sort took: %d ms", millis)); // parallel sort took: 472 ms
2) MAP
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < 10; i++) { map.putIfAbsent(i, "val" + i);}
map.forEach((id, val) -> System.out.println(val));
map.computeIfPresent(3, (num, val) -> val + num); map.get(3); // val33
map.computeIfPresent(9, (num, val) -> null); map.containsKey(9); // false
map.computeIfAbsent(23, num -> "val" + num); map.containsKey(23); // true
map.computeIfAbsent(3, num -> "bam"); map.get(3); // val33
map.getOrDefault(42, "not found"); // not found
map.merge(9, "val9", (value, newValue) -> value.concat(newValue)); map.get(9); // val9
map.merge(9, "concat", (value, newValue) -> value.concat(newValue)); map.get(9); // val9concat
相关文章推荐
- BigDecimal类的加减乘除
- 处理后缀填充
- struts2 type="redirectAction" 使用范例讲解
- 查看ADT版本,安装、卸载、更新eclipse中的ADT
- Vijava (更改虚拟机配置信息(内存、CPU和添加新磁盘)终极版本)
- Vijava (模板部署虚拟机,并指定自定义规范(终极版本))
- JAVA 面向对象和集合知识点总结(6.15更新)
- Eclipse上安装GIT插件EGit及使用
- eclipse 项目 导入到 Android studio 中
- java进阶之工厂模式(二)抽象工厂模式
- java.util.ConcurrentModificationException
- Java NIO系列教程(五) 通道之间的数据传输
- Java并发编程实战笔记_并发任务执行
- Java并发编程基础构建模块(06)——高效缓存总结示例
- 《Java从入门到精通》第九章学习笔记
- Eclipse配色方案插件
- Java并发编程基础构建模块(04)——线程阻塞与中断
- Java并发编程基础构建模块(02)——并发容器
- Javascrip的应用
- java:快速文件分割及合并