import java.util.*;
public class DuplicateCheckMain {
public static void main(String[] gopal){
Integer[] args = {6,9,2,55,100,1,6,8,9};
Integer[] args1 = {3,6,2,3,5};
Set S = new HashSet();
DuplicateCheck.checkDuplicate(S,args,new String("HashSet"));
Set S1 = new HashSet();
DuplicateCheck.checkDuplicate(S1,args1,new String("HashSet"));
S = new TreeSet();
DuplicateCheck.checkDuplicate(S,args,new String("TreeSet"));
S = new LinkedHashSet();
DuplicateCheck.checkDuplicate(S,args,new String("LinkedHashSet"));
}
}
public class DuplicateCheck {
public static void checkDuplicate(Set S, Integer[] args, String setname){
for(int i = 0;i<args.length;i++){
if(!S.add(args[i])){System.out.println("Duplicate element "+args[i]);}
}
System.out.println(S +" "+ setname);
}
}
Pytanie: w przypadku HashSet z odniesieniem S, HashSet nie jest posortowane. Ale dla odniesienia S1, HashSet jest sortowany. Dlaczego tak?HashSet nie sortuje?
Proszę używać znaczników kodu. Będzie bardziej czytelny! –
Czy możesz podać wynik? –
wyjściowa: zduplikowane elementem 6 zduplikowane elementem 9 [1, 100 2, 55, 6, 8, 9] Hashset zduplikowane elementu 3 [2, 3, 5, 6] HashSet zduplikowane element 6 zduplikowane element 9 [1, 2, 6, 8, 9, 55, 100] TreeSet Duplikat elementu 6 Duplikat elementu 9 [6, 9, 2, 55, 100, 1, 8] LinkedHashSet – Gopal