Java Class that implements Map and keeps insertion order?

后端 未结 9 2198
一向
一向 2020-11-22 11:28

I\'m looking for a class in java that has key-value association, but without using hashes. Here is what I\'m currently doing:

  1. Add values to a Hashtable<
9条回答
  •  耶瑟儿~
    2020-11-22 11:46

    LinkedHashMap will return the elements in the order they were inserted into the map when you iterate over the keySet(), entrySet() or values() of the map.

    Map map = new LinkedHashMap();
    
    map.put("id", "1");
    map.put("name", "rohan");
    map.put("age", "26");
    
    for (Map.Entry entry : map.entrySet()) {
        System.out.println(entry.getKey() + " = " + entry.getValue());
    }
    

    This will print the elements in the order they were put into the map:

    id = 1
    name = rohan 
    age = 26 
    

提交回复
热议问题