java8 streamList轉(zhuǎn)換使用詳解
一、java8 stream 操作
List<Map<String, Object>> maps 轉(zhuǎn) Map<String, Object>的兩種方法
第一種,實用于數(shù)據(jù)查詢返回的是List<Map<String, Object>> maps
方法一、
Map<String, Object>; resultMap = lists .stream() .flatMap(map ->map.entrySet().stream()) .collect(Collectors.toMap(e ->e.getKey(), e->e.getValue(),(a,b)->a)));
方法二、
Map<String, Object> map = maps.stream() .map(Map::entrySet) .flatMap(Set::stream) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(a,b)->a)));
注意!這種轉(zhuǎn)換方法后面的(a,b)->a這個是必須的,因為list轉(zhuǎn)map可能會出現(xiàn)key值重復(fù)的情況,如果不指定去重規(guī)則,轉(zhuǎn)換的時候是會報錯的
第二種,實用于數(shù)據(jù)查詢返回的是List maps
Map<String, Object>; resultMap = lists .stream() .collect(Collectors.toMap(Entry::getProtity, Entry::getProtity,(a,b)->a)));
這種實體類list就比較容易,在這個過程中還可以進行條件過濾,filter 或者排序 reversed,用到時加進去就可以,這里就不贅述了
補充知識:java8 統(tǒng)計字符串字母個數(shù)的幾種方法(有你沒見到過的)
1.統(tǒng)計字符串字母個數(shù)(并且保持字母順序)
比如: aabbbbbbbba喔喔bcab cdabc deaaa
目前我做知道的有5種方式,如果你還有更好的,歡迎賜教
要求:統(tǒng)計字符串的字符個數(shù),最好按順序輸出每個字符的個數(shù)
//方式1 public static void letterCount1(String s) { s=s.replaceAll(" +", ""); //1,轉(zhuǎn)換成字符數(shù)組 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,獲取鍵所對應(yīng)的值 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 //其實就是字符串變成集合存每個字串,把每個字串循環(huán)跟集合比較 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開頭沒有字符,沒有關(guān)系,自動空填充 // 4.對于特殊字符,要注意使用轉(zhuǎn)義符 int count = (maxstr + " ").split(substr).length - 1; // System.out.println("\"" + minstr + "\"" + "字符串出現(xiàn)次數(shù):" + count); return count; } //如果要不區(qū)分大小寫,則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.統(tǒng)計字符串的單詞個數(shù)
這個其實跟上面一樣的,下面只寫一個簡潔的方法
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.統(tǒng)計文本單詞個數(shù)
//統(tǒng)計一個文本中單詞的個數(shù) 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); } //優(yōu)化:更精確的是根據(jù)非單詞來分組 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轉(zhuǎn)換使用詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java中java.util.Date和java.sql.Date之間的轉(zhuǎn)換的示例
java.util.Date是java.sql.Date的父類,有時候在和SqlServer數(shù)據(jù)庫打交道時,也會遇到,本文主要介紹了java中java.util.Date和java.sql.Date之間的轉(zhuǎn)換的示例,具有一定的參考價值,感興趣的可以了解一下2024-05-05spring整合redis緩存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用
本篇文章主要介紹了spring整合redis緩存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用,具有一定的參考價值,有興趣的可以了解一下。2017-04-04Springboot集成RabbitMQ死信隊列的實現(xiàn)
在大多數(shù)的MQ中間件中,都有死信隊列的概念。本文主要介紹了Springboot集成RabbitMQ死信隊列的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09