【word count 程序】 通过 java8实现
来源:互联网 发布:买司考音频淘宝哪家好 编辑:程序博客网 时间:2024/06/03 12:55
hadoop的入门程序,java8也能实现
txt统计单词数量程序
@Testpublic void fileWordCount() throws IOException { //特殊文件需要格式转换为txt Files.readAllLines(Paths.get("D:\\jd.txt"), StandardCharsets.UTF_8).parallelStream() //将多个流融合为一个 .flatMap(line -> Arrays.stream(line.trim().split("\\s"))) .filter(word -> word.length() > 0) .map(word -> new AbstractMap.SimpleEntry<>(word, 1)) .collect(groupingBy(AbstractMap.SimpleEntry :: getKey, counting())) .entrySet().forEach(System.out :: println);}
List统计单词数量程序@Testpublic void listWordCount(){ List<String> stringList = Arrays.asList("a","b","c","a"); stringList.stream() .map(s -> new AbstractMap.SimpleEntry<>(s, 1)) .collect(groupingBy(AbstractMap.SimpleEntry :: getKey, counting())) .entrySet().stream() .forEach(System.out :: println); System.out.println("---------------------------------------------------"); //通过自定义reduce统计,其实counting()也使用的是reduce //记住:凡是在中间操作使用了map,接口定义都需要声明出来,直接使用lambda表达式会有1.无法读取method,2.类型检查不到 的问题 BinaryOperator<Integer> binaryOperator2 = Integer::sum; //排序的转换规则接口 ToIntFunction<Map.Entry> sortMapFunction = (Map.Entry se) -> Integer.valueOf(se.getValue().toString()).intValue(); stringList .stream() .map(s -> new AbstractMap.SimpleEntry<>(s, 1)) .collect(groupingBy(AbstractMap.SimpleEntry::getKey, reducing(0, AbstractMap.SimpleEntry::getValue,binaryOperator2))) .entrySet() .stream() .sorted(Comparator.comparingInt(sortMapFunction)) .forEach(System.out::println);}
阅读全文
0 0
- 【word count 程序】 通过 java8实现
- 通过简单的Word Count讲解MapReduce原理以及Java实现
- Word Count
- 0915_MapReduce初窥——Word Count程序
- Hadoop完全分布式环境配置及 Word Count 程序运行
- 通过程序实现SMARTFORMS文本编辑模式在WORD和TEXT文本之间转换
- 【c++系列】map容器实现的word count
- Python+Hadoop Streaming实现MapReduce(word count)
- Python+Hadoop Streaming实现MapReduce(word count)
- Akka初体验之scala版word-count 的实现
- Python通过win32实现office自动化 - Word
- 通过jsp实现word预览与打印
- Word 通过添加Package 实现word藏毒
- [Memo] restricted word count
- python word count
- hadoop word count example
- Word Count on hadoop
- Word Count示例
- git 上的pull request 是什么意思?
- 如何处理Android Studio 上面关于 update 和 commit 小箭头的消失
- java new 内存分配
- java 中 return 的两种常见的用法
- Android studio 如何查看当前git 分支的4种方式
- 【word count 程序】 通过 java8实现
- RxJava 和 RxAndroid 一 (基础)
- 让用户无法在微信中举报的代码
- Android studio .gitignore 文件的内容
- Pixhawk原生固件PX4之位姿控制算法解读
- Socket客户端和服务端通讯
- Java方向如何准备BAT技术面试答案(汇总版)
- Android IntentService——知道这个你就能去BAT
- python问题:IndentationError:expected an indented block错误解决