2012-07-25 5 views
0

에서 중복 물체를 제거 나는 자바 클래스가;ArrayList를

Students students1 = new Students("har","mat","harmat"); 
    Students students2 = new Students("pan","son","panson"); 
    Students students3 = new Students("yogi","jos","yogijos"); 

    Students students4 = new Students("har","mat","harmat"); 
    Students students5 = new Students("pan","son","harmat"); 
    Students students6 = new Students("yogi","jos","someotherUName"); 
    Students students7 = new Students("yogi","jos","someotherUName2"); 

지금 모든 개체가 나는 다음과 같은 기준에 따라이 combinedList에서 중복 개체를 제거 할 combinedList

List combinedList = new ArrayList<SchoolStudents>(); 

에 추가됩니다; fName이 같거나 uName이 동일한 경우 그 두 학생이 동등한 경우 결정하는 일반적인 기준 인 경우

+0

[ArrayList에 중복 값 찾기]의 중복 가능성 (http://stackoverflow.com/questions/7281352/finding-duplicate-values-in-arraylist) –

+0

그리고 더 많은 중복에 http : // 유래 .com/questions/4778260/how-to-remove-arraylist-duplicate-values, http://stackoverflow.com/questions/9962082/prevent-duplicate-entries-in-arraylist, http://stackoverflow.com/questions/5181821/~에서 피하기 위해 중복 된 항목 -에 - arraylist, http://stackoverflow.com/questions/5754592/finding-index-of-duplicate-values-in-an-arraylist, http : //stackoverflow.com/questions/5503646/find-duplicate-objects-in-an-java-arraylist –

+0

숙제 ?? 지금까지 뭐 해봤 어? 힌트 : comparable()를 사용하고 어쩌면 설정하십시오 – maasg

답변

4

, 당신은이 명 학생이 당신의 기준에 따라도 같을 확인하기 위해 수업 시간에 equalshashcode를 오버라이드 (override) 할 수있다.

당신은 자동으로 중복 제거됩니다 일반적으로 세트 (A HashSet에 학생들을 추가 할 수 있습니다.

1

스토어 그들을 SetComparator를 공급 장치 A Set (예 : TreeSet A와)과 건설에서의 그 fName 또는 uName가 동일한 존재의 기준을 충족합니다.

은 (I 중복의 정의는 학생들에게 외부 있으리라 믿고있어는, 예를 들면 서로 다른 시간에 다른 일을 의미한다. 그 다음 @assylias 학생에게 고유 경우 대답은 더 의미가 있습니다.)

+0

pls 당신은 ref 예제를 제공 할 수 ... – testndtv

1

고유 한 목록이 필요한 경우 List 인터페이스의 항목을 사용하는 것이 좋습니다. 당신은 당신의 학생 클래스의 equals()hashCode()를 구현하고 고유성을 적용하기 위해 HashSet를 사용하는

1

이 제안 Set

재정, hashCode()하고 Students 클래스의 equals() 방법을 사용하여 더 나은 될 것입니다.

0
public List<User> removeDuplicateFromList(List<User> list){ 
    int s=0; 
    List<User> users=new ArrayList<User>(); 
    for(User us1 :list){ 
     for(User us2:users){ 
     if(us1.getIdUser()==us2.getIdUser()){ 
      s=1; 
     }else{ 
      s=0; 
     } 

     } if(s==0){ 
      users.add(us1); 
     } 

    } 
    return users; 
}