2016-09-20 7 views
0

2つのリストの内容、特に両方のリストに格納されているカスタムオブジェクトの特定のメソッドを比較したい場合は、#getID()としましょう。2つのリストの内容を比較する

私のアプローチは、これら2つのリストに同じIDを持つ同じエントリが含まれているかどうかを知る方法を持つことです。

List<CustomObject> firstList; 
List<CustomObject> secondList; 

firstList内の各エントリをループその上のgetId()を呼び出すとのgetId(同じ値を返すsecondList内のオブジェクトがある場合に見えます)。

このチェックを効率的に行うにはどうすればよいですか? Java 8ストリームを使用することをお勧めします。両方のリストがソートされている場合

答えて

1

あなたはそれを行うことができます。

public boolean equalsLists(List<YourClass> a, List<YourClass> b) { 
    if (a.size() != b.size()) { 
     return false; 
    } 
    for (int i = 0; i < a.size(); i++) { 
     if (!a.get(i).getId().equals(b.get(i).getId()) { 
      return false; 
     } 
    } 
    return true; 
} 

あなたは、要素を並べ替え、それらをソートし、前に示したものと同じアルゴリズムを適用することができます。

そうでなければ、あなたのアルゴリズムの効率は多くの要因に依存に:

  • 二つのリストがいかに多くの場合は、要素が中に1回以上存在することができる場合はリスト
  • あるどのくらい
  • に等しいです。リスト
  • 2つのリストが等しくない場合、すべての要素に関数を適用して、妥当な安全性を知ることができれば、この関数を要素ごとに比較する前に使用することができます(たとえば、麻痺を数えるそれぞれのリストの各IDの文字列が異なる場合は、falseを返すことができることを確認してください)
1

両方のリストがソートされていると仮定すると、comparatorを使用できます。

 Comparator<List<CustomObject>> myComp = new Comparator<List<CustomObject>>() { 
      @Override 
      public int compare(List<CustomObject> customObjectList, List<CustomObject> t1) { 
       for (int i = 0; i < customObjectList.size(); i++) { 
        if (customObjectList.getId().equals(t1.getId())) { 
         // Do your logic here 
        } 
       } 
      } 
     }; 

     myComp.compare(list1, list2); 

これは「Java」のやり方です。リストのサイズが一致しない場合は、何らかの防御プログラミングを実践する必要があります。ソートされていない場合は、これら2つのリストを比較する前にCollections.sortを使用できます。

+0

私はそれを言及すべきでした、それは文字列です! – manuelgu

+0

私はそれに応じて私の答えを編集します。 – Weava

0

繰り返し要素を取得する1つのアプローチは、次のようにMapを使用しています。

import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 
import java.util.Set; 
import java.util.function.Function; 
import java.util.stream.Collectors; 

public class SomeClass { 
    protected class Component { 
     public String getId() { 
      return id; 
     } 

     public void setId(String id) { 
      this.id = id; 
     } 

     public String getDescription() { 
      return description; 
     } 

     public void setDescription(String description) { 
      this.description = description; 
     } 

     public Component(String id, String description) { 
      super(); 
      this.id = id; 
      this.description = description; 
     } 

     String id; 
     String description; 

     @Override 
     public String toString() { 
      // TODO Auto-generated method stub 
      return "Id " + this.getId(); 
     } 

    } 

    public static void main(String[] args) { 

     Component component1 = new SomeClass().new Component("1", "One"); 
     Component component2 = new SomeClass().new Component("2", "One"); 
     Component component3 = new SomeClass().new Component("1", "One"); 
     Component component4 = new SomeClass().new Component("3", "Three"); 
     Component component5 = new SomeClass().new Component("4", "Four"); 
     List<Component> list1 = new ArrayList<Component>(); 
     list1.add(component1); 
     list1.add(component2); 

     List<Component> list2 = new ArrayList<Component>(); 
     list1.add(component1); 
     list1.add(component2); 

     list2.add(component3); 
     list2.add(component4); 
     list2.add(component5); 

     Map<String, Component> result = list2.stream().collect(Collectors.toMap(Component::getId, Function.identity())); 

     Set<Component> repeated = list1.stream().filter(component -> result.containsKey(component.getId())) 
       .collect(Collectors.toSet()); 
     repeated.forEach(System.out::println); 
    } 

} 
関連する問題