1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* Simple Java LinkedHashMap example This simple Java Example shows how to use Java LinkedHashMap. It also describes how to add something to LinkedHashMap and how to retrieve the value added from LinkedHashMap. */ import java.util.LinkedHashMap; public class JavaLinkedHashMapExample { public static void main(String[] args) { //create object of LinkedHashMap LinkedHashMap lHashMap = new LinkedHashMap(); /* Add key value pair to LinkedHashMap using Object put(Object key, Object value) method of Java LinkedHashMap class, where key and value both are objects put method returns Object which is either the value previously tied to the key or null if no value mapped to the key. */ lHashMap.put("One", new Integer(1)); lHashMap.put("Two", new Integer(2)); /* Please note that put method accepts Objects. Java Primitive values CAN NOT be added directly to LinkedHashMap. It must be converted to corrosponding wrapper class first. */ //retrieve value using Object get(Object key) method of Java LinkedHashMap class Object obj = lHashMap.get("One"); System.out.println(obj); /* Please note that the return type of get method is an Object. The value must be casted to the original class. */ } } /* Output of the program would be 1 */ |
unfortunately, you have not understood what was needed this class and how it differs from HashMap