Thursday, 19 June 2014

Why set interface doesn't allows duplicates in java?

Hi..if you are having 3+ yrs Of experience then some interviewer will ask this question..actually they will test for how set internally works in java. So for this you can answer like this..
    
               Internally SET store element using HASHMAP ...HASHMAP is a structure of Key value pairs..Here what the values passed by the SET is treated as Keys of HASHMAP Internally. keys are unique cannot be duplicated. That is the reason if you pass any duplicate value it return false and does not added to the SET ...
              If the adding element return true it will added into SET...Else it return False, that why it won't give any compilation or runtime error and it wont be added to SET

2 comments: