top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can allow duplicate value in set in Java?

0 votes
How can allow duplicate value in set in Java?
posted May 18, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Inserting Duplicate Values In Set ( java.util.Set)

Java.util.Set : as per definition, it is said that set contains the unique values. Now Q arises in mind how Set interface decides that the current value is duplicate.

Lets see some scenarios:

  1. If you are inserting any numbers, its great it can directly say yeah this number I already have so it’s a duplicate number.

  2. If you are inserting any String, as String implement Comparable internally, Set still can determine that yes the String is Duplicate.

  3. Now Suppose you want to enter any custom object, like your employee class object, which has many fields like name, id ,address. So in this case how our set decide which object is Duplicate.

Actually to determine duplicity it use two methods named equals() and hashcode(), if you can play with them, you can allow Set to contain duplicates.

answer May 19, 2015 by Karthick.c
Similar Questions
+1 vote

I have to set monthly, yearly recurrence pattern to calendar in pst file using java(eclipse). But it shows exception like: Duration can't be more than period of recurrence.

please give me proper answer to this exception.

+1 vote

Map data = new HashMap();

Need to have both "America" and "Africa" as value against key "a". Is there any mechanism to achieve this scenario.