1.实体
@Data
public class MerchantShopUserVO {
@ApiModelProperty(value = "用户名")
private Long userId;
@ApiModelProperty(value = "用户名")
private String username;
@ApiModelProperty(value = "电话")
private String mobile;
@ApiModelProperty("真实姓名")
private String realName;
@ApiModelProperty("门店id")
private Long shopId;
}
2.工具类
public class Utils {
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> concurrentHashMap = new ConcurrentHashMap<>();
return t -> concurrentHashMap.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}
3.集合操作
//根据集合中的userId去掉重复的用户(由于不同门店下会有相同的员工导致的重复)
List<MerchantShopUserVO> volist = new ArrayList();
Integer i = 0;
while(i < 10){
MerchantShopUserVO vo = new MerchantShopUserVO();
vo.setMobile("1548766545");
vo.setRealName("张山" + i);
vo.setShopId(1L + i);
vo.setUserId(12L + i);
vo.setUsername("张三" + i);
volist.add(vo);
i++;
}
System.out.println("volist:" + volist);
//产生相同userId的数据
MerchantShopUserVO vo = new MerchantShopUserVO();
vo.setMobile("1548766545");
vo.setRealName("张山" + i);
vo.setShopId(1L + i);
vo.setUserId(12L);
vo.setUsername("张三" + i);
volist.add(vo);
System.out.println("volist:" + volist);
//根据集合中的userId去掉重复的用户(由于不同门店下会有相同的员工导致的重复)
List<MerchantShopUserVO> distinctList = volist
.stream()
.filter(Utils.distinctByKey(MerchantShopUserVO::getUserId))
.collect(Collectors.toList());
//筛选shopId不等于2L,且id不相同的所有id
List<Long> collect = volist.stream().map(MerchantShopUserVO::getShopId).filter(e -> e != 2L).collect(Collectors.toList());
System.out.println("distinctList:" + distinctList);
System.out.println("collect:" + collect);
4.结果展示
volist:[MerchantShopUserVO(userId=12, username=张三0, mobile=1548766545, realName=张山0, shopId=1), MerchantShopUserVO(userId=13, username=张三1, mobile=1548766545, realName=张山1, shopId=2), MerchantShopUserVO(userId=14, username=张三2, mobile=1548766545, realName=张山2, shopId=3), MerchantShopUserVO(userId=15, username=张三3, mobile=1548766545, realName=张山3, shopId=4), MerchantShopUserVO(userId=16, username=张三4, mobile=1548766545, realName=张山4, shopId=5), MerchantShopUserVO(userId=17, username=张三5, mobile=1548766545, realName=张山5, shopId=6), MerchantShopUserVO(userId=18, username=张三6, mobile=1548766545, realName=张山6, shopId=7), MerchantShopUserVO(userId=19, username=张三7, mobile=1548766545, realName=张山7, shopId=8), MerchantShopUserVO(userId=20, username=张三8, mobile=1548766545, realName=张山8, shopId=9), MerchantShopUserVO(userId=21, username=张三9, mobile=1548766545, realName=张山9, shopId=10)]
volist:[MerchantShopUserVO(userId=12, username=张三0, mobile=1548766545, realName=张山0, shopId=1), MerchantShopUserVO(userId=13, username=张三1, mobile=1548766545, realName=张山1, shopId=2), MerchantShopUserVO(userId=14, username=张三2, mobile=1548766545, realName=张山2, shopId=3), MerchantShopUserVO(userId=15, username=张三3, mobile=1548766545, realName=张山3, shopId=4), MerchantShopUserVO(userId=16, username=张三4, mobile=1548766545, realName=张山4, shopId=5), MerchantShopUserVO(userId=17, username=张三5, mobile=1548766545, realName=张山5, shopId=6), MerchantShopUserVO(userId=18, username=张三6, mobile=1548766545, realName=张山6, shopId=7), MerchantShopUserVO(userId=19, username=张三7, mobile=1548766545, realName=张山7, shopId=8), MerchantShopUserVO(userId=20, username=张三8, mobile=1548766545, realName=张山8, shopId=9), MerchantShopUserVO(userId=21, username=张三9, mobile=1548766545, realName=张山9, shopId=10), MerchantShopUserVO(userId=12, username=张三10, mobile=1548766545, realName=张山10, shopId=11)]
distinctList:[MerchantShopUserVO(userId=12, username=张三0, mobile=1548766545, realName=张山0, shopId=1), MerchantShopUserVO(userId=13, username=张三1, mobile=1548766545, realName=张山1, shopId=2), MerchantShopUserVO(userId=14, username=张三2, mobile=1548766545, realName=张山2, shopId=3), MerchantShopUserVO(userId=15, username=张三3, mobile=1548766545, realName=张山3, shopId=4), MerchantShopUserVO(userId=16, username=张三4, mobile=1548766545, realName=张山4, shopId=5), MerchantShopUserVO(userId=17, username=张三5, mobile=1548766545, realName=张山5, shopId=6), MerchantShopUserVO(userId=18, username=张三6, mobile=1548766545, realName=张山6, shopId=7), MerchantShopUserVO(userId=19, username=张三7, mobile=1548766545, realName=张山7, shopId=8), MerchantShopUserVO(userId=20, username=张三8, mobile=1548766545, realName=张山8, shopId=9), MerchantShopUserVO(userId=21, username=张三9, mobile=1548766545, realName=张山9, shopId=10)]
collect:[1, 3, 4, 5, 6, 7, 8, 9, 10, 11]