Java8 List去重(单属性或组合属性)

在现有的业务场景中,经常涉及到很多应用需要针对集合内容进行去重筛选,并且涉及到对象中的组合去重条件,Java8提供的流处理就非常方便,mark 一下网上的例子,供大家学习使用,且做个记录!

栗子如下:

package com.demo.jdk8.collectors;

import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * @author Shanks
 * @date 2018-10-15
 */
public class CollectingAndThenTest {

    @Test
    void testCollectingAndThen() {

        UserOrder order0 = new UserOrder("年会", "张三", "box0001");
        UserOrder order1 = new UserOrder("婚庆", "张三", "box0002");
        UserOrder order2 = new UserOrder("旅游", "李四", "box0003");
        UserOrder order3 = new UserOrder("发布会", "张三", "box0002");
        UserOrder order4 = new UserOrder("旅游", "李四", "box0004");

        List<UserOrder> list = Lists.newArrayList(order0, order1, order2, order3, order4);

        System.out.println("=====>只根据单一属性photographerName进行去重");
        // <R, A> R collect(Collector<? super T, A, R> collector);
        // collectingAndThen(Collector<T, A, R> downstream, Function<R, RR> finisher)
        // toCollection(Supplier<C> collectionFactory)
        ArrayList<UserOrder> distinctByName = list.stream()
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserOrder::getPhotographerName))),
                                userOrders -> new ArrayList<>(userOrders)
                        )
                );
        distinctByName.forEach(System.out::println);

        System.out.println("=====>同时根据photographerName和deviceCode两个属性进行去重");
        ArrayList<UserOrder> distinctByNameAndCode = list.stream()
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(userOrder -> userOrder.getPhotographerName() + userOrder.getDeviceCode()))),
                                ArrayList::new
                        )
                );
        distinctByNameAndCode.forEach(System.out::println);
    }
}

打印结果:

=====>只根据单一属性photographerName进行去重
UserOrder{shootingTheme='年会', photographerName='张三', deviceCode='box0001'}
UserOrder{shootingTheme='旅游', photographerName='李四', deviceCode='box0003'}

=====>同时根据photographerName和deviceCode两个属性进行去重
UserOrder{shootingTheme='年会', photographerName='张三', deviceCode='box0001'}
UserOrder{shootingTheme='婚庆', photographerName='张三', deviceCode='box0002'}
UserOrder{shootingTheme='旅游', photographerName='李四', deviceCode='box0003'}
UserOrder{shootingTheme='旅游', photographerName='李四', deviceCode='box0004'}

实体类:

package com.demo.jdk8.collectors;

/**
 * @author Shanks
 * @date 2018-10-16
 */
public class UserOrder {

    /**
     * 拍摄主题
     */
    private String shootingTheme;
    /**
     * 摄影师
     */
    private String photographerName;
    /**
     * 设备编号
     */
    private String deviceCode;

    public UserOrder() {
    }

    public UserOrder(String shootingTheme, String photographerName, String deviceCode) {
        this.shootingTheme = shootingTheme;
        this.photographerName = photographerName;
        this.deviceCode = deviceCode;
    }

    @Override
    public String toString() {
        return "UserOrder{" +
                "shootingTheme='" + shootingTheme + '\'' +
                ", photographerName='" + photographerName + '\'' +
                ", deviceCode='" + deviceCode + '\'' +
                '}';
    }

    public String getShootingTheme() {
        return shootingTheme;
    }

    public void setShootingTheme(String shootingTheme) {
        this.shootingTheme = shootingTheme;
    }

    public String getPhotographerName() {
        return photographerName;
    }

    public void setPhotographerName(String photographerName) {
        this.photographerName = photographerName;
    }

    public String getDeviceCode() {
        return deviceCode;
    }

    public void setDeviceCode(String deviceCode) {
        this.deviceCode = deviceCode;
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值