2016-08-09 39 views
-4

addAll Werke Nachweis aber removeAll nicht einmal ähnliche Werte sowohl in der Array-ListeListe Array Nicht eindeutigen Wert

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

Sicherstellen, dass 'Student' '' '' ''' sinnvoll' 'überschreibt. – Mena

+0

ok ich werde überprüfen – VigneshWaran

Antwort

0

Sie haben nicht funktioniert eine Hash Set stattdessen verwenden könnte, ist es entworfen, um keine Duplikate zu erlauben.

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

danke für deine antwort ich probiere Hashset auch, aber ich kann nicht entfernen AddAll funktioniert, aber entfernenAll nicht funktioniert – VigneshWaran

0

Endlich habe ich die Lösung. zeigt ähnliche Elemente in zwei 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() + " ]"); 
     } 
    } 

}