• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does Set represent elements in a sequence?  RSS feed

 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does Set represent elements in a sequence
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the API say about sets?
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Fred,

I went through the below link:
http://docs.oracle.com/javase/6/docs/api/

But in the description of it , I was unable to pinpoint it.Can you please help me out
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:Dear Fred,

I went through the below link:
http://docs.oracle.com/javase/6/docs/api/

But in the description of it , I was unable to pinpoint it.Can you please help me out


Since the general description of Set doesn't say anything about how it stores its elements, that means that it is unspecified, and it can store them however it wants.

Additionally, the docs for Set's iterator() method say, "The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee)."
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, a Set does not contain elements in any particular order. It's just a bag that can contain things, and the things are not in any particular order. A sequence implies that there's a certain order to the elements - so a Set is not a sequence.

There are implementations of interface Set, such as LinkedHashSet, that do keep the elements in a specific order. But for example HashSet does not.
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jeff for pointing out my mistake and Jesper, thank you for simplifying it with a good example
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then there is the SortedSet interface, which extends Set and which actually defines the order of contained items.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure Martin meant to say NavigableSet
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you found the Java Tutorials?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!