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?

- 12,965
- 5
- 47
- 74

- 35
- 3
-
2See 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 Answers
In a word - yes. Neither HashMap
or HashSet
give any guarantee on the order of iteration.

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

- 497
- 4
- 11
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

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

- 122
- 8
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

- 1,483
- 1
- 15
- 29
-
The question is the ordering, not the difference between map and set – Steve Kuo Dec 01 '17 at 16:49
-
@SteveKuo the question was **"I am confused and why do you need these two?"** – Rômulo M. Farias Dec 01 '17 at 16:51
-
1
-
@Bedla I just make it quick and messed it up, you're right. The correct method is `put` – Rômulo M. Farias Dec 01 '17 at 16:53
-
You've still got raw types on your HashMap, might want to change that. – luckydog32 Dec 01 '17 at 16:56
-
@luckydog32 can Java use generics with raw types? I'm not aware of that – Rômulo M. Farias Dec 01 '17 at 16:57
-
Yes it can, if you leave out the generic parameters it will use a raw type, like in `new HashMap();`. [Doc Link](https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation) – luckydog32 Dec 01 '17 at 17:06