java list 根据对象一个字段去重

本文介绍了一种通过重写equals和hashCode方法实现Java对象去重的方法。以PullThroughAnalysisUser类为例,展示了如何根据value1属性进行对象去重的具体实现。

1.主要思路就是根据从写equals 以及 hashCode 方法。

代码如下:

package com.bfd.unibase.modules.dataview.entity;

import org.hibernate.validator.constraints.Length;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonFormat;

import javax.validation.constraints.NotNull;

import com.bfd.unibase.common.persistence.DataEntity;

/**
 * test
 * @author kui.peng
 * @version 2015-10-22
 */
public class PullThroughAnalysisUser extends DataEntity<PullThroughAnalysisUser> {
    
    private static final long serialVersionUID = 1L;
    
    private String value1;        //
    private String value2;        //
    
    
    public String getValue1() {
        return value1;
    }
    public void setValue1(String value1) {
        this.value1 = value1;
    }
    public String getValue2() {
        return value2;
    }
    public void setValue2(String value2) {
        this.value2 = value2;
    }
    @Override  
    public boolean equals(Object obj) {  
        PullThroughAnalysisUser s=(PullThroughAnalysisUser)obj;   
        return  value1.equals(s.value1);  

// 根据value1 属性去重    }  
    @Override  
    public int hashCode() {  
        String in = value1;  
        return in.hashCode();  
    }
    
    public static void main(String[] args) {
        List< PullThroughAnalysisUser> listTest = new ArrayList<PullThroughAnalysisUser>();
        
        PullThroughAnalysisUser pullThroughAnalysisUser = new PullThroughAnalysisUser();
        pullThroughAnalysisUser.setValue1("wwww&mmmm");
        
        pullThroughAnalysisUser.setValue2("2222");
        
        PullThroughAnalysisUser pullThroughAnalysisUser2 = new PullThroughAnalysisUser();
        pullThroughAnalysisUser2.setValue1("wwww&mmmm");
        pullThroughAnalysisUser2.setValue2("3333");
        
        PullThroughAnalysisUser pullThroughAnalysisUser3 = new PullThroughAnalysisUser();
        pullThroughAnalysisUser3.setValue1("mmm");
        pullThroughAnalysisUser3.setValue2("3333");
        listTest.add(pullThroughAnalysisUser3);
        listTest.add(pullThroughAnalysisUser2);
        listTest.add(pullThroughAnalysisUser);
        
        
        
        Set<PullThroughAnalysisUser> set = new HashSet<PullThroughAnalysisUser>();  
        set.addAll(listTest);  
        Iterator<PullThroughAnalysisUser> it = set.iterator();
        
        /*while(it.hasNext()){
            
            System.out.println("00000000");
            System.out.println(it.next().getValue1());
            System.out.println(it.next().getValue2());
        }*/
         for (PullThroughAnalysisUser student : set) {  
                System.out.println(student.getValue1()+"-"+student.getValue2());  
            }  
        
    }
    
    
    
}





输入结果:


Java中,若需要根据对象列表的某个字段复项,可以通过Java 8引入的Stream API实现。具体方式是使用`Collectors.collectingAndThen`方法配合`TreeSet`或自定义逻辑来完成操作。 以下是一个通用的实现方法:假设有一个名为`Student`的对象类,并且希望根据其`age`字段进行: ```java studentList = studentList.stream() .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getAge))), ArrayList::new )); ``` 上述代码的工作原理如下: - `Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getAge)))`部分负责将流中的元素收集到一个`TreeSet`中,该`TreeSet`通过比较器按照`Student`对象的`age`字段排序并自动。 - `Collectors.collectingAndThen(..., ArrayList::new)`则确保最终结果被转换为一个`ArrayList`[^2]。 如果对象的属性值是可变的,并且希望基于这些属性的最新值进行,则应首先对列表进行排序,以保证时保留的是最新的数据[^1]。 对于更复杂的场景,例如需要根据多个字段(如`DeptType`和`UserPosition`)组合进行的情况,可以采用类似的方法,但需构造一个能够唯一标识这两个字段组合的比较逻辑: ```java list.stream() .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getDeptType() + ";" + o.getUserPosition()))), ArrayList::new )); ``` 此代码片段中,`o.getDeptType() + ";" + o.getUserPosition()`创建了一个字符串键,用于表示两个字段的组合值,并据此执行操作[^4]。 此外,当需要处理不同列表之间的合并与时,例如方法`deDuplicationList`所示的例子,可以通过先合并两个列表再应用相同的流处理技术来达到目的: ```java public static List<TestBean> deDuplicationList(List<TestBean> list1, List<TestBean> list2) { list1.addAll(list2); return list1.stream() .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TestBean::getName))), ArrayList::new )); } ``` 在这个例子中,`TestBean`对象的`name`字段被用来作为的标准[^3]。 总之,利用Java 8的Stream API,可以根据对象的单个或多个字段灵活地实现列表功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值