에 비교기를 실행 : 내 ArrayList를 pAllNisType
가 같음에 따라 6 개의 오브젝트 (가득요소가 나는 코드의 평화를 TreeSet의
private class NisTypeComparator implements Comparator<NisType> {
@Override
public int compare(NisType pNisType, NisType pNisType2) {
if (pNisType.getPrio()>pNisType2.getPrio())
return 1;
else if (pNisType.getPrio()<pNisType2.getPrio())
return -1;
else
return 0;
}
}
:
final SortedSet<NisType> lAllNNisType = new TreeSet<NisType>(new NisTypeComparator());
lAllNisType.addAll(pAllNisType); // where pAllNisType is of type ArrayList<NisType>
내 비교 클래스를 먹으 렴 및 hashCode 메소드). 이 라인이 실행 된 그럼에도 불구하고 후 :
lAllNisType.addAll(pAllNisType);
lAllNisType
만 5 개체를 포함합니다. 0을 반환하는 비교가 하나 있습니다.이 결과로 lAllNisType
에서 개체가 제거되었습니다.
나는 여기서 어떤 일이 일어나는지 알지 못합니다. 개체가 다릅니다. 내가 이렇게하면 :
final Set<NisType> lAllNisType = new HashSet<NisType>(pAllNisType);
lAllNisType
에는 6 가지 요소가 있습니다. 당신의 도움에 대한
감사
스테판
그러면 'Comparator'가 'equals'및 'hashCode'와 다르게 비교됩니다. 물론 –
입니다. 그게 왜 중요한가요? –
그 이유는 'TreeSet'에 5 개의 요소가 있고 'HashSet'에 6 개의 요소가있는 이유입니다. –