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

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

    Use a LinkedHashMap and when you need to retrieve by position, convert the values into an ArrayList.

    LinkedHashMap linkedHashMap = new LinkedHashMap();
    /* Populate */
    linkedHashMap.put("key0","value0");
    linkedHashMap.put("key1","value1");
    linkedHashMap.put("key2","value2");
    /* Get by position */
    int pos = 1;
    String value = (new ArrayList(linkedHashMap.values())).get(pos);
    

提交回复
热议问题