去除list重复的对象
//实体类
import lombok.Data;
@Data
public class IpData {
private String ip;
private String country;
public IpData(String ip, String country) {
this.ip = ip;
this.country = country;
}
public IpData( ) {
}
}
//测试方法
public void test(){
Set<IpData> set = new HashSet<IpData>();
set.add(new IpData("1","你"));
set.add(new IpData("2","我"));
set.add(new IpData("3","他"));
set.add(new IpData("3","他1"));
// 重复数据添加失败
set.add(new IpData("1","你"));
set.add(new IpData("2","我"));
for(IpData s: set){
System.out.println(s.getIp()+"--"+s.getCountry());
}
}
//测试方法
private void setTest() {
Set<String> set = new HashSet<String>();
set.add("JAVA");
set.add("C");
set.add("C++");
// 重复数据添加失败
set.add("JAVA");
set.add("JAVASCRIPT");
// 使用增强for循环遍历set集合
for(String s: set){
System.out.println(s);
}
}