File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set Does not allow Duplicate Element

 
Deepak Sahu
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,


Can any one provide the answer for "Why Set Does not allow duplicate elements".
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think why it's not allowed?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will put it in other way,

Why List allows duplicate elements ? and do you know the meaning of Abstract Data Type(ADT)?
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple answer: because that's the definition of a set, in just about any context including math and programming.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because sometimes you need a collection that doesn't allow duplicate elements, and you have to give it some kind of name.
 
Nico Van Brandt
Ranch Hand
Posts: 66
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's by design, so whatever you think... it's supposed to be that way
 
Dave Brock
Greenhorn
Posts: 2
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike using a List, which can include duplicates, a Set does not. Take a look at the Java API definition of a Set:
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.

As it says, that is the true definition of a set as in the math and science industries. You will often need a List for when you do not want duplicates and want things to be distinct.

If you want information on ADT for Java, this might help you:
http://css.freetonik.com/wiki/abstract_data_types_and_algorithms:basic_java_review
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic