0

In Java 5, If i have a Set, and i add two objects to the Set. When I retrieve the objects, will it give me back in the same order as I added? I am not able to find the answer to this. Does it depend on which Set implementation I use?

2
  • I was going to ask "Why not just use a Queue", and then I saw the word "Set". :) So I assume you want your data structure to maintain the "uniqueness" property? If yes, then as Jon Skeet mentioned, LinkedHashSet is perfect. If you don't care about uniqueness, then a Queue will do just fine. Commented Feb 9, 2012 at 18:58
  • It's right in Set's documentation. See docs.oracle.com/javase/6/docs/api/java/util/Set.html Commented Feb 9, 2012 at 19:03

6 Answers 6

9

Yes, it depends on which implementation you use. For example, LinkedHashSet will preserve insertion order:

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.)

... but HashSet won't:

It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

Sign up to request clarification or add additional context in comments.

Comments

2

Straight from the documentation:

The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

Comments

0

Depends on Set implementation. LinkedHashSet does exactly that.

Comments

0

Yes it does depend on the implementation you choose. HashSet will not guarantee order but LinedHashSet will.

Comments

0

JavaDocs is your best friend. It's implementation specific. For example:

java.util.Set:

Iterator<E> iterator(); 
Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

and

java.util.TreeSet:

public Iterator<E> iterator();
Returns an iterator over the elements in this set in ascending order.

Comments

0

The Set interface itself is for unordered container implementations. But there might be implementations of Set that do return the elements in a particular order.

Also see the documentation of Set.iterator:

Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.