List stream对象去重

核心:

List<AreaAging> unique = newDetails.stream().collect(
		           Collectors. collectingAndThen(
		                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getArriveProvince() + ";" + o.getHairCity()))), ArrayList::new)
		);

示例:

AreaAging 实体

List<AreaAging> newDetails = Lists.newArrayList();
		AreaAging temp1 = new AreaAging();
		temp1.setArriveProvince("四川省");
		temp1.setHairCity("成都市");
		newDetails.add(temp1);
		
		AreaAging temp2 = new AreaAging();
		temp2.setArriveProvince("四川省");
		temp2.setHairCity("成都市2");
		newDetails.add(temp2);

		System.out.println(JSON.toJSONString(newDetails));
		// 根据name,sex两个属性去重
		// 根据name去重
		


		List<AreaAging> unique = newDetails.stream().collect(
		           Collectors. collectingAndThen(
		                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getArriveProvince() + ";" + o.getHairCity()))), ArrayList::new)
		);

		System.out.println(JSON.toJSONString(unique));

猜你喜欢

转载自blog.csdn.net/qq_16843563/article/details/123090610