在列表中删除一些重复的项目
问题描述:
我使用的Java对象是这样的:在列表中删除一些重复的项目
public class GeoName {
private String country;
private String city;
private float lat;
private float lon;
}
我收到GeoName的名单,我想删除在同一个国家,在列表中重复的城市,尽可能高效。我的意思是,如果我收到下面的列表:
Madrid, Spain, ...
London, England, ...
Madrid, Mexico, ...
London, England, ...
Paris, France, ...
Madrid, Spain, ...
我想删除重复项(城市+国家),直到名单是这样的:
Madrid, Spain, ...
London, England, ...
Madrid, Mexico, ...
Paris, France, ...
我的工作就可以了但我不知道该怎么做!
有什么想法吗?
谢谢!
PS:我不能使用Set集合,因为我找到了一个城市在不同经度和纬度的国家中重复的名称(这很奇怪,但它们存在)。所以它不会是一个完全平等的项目集合
答
你可以实现只考虑国家和城市的GeoName
的hashCode()和equals()。
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
GeoName geoName = (GeoName) o;
if (!country.equals(geoName.country))
return false;
return city.equals(geoName.city);
}
@Override
public int hashCode() {
int result = country.hashCode();
result = 31 * result + city.hashCode();
return result;
}
后,您可以使用一个HashSet()
把所有GeoNames的英寸重复将被自动高效地整理出来。
List<GeoName> myInputList = ...;
Set<GeoName> geoSet = new HashSet<>(myInputList);
答
这应做到:
我创建类与修改.equals方法,然后检查是否是类的2个测试实例是相同的使用说.equals方法。
class GeoName {
private String country;
private String city;
public GeoName(String country, String city) {
this.country = country;
this.city = city;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final GeoName other = (GeoName) obj;
if (!Objects.equals(this.country, other.country)) {
return false;
}
if (!Objects.equals(this.city, other.city)) {
return false;
}
return true;
}
}
测试类:
public class Cities {
public static void main(String[] args) {
// ArrayList<GeoName> geos = new ArrayList<>();
GeoName test = new GeoName("Madrid", "Spain");
GeoName test1 = new GeoName("Madrid", "Mexico");
if (test.equals(test)) {
System.out.println("True 1");
}
if (test.equals(test1)) {
System.out.println("True 2");
}
}
}
输出:
True 1
这样,你会遍历数组,并检查所有这些,如果它不存在,那么你将它添加到阵列,我把它留给你。
答
这是一个完整的例子:
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class GeoName {
private String country, city;
private float lat, lon;
public GeoName(String country, String city, float lat, float lon){
this.country = country;
this.city = city;
this.lat = lat;
this.lon = lon;
}
@Override
public boolean equals(Object other){
if(other==null) return false;
if(other instanceof GeoName){
return ((GeoName)other).city.equals(this.city) &&
((GeoName)other).country.equals(this.country);
}
return false;
}
@Override
public String toString(){
return city + ", "+ country +
", " + lat +", " + lon;
}
@Override
public int hashCode(){
return Objects.hash(country, city);
}
// to test
public static void main(String[] args) {
List<GeoName> list = new ArrayList<>();
list.add(new GeoName("Madrid", "Spain",1.0f, 2.0f));
list.add(new GeoName("England", "London",3.0f, 4.0f));
list.add(new GeoName("England", "London",3.0f, 4.0f));
list.add(new GeoName("France", "Paris",7.0f, 9.0f));
list.add(new GeoName("Mexico", "Madrid",9.0f, 10.0f));
Set<GeoName> set = new HashSet<>(list);
for(GeoName geoName : set){
System.out.println(geoName);
}
}
}
输出:
London, England, 3.0, 4.0
Madrid, Mexico, 9.0, 10.0
Paris, France, 7.0, 9.0
Spain, Madrid, 1.0, 2.0
答
对于从自定义数据(如GeoName)的集合中移除重复的条目实现了equals()和hashCode()方法。
然后将数据添加到Set中以删除重复条目。
根据您的逻辑实现equals()和hashcode()以识别重复数据。
嗨,你到目前为止尝试过什么? –
使用'Set'而不是数组。 – victor
昨天我回答了这样一个问题,它被删除T_T – Daedric