Using functional programming stream is processed java8
1. To achieve separation of the letters
map the entire string as a word stream processing
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. To achieve the letter string repetition frequency statistics by convection flatMap flattening process (see flatMap with different map java8 stream programmed point 6 illustrates a)
Map <String, Long> collect14 Stream.of = ( "Hello Word How are you" ) .map (O -> o.split ( "" )) .flatMap (Arrays, :: Stream) obtained above with // the large brackets word stream into a character 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. The map of the entry into stream, to map the filter 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);
Output: r