java8集合多字段分组统计 java8 集合 多字段 分组 统计个数代码
weixin_36754290 人气:0想了解java8 集合 多字段 分组 统计个数代码的相关内容吗,weixin_36754290在本文为您仔细讲解java8集合多字段分组统计的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java8,集合,多字段,分组,统计个数,下面大家一起来学习吧。
1.user实体
package com.demo.dto; public class User { private Integer id; private String userName; private String password; private Integer age; private long c; public User() { super(); // TODO Auto-generated constructor stub } public User(Integer id, String userName, String password, Integer age) { super(); this.id = id; this.userName = userName; this.password = password; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public long getC() { return c; } public void setC(long l) { this.c = l; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", age=" + age + ", c=" + c + "]"; } public static void main(String[] args) { User u1=new User(1, "aa", "aap", 23); User u2=new User(2, "aa", "aap", 23); User u3=new User(3, "bb", "aap", 23); User u4=new User(4, "cc", "aap", 23); User u5=new User(5, "cc", "aap", 23); User u6=new User(6, "cc", "aap", 23); User u7=new User(7, "aa", "aap", 24); List<User> list=new ArrayList<User>(); list.add(u1);list.add(u2);list.add(u7);list.add(u3);list.add(u4);list.add(u5);list.add(u6); //原有list(根据第二个字段:userName和第四个字段:age 统计重复的记录数) //jdk8的方法统计个数: Map<String, Map<Integer, Long>> map = list.stream().collect(Collectors.groupingBy(User::getUserName,Collectors.groupingBy(User::getAge,Collectors.counting()))); //jdk8以下: Map<String,Map<Integer,Long>> map=new HashMap<String,Map<Integer,Long>>(); for (User user1 : list) { Map<Integer, Long> value=new HashMap<Integer,Long>(); long count=0; if(map.containsKey(user1.getUserName())){ continue; } for(int i=0;i<list.size();i++){ if(user1.getUserName().equals(list.get(i).getUserName())&&user1.getAge()==list.get(i).getAge()){ count+=1; value.put(user1.getAge(),count); map.put(user1.getUserName(), value); }else if(user1.getUserName().equals(list.get(i).getUserName())&&user1.getAge()!=list.get(i).getAge()){ value.put(list.get(i).getAge(),Long.valueOf(1)); map.put(user1.getUserName(), value); } } } map.forEach((k, v) -> { System.out.println(k+">>>>"+v); }); List<User> list2=new ArrayList<User>(); list.forEach(user ->{ map.forEach((k, v) -> { if(k==user.getUserName()){ Long remove = v.remove(user.getAge()); user.setC(null==remove?0:remove); } }); list2.add(user); }); //遍历最后想要的结果(User中c为统计后的个数,方便前台遍历集合时单元格合并行) list2.forEach(u ->{ System.out.println(u); }); } }
备注:运行结果如下
cc>>>>{23=3} bb>>>>{23=1} aa>>>>{23=2, 24=1} User [id=1, userName=aa, password=aap, age=23, c=2] User [id=2, userName=aa, password=aap, age=23, c=0] User [id=7, userName=aa, password=aap, age=24, c=1] User [id=3, userName=bb, password=aap, age=23, c=1] User [id=4, userName=cc, password=aap, age=23, c=3] User [id=5, userName=cc, password=aap, age=23, c=0] User [id=6, userName=cc, password=aap, age=23, c=0]
此处是为了实现如下效果:
补充知识:List集合中元素按照固定数量进行分组
最近做项目,被要求将全国所有省份在页面中展示出来,但是要求
1、字少的三个省份占一行
2、字多的两个省份占一行
想来想去只能想到查询出所有省份,然后分组返回数据,上代码
public Map<String,Object> campus_list_provence(HttpServletRequest request,HttpServletResponse response)throws Exception{ Map<String,Object> map=new HashMap<String,Object>(); String sql="查询所有省份的名称,可以先在数据库中将全国所有省份建一个表"; List<String> list=bd.getSQLList(sql); //创建第一个list1,里面存入省份名称少于4个字的 List<String> list1=new ArrayList<String>(); //创建第二个list2,里面存入省份名称大于4个字的 List<String> list2=new ArrayList<String>(); if(list!=null&&list.size()>0){ for (String string : list) { if(string.length()>4){ list2.add(string); }else{ list1.add(string); } } } int keyToken = 0; int toIndex=3; List<Object> listSortMap = new ArrayList<Object>(); for(int i = 0;i<list1.size();i+=3){ if(i+3>list1.size()){ //作用为toIndex最后没有3条数据则剩余几条newList中就装几条 toIndex=list1.size()-i; } List newList = list1.subList(i,i+toIndex); keyToken++; listSortMap.add(newList); } int keyToken2 = 0; List<Object> listSortMap2 = new ArrayList<Object>(); for(int i = 0;i<list2.size();i+=2){ if(i+2>list2.size()){ //作用为toIndex最后没有2条数据则剩余几条newList中就装几条 toIndex=list2.size()-i; } List newList2 = list2.subList(i,i+toIndex); keyToken2++; listSortMap2.add(newList2); } map.put("prov1", listSortMap); map.put("prov2", listSortMap2); return map; }
页面接收
function campus_list_ByProvence(req_data){ $.ajax({ type : "POST", url : "刚才方法的url", traditional: true, data : req_data, timeout : 20000, cache : false, beforeSend : function(XMLHttpRequest) {}, success : function(data) { /* 生成html */ $('#campus_list_provence').html(campus_list_ByProvenceHtml1(data.prov1,data.prov2)); }, error : function() { alert("ajax失败!");} }); } function campus_list_ByProvenceHtml1(prov,prov2){ var html = ""; for(var i = 0 , j = prov.length ; i < j ; i++){ html+='<li class="li1" >'; if(prov[i][0]!=undefined&&prov[i][0]!=null&&prov[i][0]!=""){ var provstr0="'"+prov[i][0]+"'"; html+='<a href="#2" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="GoToUrlItem('+provstr0+');">'+prov[i][0]+'</a>'; }else{ html+=''; } if(prov[i][1]!=undefined&&prov[i][1]!=null&&prov[i][1]!=""){ var provstr1="'"+prov[i][1]+"'"; html+='<a href="#2" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="GoToUrlItem('+provstr1+');">'+prov[i][1]+'</a>'; }else{ html+=''; } if(prov[i][2]!=undefined&&prov[i][2]!=null&&prov[i][2]!=""){ var provstr2="'"+prov[i][2]+"'"; html+='<a href="#2" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="GoToUrlItem('+provstr2+');">'+prov[i][2]+'</a>'; }else{ html+=''; } html+='</li>'; } for(var i = 0 , j = prov2.length ; i < j ; i++){ html+='<li class="li2" >'; if(prov2[i][0]!=undefined&&prov2[i][0]!=null&&prov2[i][0]!=""){ var pro2vstr0="'"+prov2[i][0]+"'"; html+='<a href="#2" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="GoToUrlItem('+pro2vstr0+');">'+prov2[i][0]+'</a>'; }else{ html+=''; } if(prov2[i][1]!=undefined&&prov2[i][1]!=null&&prov2[i][1]!=""){ var prov2str1="'"+prov2[i][1]+"'"; html+='<a href="#2" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="GoToUrlItem('+prov2str1+');">'+prov2[i][1]+'</a>'; }else{ html+=''; } html+='</li>'; } return html ; }
感觉这样写好麻烦,不过最后还好弄出来了,如果有更好的方法请告诉我一下
页面展示:
以上这篇java8 集合 多字段 分组 统计个数代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
加载全部内容