-1

I was wondering, both HashMap and HashSet do not return values in order? Please someone clarify. I am confused and why do you need these two?

3
  • 2
    See docs.oracle.com/javase/8/docs/api/java/util/HashSet.html, in particular "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." Commented Dec 1, 2017 at 16:47
  • If you are looking for a data structure that you can iterate over in insertion order you can use a LinkedHashSet or LinkedHashMap Commented Dec 1, 2017 at 16:50
  • Use any implementation of SortedSet (a subinterface of Set) if you care about the order. Commented Dec 1, 2017 at 17:37

5 Answers 5

1

In a word - yes. Neither HashMap or HashSet give any guarantee on the order of iteration.

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

Comments

1

The HashMap API does not define the order of iteration.

However, if you look at the implementation of HashMap, you can deduce that there is a complex transient relationship between the iteration order, the keys' hash values, the order in which the keys were inserted and the size of the hashtable. This relationship gets scrambled if the hashtable resizes itself.

Please refer:

Is the order of values retrieved from a HashMap the insertion order

Comments

0

If you want the values in the order you insert you have to use LinkedHasmap other wise you can use TreeMap where sort by the key. hash does not give any order because it uses the hascode to order values it may vary depend on the object.

Comments

0

A HashMap stores key value pairs. A HashSet is an unordered collection of objects in which each there can be no repeats. Neither are necessarily iterated in order insertion or otherwise. There are ordered implementations but you would not use the standard HashMap or HashSet classes.

Talked about further here

Linked Hash Map does maintain insertion order

Comments

0

HashMap is a implementation of Map interface. Map is a data structure to say that A corresponds to B.

Map<Integer, String> map = new HashMap<Integer, String>();
map.put(new Integer(1), "Test");

In the case above we know that every time that we look for 1, the correspondent is "Test".

Sets are something completely different. Sets ensures that no duplicate object will exists in your collection. HashSet is an implementation of Set interface.

To insert and retrieve something in order you could use LinkedHashMap, LinkedHashSet, ArrayList (implementation of List interface) and others

Wrapping it up:

  • Map - correspond objects
  • Set - Ensure unique objects in a collection

7 Comments

The question is the ordering, not the difference between map and set
@SteveKuo the question was "I am confused and why do you need these two?"
Are you sure, java.util.Map contains set method?
@Bedla I just make it quick and messed it up, you're right. The correct method is put
You've still got raw types on your HashMap, might want to change that.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.