两个自定义对象List列表取交集(intersection)

本文展示了一个使用Java实现的两个包含Fpxx对象的列表的交集操作示例。Fpxx对象通过其fpdm和fphm属性确定唯一性。通过自定义equals和hashCode方法确保了对象比较的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    public static void main(String[] args) {
        List<Fpxx> list = ListUtils.intersection(getFpList1(), getFpList2());
        
        for(Fpxx fp:list){
            System.out.println(fp);
        }
        
        System.out.println("size:"+list.size());
    }
    
    
    public static List<Fpxx> getFpList1() {
        List<Fpxx> list = new ArrayList<Fpxx>();
        for(int i=100000;i<200000;i++){
            Fpxx fp = new Fpxx();
            fp.fpdm = "0223678";
            fp.fphm = "3"+i;
            
            list.add(fp);
        }
        
        return list;
    }
    
    public static List<Fpxx> getFpList2() {
        List<Fpxx> list = new ArrayList<Fpxx>();
        for(int i=199900;i<199910;i++){
            Fpxx fp = new Fpxx();
            fp.fpdm = "0223678";
            fp.fphm = "3"+i;
            
            list.add(fp);
        }
        
        return list;
    }
}

class Fpxx {
    public String fpdm;
    public String fphm;

    @Override
    public int hashCode() {
        int hash = 3;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Fpxx other = (Fpxx) obj;
        if ((this.fpdm == null) ? (other.fpdm != null) : !this.fpdm.equals(other.fpdm)) {
            return false;
        }
        if ((this.fphm == null) ? (other.fphm != null) : !this.fphm.equals(other.fphm)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Fpxx{" + "fpdm=" + fpdm + ", fphm=" + fphm + '}';
    }
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值