利用java8的stream函数式编程进行处理

1.实现字母分离

map将整个字符串当成一个单词流来处理

Map<String[], Long> collect14 = Stream.of("hello word how are you")
.map(o -> o.split(""))
// .flatMap(Arrays::stream)
.collect(Collectors.groupingBy(o -> o, Collectors.counting()));
System.out.println(JSONObject.toJSONString(collect14));

输出:{["h","e","l","l","o"," ","w","o","r","d"," ","h","o","w"," ","a","r","e"," ","y","o","u"]:1}

2.实现字符串中字母重复频率统计,利用flatMap对流进行扁平化处理(flatMap与map的不同见java8 stream编程第6点图解)

Map<String, Long> collect14 = Stream.of("hello word how are you")
.map(o -> o.split(""))
.flatMap(Arrays::stream)//将前面得到的带大括号的单词流转为字符流
.collect(Collectors.groupingBy(o -> o, Collectors.counting()));
System.out.println(JSONObject.toJSONString(collect14));

输出:{" ":4,"a":1,"r":2,"d":1,"u":1,"e":2,"w":2,"h":2,"y":1,"l":2,"o":4}

3.将map的entry转为stream,对map中的kv进行过滤

String collect14 = Stream.of("hello word how are you")
.map(o -> o.split(""))
.flatMap(Arrays::stream)
.collect(Collectors.groupingBy(o -> o, Collectors.counting()))
.entrySet()
.stream()
.filter(o -> o.getValue() == 2)
.limit(1)
.map(o -> o.getKey())
.collect(Collectors.joining());
System.out.println(collect14);

输出:r

最新文章

  1. 15天玩转redis —— 第九篇 发布/订阅模式
  2. npoi批量导入实现及相关技巧
  3. SqlIO优化
  4. C/C++基础总结
  5. 实现简单的cp命令
  6. 编译时IOS Device 无法选择的情况
  7. ArrayList、LinkedList、HashMap底层实现
  8. Oracle10g、 Oracle11g完美共存
  9. Red Hat Enterprise Linux x86-64 上安装 oracle 11gR2
  10. couldn&#39;t connect to the device trackpad
  11. Ch06 验证
  12. getuid和geteuid的区别
  13. Java 错误:找不到或无法加载主类
  14. [ExtJS5学习笔记]第二十二节 Extjs5中使用beforeLabelTpl配置给标签增加必填选项星号标志
  15. Openlayer 3加载本地ArcGIS切片
  16. 遇到的Cocos2dx问题
  17. Swift.Operator-and-Items-in-Swift(1)
  18. session 详细解析(转)
  19. 福州大学软件工程1816 | W班 第5次作业成绩排名
  20. Zephyr学习(三)启动过程

热门文章

  1. java 统计字符串中连续重复的字符,并得出新字符串
  2. AIX中的页空间管理
  3. 不要和SB理论
  4. ASP.NET 中 取得 Repeater 里的checkbox值
  5. 告别if/else连环写法
  6. DevExpress Windows 10 v19.1新版亮点:UWP控件新功能全面解析
  7. 集合操作符 Union / Union All / Intersect / Minus
  8. C语言字符串追加,双色球等案例
  9. Raspberry Pi3驱动Oled ssh1106屏
  10. ideal 工具jdk环境配置