How to write a counter in Java 8?
2015-12-10 10:35
585 查看
Writing a counter in Java can be as simple as 2 lines. In addition to its simplicity, we can also utilize the parallel computation to increase the counter's performance.
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Map;
public class Java8Counter {
public static void main(String[] args) {
String[] arr = { "program", "creek", "program", "creek", "java", "web",
"program" };
Stream<String> stream = Stream.of(arr).parallel();
Map<String, Long> counter = stream.collect(Collectors.groupingBy(
String::toString, Collectors.counting()));
System.out.println(counter.get("creek"));
}
}
When you get the map, you may also want to sort the map by value. Check out this post to see how.
参考: http://www.programcreek.com/2014/01/how-to-write-a-counter-in-java-8/
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Map;
public class Java8Counter {
public static void main(String[] args) {
String[] arr = { "program", "creek", "program", "creek", "java", "web",
"program" };
Stream<String> stream = Stream.of(arr).parallel();
Map<String, Long> counter = stream.collect(Collectors.groupingBy(
String::toString, Collectors.counting()));
System.out.println(counter.get("creek"));
}
}
When you get the map, you may also want to sort the map by value. Check out this post to see how.
参考: http://www.programcreek.com/2014/01/how-to-write-a-counter-in-java-8/
相关文章推荐
- 【springmvc+mybatis项目实战】杰信商贸-15.细粒度的权限控制+业务上报取消
- 【springmvc+mybatis项目实战】杰信商贸-16.新增从表货物信息
- Java常用代码片段
- Java使用BigDecimal保留double、float运算精度、保留指定位数有效数字、四舍五入
- Java使用BigDecimal保留double、float运算精度、保留指定位数有效数字、四舍五入
- MyEclipse工程高版本换低版本报错
- Java中equals和==的区别
- Java对于私有变量“反思暴力”技术
- MyEclipse工程高版本换低版本报错
- Java中equals和==的区别
- 从几个sample来学习Java堆,方法区,Java栈和本地方法栈
- Java中equals和==的区别
- JavaEE_post乱码和乱码的解决方法
- 【Java字符串高效率】统计单词统计个数
- jdk和jre有什么区别
- Efficient Counter in Java
- Java性能调优笔记
- Struts2 Convention Plugin ( struts2 零配置 )
- [SqlHandle] java mysql语法 sql语句拼接工具简类
- Java内存与垃圾回收调优