Java stream的collect和Collector

最近在使用Java8的新特性stream,因为LZ也在使用Spark,所以这种流式编程还是蛮顺手的,比以前的循环处理方便很多。stream基本用法网上有很多教程,这里就不在赘述了。这里LZ重点介绍stream的collect方法和其强大的工具类Collector。
先看下接口中声名的collect方法:

第一个方法比较常用,比如将stream转化为集合:

其中Collectors类中提供了许多Collector接口的默认实现,这些实现能满足大部分的需求,否则要自己来实现Collector接口。Collector接口的3个泛型分别表示:

  • T:输入元素类型(上例中的集合元素类型String)
  • A:缩减操作的可变累积类型(上例中不存在)
  • R:可变减少操作的结果类型(上例中的集合类型List)
    这个方法是将N个输入元素T转化为1个输出元素R的过程,比如算平均数:

还有更复杂的groupby操作:

第二个方法稍微复杂些,下面是该方法几个参数的接口:

supplier用来创建最终的结果类型R(第一个例子中的List)

accumulator和combiner都实现了上面的接口,accumulator用来将输入元素转化为中间元素(第一个例子中将String插入List,T->A。这个例子中的中间元素和结果元素的类型相同)。combiner用来将中间元素转化为结果元素(第一个例子中将多个List合并为一个List,A->R),下图为整个合并的过程:
avatar
用这种方式改写下第一个例子:

100 Replies to “Java stream的collect和Collector”

  1. Pingback: viagra pills
  2. Pingback: 5mg cialis
  3. Pingback: ed drugs
  4. Pingback: sildenafil 50mg
  5. Pingback: DPTPtNqS
  6. Pingback: qQ8KZZE6
  7. Pingback: D6tuzANh
  8. Pingback: SHKALA TONOV
  9. Pingback: 匿名
  10. Pingback: russianmanagement.com
  11. Pingback: chelovek-iz-90-h
  12. Pingback: 3Hk12Bl
  13. Pingback: 3NOZC44
  14. Pingback: 01211
  15. Pingback: tor-lyubov-i-grom
  16. Pingback: film-tor-2022
  17. Pingback: hd-tor-2022
  18. Pingback: hdorg2.ru
  19. Pingback: Psikholog
  20. Pingback: netstate.ru
  21. Pingback: Link
  22. Pingback: tor-lyubov-i-grom.ru
  23. Pingback: bit.ly
  24. Pingback: cleantalkorg2.ru
  25. Pingback: bucha killings
  26. Pingback: War in Ukraine
  27. Pingback: Ukraine
  28. Pingback: Ukraine-Russia
  29. Pingback: site
  30. Pingback: stats
  31. Pingback: Ukraine-war
  32. Pingback: movies
  33. Pingback: gidonline
  34. Pingback: web
  35. Pingback: film.8filmov.ru
  36. Pingback: video
  37. Pingback: film
  38. Pingback: sY5am
  39. Pingback: Dom drakona
  40. Pingback: JGXldbkj
  41. Pingback: aOuSjapt
  42. Pingback: psikholog moskva
  43. Pingback: Usik Dzhoshua 2 2022
  44. Pingback: Dim Drakona 2022
  45. Pingback: TwnE4zl6
  46. Pingback: psy 3CtwvjS
  47. Pingback: lalochesia
  48. Pingback: film onlinee
  49. Pingback: psycholog-v-moskve.ru
  50. Pingback: psycholog-moskva.ru
  51. Pingback: 3qAIwwN
  52. Pingback: video-2
  53. Pingback: sezons.store
  54. Pingback: psy-news.ru
  55. Pingback: 000-1
  56. Pingback: 3SoTS32
  57. Pingback: 3DGofO7
  58. Pingback: sitestats01
  59. Pingback: 1c789.ru
  60. Pingback: cttdu.ru
  61. Pingback: 1703
  62. Pingback: hdserial2023.ru
  63. Pingback: serialhd2023.ru
  64. Pingback: matchonline2022.ru
  65. Pingback: bit.ly/3OEzOZR
  66. Pingback: bit.ly/3gGFqGq
  67. Pingback: bit.ly/3ARFdXA
  68. Pingback: bit.ly/3ig2UT5
  69. Pingback: bit.ly/3GQNK0J
  70. Pingback: bep5w0Df
  71. Pingback: www
  72. Pingback: icf
  73. Pingback: 24hours-news
  74. Pingback: rusnewsweek
  75. Pingback: uluro-ado
  76. Pingback: irannews.ru
  77. Pingback: klondayk2022
  78. Pingback: mangalib
  79. Pingback: x
  80. Pingback: 9xflix
  81. Pingback: xnxx
  82. Pingback: 123movies
  83. Pingback: video.vipspark.ru
  84. Pingback: vitaliy-abdulov.ru
  85. Pingback: psychophysics.ru
  86. Pingback: vipspark.vipspark.ru

发表评论