2017-07-11 51 views
3

我使用的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集合,因爲我找到了一個城市在不同經度和緯度的國家中重複的名稱(這很奇怪,但它們存在)。所以它不會是一個完全平等的項目集合

+0

嗨,你到目前爲止嘗試過什麼? –

+1

使用'Set'而不是數組。 – victor

+0

昨天我回答了這樣一個問題,它被刪除T_T – Daedric

回答

1

你可以實現只考慮國家和城市的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); 
1

這應做到:

我創建類與修改.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 

這樣,你會遍歷數組,並檢查所有這些,如果它不存在,那麼你將它添加到陣列,我把它留給你。

+0

你不應該這樣做。您必須將每個實例與另一個實例進行比較,從而導致「O(n^2)」複雜性。使用HashSet更通用,更清潔,並在'O(n)'中運行。 – A1m

+0

對於小規模來說這很好。 – Daedric

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 
2

對於從自定義數據(如GeoName)的集合中移除重複的條目實現了equals()和hashCode()方法。

然後將數據添加到Set中以刪除重複條目。

根據您的邏輯實現equals()和hashcode()以識別重複數據。