Map接口指定一个合约,以键/值 对的形式实现元素的集合。实现Map 接口的Java集合类是HashMap,LinkedHashMap和TreeMap。
片段1
jshell> HashMap<String, Integer> hashMap = new HashMap<>(); hashMap ==> {} jshell> hashMap.put("Adithya", 101); $2 ==> null jshell> hashMap.put("Jai", 102); $3 ==> null jshell> hashMap.put("Chaitanya", 103); $4 ==> null jshell> hashMap.put("Ravi", 104); $5 ==> null jshell> hashMap hashMap ==> {Chaitanya=103, Jai=102, Ravi=104, Adithya=101}
我n中的下面的代码片断,的元素LinkedHashMap的 已经存储在插入 顺序。
片段3
jshell> TreeMap<String, Integer> treeMap = new TreeMap<>(); treeMap ==> {} jshell> treeMap.put("Raj", 101); $14 ==> null jshell> treeMap.put("Pavan", 102); $15 ==> null jshell> treeMap.put("Arjun", 103); $16 ==> null jshell> treeMap.put("Manoj", 104); $17 ==> null jshell> treeMap treeMap ==> {Arjun=103, Manoj=104, Pavan=102, Raj=101}