I guess you want to have duplicate element in your collection, you can't have duplicate element in Set
, like definition of set in mathematical.
Oracle's Java says for Set
collection:
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.
but this restriction don't exist in List
collection, Oracle's Java says for 'List' collection:
Unlike sets, lists typically allow duplicate elements.
More formally, lists typically allow pairs of elements e1 and e2
such that e1.equals(e2), and they typically allow multiple null elements
if they allow null elements at all.
It is not inconceivable that someone might wish to implement
a list that prohibits duplicates,
by throwing runtime exceptions when the user attempts to insert them,
but we expect this usage to be rare.
So you can't have duplicate elemet in your set
instance.
for more information see following question in this site:
Blockquote