Is it possible to get element from HashMap by its position?

后端 未结 14 1327
时光说笑
时光说笑 2020-12-12 18:31

How to retrieve an element from HashMap by its position, is it possible at all?

相关标签:
14条回答
  • 2020-12-12 19:12

    I'm assuming by 'position' you're referring to the order in which you've inserted the elements into the HashMap. In that case you want to be using a LinkedHashMap. The LinkedHashMap doesn't offer an accessor method however; you will need to write one like

    public Object getElementAt(LinkedHashMap map, int index) {
        for (Map.Entry entry : map.entrySet()) {
            if (index-- == 0) {
                return entry.value();
            }
        }
        return null;
    }
    
    0 讨论(0)
  • 2020-12-12 19:14

    By default, java LinkedHasMap does not support for getting value by position. So I suggest go with customized IndexedLinkedHashMap

    public class IndexedLinkedHashMap<K, V> extends LinkedHashMap<K, V> {
    
        private ArrayList<K> keysList = new ArrayList<>();
    
        public void add(K key, V val) {
            super.put(key, val);
            keysList.add(key);
        }
    
        public void update(K key, V val) {
            super.put(key, val);
        }
    
        public void removeItemByKey(K key) {
            super.remove(key);
            keysList.remove(key);
        }
    
        public void removeItemByIndex(int index) {
            super.remove(keysList.get(index));
            keysList.remove(index);
        }
    
        public V getItemByIndex(int i) {
            return (V) super.get(keysList.get(i));
        }
    
        public int getIndexByKey(K key) {
            return keysList.indexOf(key);
        }
    }
    

    Then you can use this customized LinkedHasMap as

    IndexedLinkedHashMap<String,UserModel> indexedLinkedHashMap=new IndexedLinkedHashMap<>();
    

    TO add Values

    indexedLinkedHashMap.add("key1",UserModel);
    

    To getValue by index

    indexedLinkedHashMap.getItemByIndex(position);
    
    0 讨论(0)
  • 2020-12-12 19:18

    HashMaps do not preserve ordering:

    This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

    Take a look at LinkedHashMap, which guarantees a predictable iteration order.

    0 讨论(0)
  • 2020-12-12 19:21

    If you want to maintain the order in which you added the elements to the map, use LinkedHashMap as opposed to just HashMap.

    Here is an approach that will allow you to get a value by its index in the map:

    public Object getElementByIndex(LinkedHashMap map,int index){
        return map.get( (map.keySet().toArray())[ index ] );
    }
    
    0 讨论(0)
  • 2020-12-12 19:22

    Use LinkedHashMap and use this function.

    private LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
    

    Define like this and.

    private Entry getEntry(int id){
            Iterator iterator = map.entrySet().iterator();
            int n = 0;
            while(iterator.hasNext()){
                Entry entry = (Entry) iterator.next();
                if(n == id){
                    return entry;
                }
                n ++;
            }
            return null;
        }
    

    The function can return the selected entry.

    0 讨论(0)
  • 2020-12-12 19:23

    HashMap has no concept of position so there is no way to get an object by position. Objects in Maps are set and get by keys.

    0 讨论(0)
提交回复
热议问题