Map接口中的常用方法import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;//Map接口public class Demo01 { public static void main(String[] args) { Mapmap=new HashMap (); //添加元素 map.put("a", "1"); map.put("b", "2"); map.put("a", "3");//key键不能重复 如果重复会覆盖值 值可以重复 map.put("c", "1"); /*String v=map.remove("b"); //查询元素 System.out.println(map.get("a")); System.out.println(map.get("b")); System.out.println(map.get("c"));*/Map集合遍历键找值方式 //1.通过keyset遍历map //增强for Set keys=map.keySet(); for(String key:keys){ System.out.print(key);//取key System.out.println(map.get(key));//取值 } //Interator迭代器迭代 Set key1=map.keySet(); Iterator it=key1.iterator(); while(it.hasNext()){ String key=it.next(); String value=map.get(key); System.out.println(key+value); }Map集合遍历键值对方式 //2.entrySet方法遍历 //增强for Set > entrys=map.entrySet(); for(Map.Entry entry:entrys){ String key=entry.getKey(); String value=entry.getValue(); System.out.println(key+value); } //Interator迭代器迭代 Set > entry1=map.entrySet(); Iterator > it1=entry1.iterator(); while(it1.hasNext()){ //获取每一张结婚证 Map.Entry entry=it1.next(); String key=entry.getKey();//从结婚证上获取key String value=entry.getValue();//从结婚证上获取值 System.out.println(key+value); } }}HashMap存储自定义类型键值先建一个Person类public class Person { private String name; private int 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; } public Person(){} public Person(String name, int age) { super(); this.name = name; this.age = age; } public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }import java.util.HashMap;import java.util.LinkedHashMap;import java.util.Map;import java.util.Set;public class Demo02 { public static void main(String[] args) { method1(); method2(); method3(); } public static void method1(){ Map map=new HashMap (); map.put("2", new Person("帅涛",18)); map.put("1", new Person("狗炜",19)); map.put("3", new Person("小菜",19)); map.put("4", new Person("小明",19)); map.put("4", new Person("小明",19)); //遍历 Set set=map.keySet(); for(String s:set){ System.out.println(s+map.get(s)); } }
//自定义类型当key键时需要重写hashCode和equals方法 public static void method2(){ Mapmap=new HashMap (); map.put(new Person("帅涛",18),"1" ); map.put(new Person("狗炜",19),"2" ); map.put(new Person("小菜",19),"3"); map.put(new Person("小明",19),"4" ); map.put(new Person("小明",19),"4" ); //遍历 Set set=map.keySet(); for(Person s:set){ System.out.println(s+map.get(s)); } }
//有序遍历 public static void method3(){ Mapmap=new LinkedHashMap (); map.put(new Person("帅涛",18),"2" ); map.put(new Person("狗炜",19),"1" ); map.put(new Person("小菜",19),"3"); map.put(new Person("小明",19),"4" ); map.put(new Person("小明",19),"4" ); //遍历 Set set=map.keySet(); for(Person s:set){ System.out.println(s+map.get(s)); } } }