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);
}
}
प्रश्न: संदर्भ एस के साथ HashSet के लिए, HashSet पृथक नहीं किया जा रहा है। लेकिन संदर्भ एस 1 के लिए, हैशसेट को क्रमबद्ध किया गया है। ऐसा क्यों?हैशसेट ने गारेनटे सॉर्टिंग नहीं किया है?
कृपया कोड टैग का उपयोग करें। यह और अधिक पठनीय होगा! –
क्या आप आउटपुट प्रदान कर सकते हैं? –
आउटपुट: डुप्लिकेट तत्व 6 डुप्लिकेट तत्व 9 [1, 100, 2, 55, 6, 8, 9] HashSet डुप्लिकेट तत्व 3 [2, 3, 5, 6] HashSet डुप्लिकेट तत्व 6 डुप्लिकेट तत्व 9 [1, 2, 6, 8, 9, 55, 100] ट्रीसेट डुप्लिकेट तत्व 6 डुप्लिकेट तत्व 9 [6, 9, 2, 55, 100, 1, 8] लिंक्ड हैशसेट – Gopal