Map集合的特点:
1)Map集合是双边队列的,通过键值对(key-value)来进行存储数据
2)key值不能重复,value值可以重复
Map集合下面常用的方法:
增加:put(K key, V value);存放的是一个键值对的数据
putAll(Map extends K> k, Map extends V> v);将一个map集合存放到另一个map集合
删除:remove(Object k);通过键删除整个键值对,并返回被删除的值
修改:put(K key, V value); 当key存在的时候,就修改。当key不存在时,就添加
查询:boolean isEmpty();是否为空,map集合为空就返回true,不为空就返回false
boolean containsKey();是否包含这个键
boolean containsValue();是否包含这个值
V get(Object k);通过键获取值
Collection
Set
2.Map的子类public class Test { public static void main(String[] args) { HashMapmap = new HashMap (); map.put("01", "哈哈"); map.put("02", "嘻嘻"); map.put("03", "嘿嘿"); map.put("03", "呵呵"); System.out.println(map); HashMap map1 = new HashMap (); map1.put("07", "呼呼"); map1.put("08", "哦哦"); map1.put("01", "哈哈哈"); map.putAll(map1); System.out.println(map); System.out.println(map.remove("07")); System.out.println(map); map.put("01", "哈哈"); System.out.println(map); System.out.println(map.isEmpty()); System.out.println(map.size()); System.out.println(map.containsKey("08")); System.out.println(map.containsValue("嘿嘿嘿")); Set set = map.keySet(); System.out.println(set); System.out.println(map.get("01")); Collection collection = map.values(); System.out.println(collection); Set > set1 = map.entrySet(); System.out.println(set1); for (Entry entry : set1) { System.out.println(entry.getKey()); System.out.println(entry.getValue()); } } }
1)HashMap
示例
class Person { private String name; private int age; Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int hashCode() { return this.name.hashCode() + age * 37; } @Override public boolean equals(Object obj) { if (obj instanceof Person) { Person p = (Person) obj; return this.name.equals(p.name) && this.age == p.age; } else { return false; } } @Override public String toString() { return "Person@name:" + this.name + " age:" + this.age; } } public class Test { public static void main(String[] args) { HashMaphm = new HashMap (); hm.put(new Person("jack", 20), "1001"); hm.put(new Person("rose", 18), "1002"); hm.put(new Person("lucy", 19), "1003"); hm.put(new Person("hmm", 17), "1004"); hm.put(new Person("ll", 25), "1005"); System.out.println(hm); System.out.println(hm.put(new Person("rose", 18), "1006")); Set > entrySet = hm.entrySet(); Iterator > it = entrySet.iterator(); while (it.hasNext()) { Entry next = it.next(); Person key = next.getKey(); String value = next.getValue(); System.out.println(key + " = " + value); } } } }
2)TreeMap
示例
class Student implements Comparable{ private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } @Override public int compareTo(Student o) { int n1 = this.age - o.age; int n2 = this.name.compareTo(o.name); if (n1 == 0) { return n2; } else { return n1; } } } public class Test { public static void main(String[] args) { Student stu1 = new Student("张三", 12); Student stu3 = new Student("李四", 20); Student stu2 = new Student("王五", 23); Student stu4 = new Student("马六", 13); Student stu5 = new Student("赵二", 13); TreeMap treeMap = new TreeMap (); treeMap.put(stu1, "踏实"); treeMap.put(stu2, "勤奋"); treeMap.put(stu3, "刻苦"); treeMap.put(stu4, "愚钝"); treeMap.put(stu5, "懒散"); System.out.println(treeMap); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)