-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?

user1803551
  • 12,965
  • 5
  • 47
  • 74
Syed Alam
  • 35
  • 3
  • 2
    See https://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." – Steve Kuo Dec 01 '17 at 16:47
  • If you are looking for a data structure that you can iterate over in insertion order you can use a [LinkedHashSet](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashSet.html) or [LinkedHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) – luckydog32 Dec 01 '17 at 16:50
  • Use any implementation of [`SortedSet`](https://docs.oracle.com/javase/9/docs/api/java/util/SortedSet.html) (a subinterface of `Set`) if you care about the order. – Basil Bourque Dec 01 '17 at 17:37

5 Answers5

1

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

Mureinik
  • 297,002
  • 52
  • 306
  • 350
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

Akshay Bahadur
  • 497
  • 4
  • 11
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

ford prefect
  • 7,096
  • 11
  • 56
  • 83
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.

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
Rômulo M. Farias
  • 1,483
  • 1
  • 15
  • 29