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 {
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 = 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 set = new HashSet();
set.addAll(listTest);
Iterator 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());
}
}
}
输入结果: