How to get a key position from HashMap in Java

How can I get a key position on the map? How can I see where Audi and BMW are?

Map<String, Integer> map = new HashMap<String, Integer>(); map.put("Audi", 3); map.put("BMW", 5); 
+5
source share
3 answers

Like other answers, you should use a structure like java.util.LinkedHashMap . LinkedHashMap supports internal keys using LinkedEntrySet , this does not formally provide order, but iterates in the insertion order used.

If you pass Map.keySet() to the implementation of the list, you can use the List.indexOf(Object) method without writing additional code to another answer.

 Map<String, Integer> map = new LinkedHashMap<String, Integer>(); map.put("Audi", 3); map.put("BMW", 5); map.put("Vauxhall", 7); List<String> indexes = new ArrayList<String>(map.keySet()); // <== Set to List // BOOM ! System.out.println(indexes.indexOf("Audi")); // ==> 0 System.out.println(indexes.indexOf("BMW")); // ==> 1 System.out.println(indexes.indexOf("Vauxhall")); // ==> 2 
+13
source

You can not. From the HashMap JavaDocs :

Implementation of a map interface based on a hash table. This implementation provides all optional card operations and allows null values ​​and a null key. (The HashMap class is roughly equivalent to the Hashtable, except that it is unsynchronized and resolves to null.) This class gives no guarantees regarding the order of the map; in particular, it does not guarantee that order will remain constant over time.

Thus, the order can vary between iterations. If you need to save an order, you can take a look at LinkedHashMap

From LinkedHashMap JavaDocs :

A hash table and a linked list of the map interface with a predictable iteration order. This implementation differs from HashMap in that it supports a doubly linked list passing through all its entries. This linked list defines the iteration order, which is usually the order in which keys were inserted into the map (insert order).

So, to find the key position, you basically need to iterate over the keys and count the position of the key you are looking for.

On the side of the IMO note, this may not be the best way to use the Map data type. I believe that if you really need a position, you should use some type of List (for example, ArrayList ) that actually keeps order, and you can use the get method to retrieve the elements for a specific index.

+2
source

You can not. Keys on the map and HashMap are not ordered. You will need to use a structure that preserves order, such as LinkedHashMap .

Please note that LinkedHashMap does not provide a method that retrieves keys by position, so this only applies if you intend to use an iterator.

An alternative is to create a second Card, which maps from your key to the Integer position and adds to it along the way:

 Map<String, Integer> indexMap = new HashMap<String, Integer>(); indexMap.put("Audi", 0); indexMap.put("BMW", 1); 

For a more elegant solution, you may need additional information about what you are doing.

+2
source

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


All Articles