Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除列表中的一些重复项_Java_List_Collections_Unique_Items - Fatal编程技术网

Java 删除列表中的一些重复项

Java 删除列表中的一些重复项,java,list,collections,unique,items,Java,List,Collections,Unique,Items,我使用的java对象如下: public class GeoName { private String country; private String city; private float lat; private float lon; } Madrid, Spain, ... London, England, ... Madrid, Mexico, ... Paris, France, ... 我收到一份GeoName列表,我希望尽可能高效地删除列表中同一国家的重

我使用的java对象如下:

public class GeoName {
   private String country;
   private String city;
   private float lat;
   private float lon;
}
Madrid, Spain, ...
London, England, ...
Madrid, Mexico, ...
Paris, France, ...
我收到一份GeoName列表,我希望尽可能高效地删除列表中同一国家的重复城市。我的意思是,如果我收到以下列表:

Madrid, Spain, ...
London, England, ...
Madrid, Mexico, ...
London, England, ...
Paris, France, ...
Madrid, Spain, ...
我想删除重复的项目(城市+国家),直到列表如下所示:

public class GeoName {
   private String country;
   private String city;
   private float lat;
   private float lon;
}
Madrid, Spain, ...
London, England, ...
Madrid, Mexico, ...
Paris, France, ...
我正在努力,但我不知道怎么做

有什么想法吗

谢谢

PS:我不能使用集合,因为我发现一个城市的名称在不同纬度和经度的国家重复出现(这很奇怪,但它们确实存在)。因此,在集合

< p>中不会是一个等值的项目。您可以实现HASCODE()和GeoName(< /代码>)的均衡器()。
@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()
将所有地理名称放入。重复项将自动高效地分拣

    List<GeoName> myInputList = ...;
    Set<GeoName> geoSet = new HashSet<>(myInputList);
List myInputList=。。。;
Set geoSet=newhashset(myInputList);
这应该可以做到:

我使用修改后的.equals方法创建类,然后使用said.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");
            }
    }
}

然后,您将在数组中循环并检查所有数组,如果它不存在,则将其添加到数组中,我将其留给您。

这是一个完整的示例:

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);
        }          
    }    
}

要从自定义数据集合(例如GeoName)中删除重复项,请实现equals()和hashcode()方法

然后将数据添加到集合中以删除重复条目


根据您的逻辑实现equals()和hashcode(),以识别重复数据。

嗨,到目前为止您做了哪些尝试?使用
集合
而不是数组。我昨天回答了一个类似的问题,结果它被删除了,因为我发现同一个国家的一些城市具有不同的纬度和经度(lat,lon属性)。能否使用哈希集,并将该对象的哈希代码定义为仅使用国家和城市名称?您不应该这样做。您必须将每个实例与另一个实例进行相等比较,从而导致
O(n^2)
复杂性。使用哈希集更通用、更清晰,并且在
O(n)中运行
。小规模也可以。