2016-08-09 4 views
-4

addAll作品以外のユニークな値を検出するが、でも、あなたが代わりにHash Setを使用することができ、両方の配列リストで同様の値リスト・アレイは

List<Student> sourceList = new ArrayList<Student>(students); 
    List<Student> destinationList = new ArrayList<Student>(students1); 

    sourceList.removeAll(students1); 
    destinationList.removeAll(students); 
    sourceList.addAll(destinationList); 

    for (Student student : sourceList) { 
     System.out.println("SIM:::Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]"); 
    } 
+1

「学生」が「同等」を意味するように上書きするようにします。 – Mena

+0

ok私は – VigneshWaran

答えて

0

を持ってremoveAll動作しません、重複を許可しないように設計されています。

import java.util.*; 

public class HashSetDemo { 

    public static void main(String args[]) { 
     // create a hash set 
     HashSet<String> hs = new HashSet<String>(); 

     // add elements to the hash set 
     hs.add("B"); 
     hs.add("A"); 
     hs.add("C"); 

     // trying to add duplicate elements to the hash set 
     hs.add("C"); 
     hs.add("B"); 
     hs.add("A"); 

     // Only stores one of each, and orders them. 
     System.out.println(hs); // [A, B, C] 
    } 
} 
+0

あなたの答えをありがとう、私もハッシュセットを試してみますが、私はcan not addAllは動作しますが、removeAllは機能しません – VigneshWaran

0

私は解決策を得ました。 同様の項目を2つのlistArrayに表示します。

private void findsimalarity() { 

    List<Student> sourceList = new ArrayList<>(); 
    List<Student> destinationList = new ArrayList<>(); 

    sourceList.addAll(students); 
    destinationList.addAll(students1); 

    destinationList.removeAll(sourceList); 

    sourceList.removeAll(destinationList); 

    if (sourceList.size() >= destinationList.size()) { 
     //destination list contains similar items 
     for (Student student : destinationList) { 
      System.out.println("SIMILAR:::Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]"); 
     } 
    } else { 
     //source list contains similar items 
     for (Student student : sourceList) { 
      System.out.println("SIMILAR:::Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]"); 
     } 
    } 

}