List集合按某个属性或者字段进行分组

package com.cetcnav.lbs.pis.stats.entity;
/**
 * 
 * @author wp
 * 2020年11月12日
 *
 */
public class UserInfo implements java.io.Serializable {

	private static final long serialVersionUID = 7902191860101550571L;
	
    private String username;
    private double fRate;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public double getfRate() {
		return fRate;
	}
	public void setfRate(double fRate) {
		this.fRate = fRate;
	}
	public UserInfo(String username, double fRate) {
		super();
		this.username = username;
		this.fRate = fRate;
	}
	@Override
	public String toString() {
		return "UserInfo [username=" + username + ", fRate=" + fRate + "]";
	}
}

 public static void main(String[] args) throws Exception {

        ListUtilsTest testObj = new ListUtilsTest();

        List<UserInfo> list = new ArrayList<UserInfo>();

        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
        UserInfo user1 = new UserInfo("a",7.5d);
        UserInfo user2 = new UserInfo("a",5.5d);
        UserInfo user3 = new UserInfo("a",9.5d);
        UserInfo user4 = new UserInfo("b",2.5d);
        UserInfo user5 = new UserInfo("b",9.5d);
        UserInfo user6 = new UserInfo("d",5.5d);
        UserInfo user7 = new UserInfo("d",4.5d);
        UserInfo user8 = new UserInfo("d",9.9d);
        UserInfo user9 = new UserInfo("d",8.5d);

        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        list.add(user6);
        list.add(user7);
        list.add(user8);
        list.add(user9);        
        
        Map<String, List<UserInfo>> collect = list.stream().collect(Collectors.groupingBy(UserInfo::getUsername));
		for(String key:collect.keySet()){
			System.out.println(key+":" +collect.get(key).size());
			System.out.println(collect.get(key));
		}
	}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值