Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why set is not allowed duplicate value, which kind of mechanism used behind them?

Tags:

java

I am new to java, i know set is not allowed duplicate value but i don't know why set is not allowed duplicate value, Actually i am doing practically,

Declared one set and add duplicate value but no kind of error is occurring, no compile time error, no run time. why?

like image 919
J.B.Vala Avatar asked Jan 01 '14 16:01

J.B.Vala


People also ask

Why set does not allow duplicate method?

The meaning of "sets do not allow duplicate values" is that when you add a duplicate to a set, the duplicate is ignored, and the set remains unchanged. This does not lead to compile or runtime errors: duplicates are silently ignored. Set is implemented like that to avoid duplication.

Is duplicate value allowed in set?

A Set is a Collection that cannot contain duplicate elements.

Which data type does not allow duplicate values?

HASHTABLE is a structure of Key value pairs.. Here what the values passed by the SET is treated as Keys of HASHTABLE Internally. keys are unique cannot be duplicated.

Why duplicate values are not stored in HashSet?

HashSet is an implementation of Set Interface which does not allow duplicate value. The main thing is, objects that are stored in HashSet must override equals() for check for equality, and hashCode() methods for no duplicate value are stored in our set.


1 Answers

Set (Oracle Documentation)

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

See: http://docs.oracle.com/javase/7/docs/api/java/util/Set.html

Set (Methematics) - Quoting from wikipedia

In mathematics, a set is a collection of distinct objects, considered as an object in its own right.

Add Method

According to the documentation of the interface, if the element does not exist it is added. Otherwise, nothing changes.

boolean add(E e):

Adds the specified element to this set if it is not already present (optional operation). If this set already contains the element, the call leaves the set unchanged and returns false.

Example Implementation Code: HashSet

 /**
     * Adds the specified element to this set if it is not already present.
     * More formally, adds the specified element <tt>e</tt> to this set if
     * this set contains no element <tt>e2</tt> such that
     * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
     * If this set already contains the element, the call leaves the set
     * unchanged and returns <tt>false</tt>.
     *
     * @param e element to be added to this set
     * @return <tt>true</tt> if this set did not already contain the specified
     * element
     */
    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }
like image 117
Menelaos Avatar answered Oct 21 '22 22:10

Menelaos