Java HashMap Collection and Base Values ​​()

I was wondering if the presentation of the collection of values ​​contained in the HashMap is supported when the HashMap changes.

For example, if I have a HashMap, the values ​​() method returns L = {a, b, c} What happened to L if I add a new "d" element to the map? It is added at the end, that is, if I repeat the elements, is this order preserved?

In particular, if adding a new “d” element causes a rework, will the order be kept in L?

Many thanks!

+3
source share
4 answers

I was wondering if the view of the collection of values ​​contained in the HashMap is preserved when the HashMap changes.

No, there is no such guarantee.

, 1-100

HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

for (int i = 0; i < 100; i++)
    map.put(i, i);

System.out.println(map.values());

( ).

, , , LinkedHashMap:

- . HashMap , , . , , ( ).

+5

JavaDoc, . Java . .

, LinkedHashMap.

+1

HashMap Java , , , values ​​() .

LinkedHashMap - HashMap ( ), , () . , .

0

, HashMap. , , , , .

0

Source: https://habr.com/ru/post/1796326/


All Articles