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

后端 未结 14 1465
时光说笑
时光说笑 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:22

    Use LinkedHashMap and use this function.

    private LinkedHashMap map = new LinkedHashMap();
    

    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.

提交回复
热议问题