import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; public class MapTest { public static void main(String[] args) { Map<String, Integer> map1 = new HashMap<>(); //HashMap特点: 无序,不重复,无索引 map1.put("手表", 100); map1.put("手表", 220); //后面重复键对应的值会覆盖前面的值 map1.put("手机", 2); map1.put("java", 2); map1.put(null, null); System.out.println(map1); //{null=null, 手表=220, 手机=2, java=2} Map<String, Integer> map2 = new LinkedHashMap<>(); //LinkedHashMap: 有序,不重复,无索引 map2.put("手表", 100); map2.put("手表", 220); //后面重复键对应的值会覆盖前面的值 map2.put("手机", 2); map2.put("java", 2); map2.put(null, null); System.out.println(map2); //{手表=220, 手机=2, java=2, null=null} Map<Integer, String> map3 = new TreeMap<>(); //TreeMap特点: 可排序,不重复,无索引 map3.put(23, "java"); map3.put(23, "MySQL"); map3.put(19, "python"); map3.put(26, "PHP"); System.out.println(map3); //{19=python, 23=MySQL, 26=PHP} } }
Map集合常用种类及特点
最新推荐文章于 2024-09-02 22:33:56 发布