亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java8 streamList轉(zhuǎn)換使用詳解

 更新時間:2020年08月15日 16:13:33   作者:一匹有夢想的蝸牛  
這篇文章主要介紹了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)文章

  • Jmeter使用接口傳遞數(shù)據(jù)過程圖解

    Jmeter使用接口傳遞數(shù)據(jù)過程圖解

    這篇文章主要介紹了Jmeter使用接口傳遞數(shù)據(jù)過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • java中java.util.Date和java.sql.Date之間的轉(zhuǎ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-05
  • spring整合redis緩存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用

    spring整合redis緩存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用

    本篇文章主要介紹了spring整合redis緩存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用,具有一定的參考價值,有興趣的可以了解一下。
    2017-04-04
  • idea本地merge如何合并代碼

    idea本地merge如何合并代碼

    這篇文章主要介紹了idea本地merge如何合并代碼問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Java 堆內(nèi)存溢出原因分析

    Java 堆內(nèi)存溢出原因分析

    這篇文章主要介紹了Java 堆內(nèi)存溢出原因分析,任何使用過基于 Java 的企業(yè)級后端應(yīng)用的軟件開發(fā)者都會遇到過這種報錯,java.lang.OutOfMemoryError:Java heap space。,需要的朋友可以參考下
    2019-06-06
  • maven 配置多個倉庫的方法

    maven 配置多個倉庫的方法

    這篇文章主要介紹了maven 配置多個倉庫的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Springboot集成RabbitMQ死信隊列的實現(xiàn)

    Springboot集成RabbitMQ死信隊列的實現(xiàn)

    在大多數(shù)的MQ中間件中,都有死信隊列的概念。本文主要介紹了Springboot集成RabbitMQ死信隊列的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Spring?Data?JPA注解Entity使用示例詳解

    Spring?Data?JPA注解Entity使用示例詳解

    這篇文章主要為大家介紹了Spring?Data?JPA注解Entity使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Spring的實例工廠方法和靜態(tài)工廠方法實例代碼

    Spring的實例工廠方法和靜態(tài)工廠方法實例代碼

    這篇文章主要介紹了Spring的實例工廠方法和靜態(tài)工廠方法實例代碼,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • JAVALambda表達式與函數(shù)式接口詳解

    JAVALambda表達式與函數(shù)式接口詳解

    大家好,本篇文章主要講的是JAVALambda表達式與函數(shù)式接口詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02

最新評論