一、java8 stream 操作
List<Map<String, Object>> maps 轉 Map<String, Object>的兩種方法
第一種,實用于數據查詢返回的是List<Map<String, Object>> maps
方法一、
1
2
3
4
|
Map<String, Object>; resultMap = lists .stream() .flatMap(map ->map.entrySet().stream()) .collect(Collectors.toMap(e ->e.getKey(), e->e.getValue(),(a,b)->a))); |
方法二、
1
2
3
4
|
Map<String, Object> map = maps.stream() .map(Map::entrySet) .flatMap(Set::stream) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(a,b)->a))); |
注意!這種轉換方法后面的(a,b)->a這個是必須的,因為list轉map可能會出現key值重復的情況,如果不指定去重規則,轉換的時候是會報錯的
第二種,實用于數據查詢返回的是List maps
1
2
3
|
Map<String, Object>; resultMap = lists .stream() .collect(Collectors.toMap(Entry::getProtity, Entry::getProtity,(a,b)->a))); |
這種實體類list就比較容易,在這個過程中還可以進行條件過濾,filter 或者排序 reversed,用到時加進去就可以,這里就不贅述了
補充知識:java8 統計字符串字母個數的幾種方法(有你沒見到過的)
1.統計字符串字母個數(并且保持字母順序)
比如: aabbbbbbbba喔喔bcab cdabc deaaa
目前我做知道的有5種方式,如果你還有更好的,歡迎賜教
要求:統計字符串的字符個數,最好按順序輸出每個字符的個數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
//方式1 public static void letterCount1(String s) { s=s.replaceAll( " +" , "" ); //1,轉換成字符數組 char c[]=s.toCharArray(); Map<Character, Integer> tree= new TreeMap<Character, Integer>(); for ( int i = 0 ; i < c.length; i++) { //第一次:a,1 //第二次:a,2 //2,獲取鍵所對應的值 Integer value=tree.get(c[i]); //3,存儲判斷 tree.put(c[i], value== null ? 1 :value+ 1 ); } System.out.println(tree); } //方式2 使用流 //這個在測試特殊字符,比如\ \n時,他的順序會不對,這個是Map造成的 //解決辦法使用TreeMap public static void letterCount2(String s) { s=s.replaceAll( " +" , "" ); TreeMap<String, Long> result = Arrays.stream(s.split( "" )) .sorted() // .collect(Collectors.groupingBy(Function.identity(),Collectors.counting())); .collect(Collectors.groupingBy(Function.identity(),TreeMap:: new ,Collectors.counting())); System.out.println(result); } //或者 public static void letterCount2_1(String s) throws Exception { s=s.replaceAll( " +" , "" ); Stream<String> words = Arrays.stream(s.split( "" )); Map<String, Integer> wordsCount = words.collect(Collectors.toMap(k -> k, v -> 1 , (i, j) -> i + j)); System.out.println(wordsCount); } //方式3 使用Collections.frequency //其實就是字符串變成集合存每個字串,把每個字串循環跟集合比較 public static void letterCount3(String s) { s=s.replaceAll( " +" , "" ); List<String> list=Arrays.asList(s.split( "" )); Map<String,Integer> map= new TreeMap<String, Integer>(); for (String str : list) { map.put(str, Collections.frequency(list, str)); } System.out.println(map); } //方式4 public static void letterCount4(String s) { s=s.replaceAll( " +" , "" ); String[] strs = s.split( "" ); Map<String,Integer> map= new TreeMap<String, Integer>(); for (String str : strs) { map.put(str, stringCount(s, str)); } System.out.println(map); } //方式5 public static void letterCount5(String s) { s=s.replaceAll( " +" , "" ); String[] strs = s.split( "" ); Map<String,Integer> map= new TreeMap<String, Integer>(); for (String str : strs) { map.put(str, stringCount2(s, str)); } System.out.println(map); } //巧用split public static int stringCount(String maxstr, String substr) { // 注意 // 1.比如qqqq,沒有找到,則直接返回這個字符串 // 2.比如qqqjava,末尾沒有其他字符,這時也不會分割,所以可以添加一個空格 // 3.java11開頭沒有字符,沒有關系,自動空填充 // 4.對于特殊字符,要注意使用轉義符 int count = (maxstr + " " ).split(substr).length - 1 ; // System.out.println("\"" + minstr + "\"" + "字符串出現次數:" + count); return count; } //如果要不區分大小寫,則compile(minstr,CASE_INSENSITIVE) public static int stringCount2(String maxstr, String substr) { int count = 0 ; Matcher m = Pattern.compile(substr).matcher(maxstr); while (m.find()) { count++; } return count; } |
2.統計字符串的單詞個數
這個其實跟上面一樣的,下面只寫一個簡潔的方法
1
2
3
4
5
6
7
|
public static void wordStringCount(String s) { //這里開始是字符串,分割后變成字符串流 Map<String, Long> result = Arrays.stream(s.split( "\\s+" )) .map(word -> word.replaceAll( "[^a-zA-Z]" , "" )) .collect(Collectors.groupingBy(Function.identity(),Collectors.counting())); System.out.println(result); } |
3.統計文本單詞個數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//統計一個文本中單詞的個數 public static void wordFileCount(String path) throws IOException{ //這里一開始字符串流 //先分割 //在變成字符流 //在篩選 Map<String, Long> result = Files.lines(Paths.get(path),Charset.defaultCharset()) .parallel() //字符串流--分割--字符串流 .flatMap(str->Arrays.stream(str.split( " +" ))) .map(word -> word.replaceAll( "[^a-zA-Z]" , "" )) //去掉空 .filter(word->word.length()> 0 ) .collect(Collectors.groupingBy(Function.identity(),Collectors.counting())); System.out.println(result); } //優化:更精確的是根據非單詞來分組 public static void wordFileCount0(String path) throws IOException{ Map<String, Long> result = Files.lines(Paths.get(path),Charset.defaultCharset()) .parallel() //字符串流--分割--字符串流 .flatMap(str->Arrays.stream(str.split( "[^a-zA-Z]+" ))) //去掉\n .filter(word->word.length()> 0 ) .collect(Collectors.groupingBy(Function.identity(),Collectors.counting())); System.out.println(result); } |
以上這篇java8 streamList轉換使用詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/lyy19931025/article/details/108010464