当前位置: 移动技术网 > IT编程>开发语言>Java > java8 streamList转换使用详解

java8 streamList转换使用详解

2020年08月16日  | 移动技术网IT编程  | 我要评论

一、java8 stream 操作

list<map<string, object>> maps 转 map<string, object>的两种方法

第一种,实用于数据查询返回的是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)));

注意!这种转换方法后面的(a,b)->a这个是必须的,因为list转map可能会出现key值重复的情况,如果不指定去重规则,转换的时候是会报错的

第二种,实用于数据查询返回的是list maps

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
  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.统计字符串的单词个数

这个其实跟上面一样的,下面只写一个简洁的方法

 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.统计文本单词个数

 //统计一个文本中单词的个数
  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转换使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网