Map
map每个元素由键与值两部分组成,通过键可以找对所对应的值。
Map中的集合不能包含重复的键,值可以重复;每个键只能对应一个值。
Map 初始化
Map<String, String> map = new HashMap<String, String>();
插入元素
map.put("key1", "value1");
获取元素
map.get("key1")
移除元素
map.remove("key1");
清空map
map.clear();
例:
1 public class Demo01 { 2 public static void main(String[] args) { 3 //创建Map集合 4 Map<String, Integer> map=new HashMap<String, Integer>(); 5 //添加元素 6 map.put("a", 1); 7 map.put("b", 2); 8 map.put("d", 2); 9 map.put("a", 9); 10 11 //根据key获取value 12 System.out.println(map.get("a")); 13 System.out.println(map.get("b")); 14 System.out.println(map.get("d")); 15 //根据key删除指定元素 16 System.out.println(map.remove("b")); 17 //遍历 18 //1.从map集合中获取所有key所在的set集合 19 Set<String> set=map.keySet(); 20 //2.遍历所有key所在的set集合取到的每一个key 21 for(String key:set){ 22 System.out.print(key+map.get(key)); 23 } 24 System.out.println(); 25 System.out.println("==================="); 26 //遍历:keySet+Iterator 27 Set<String> set1=map.keySet(); 28 Iterator<String> it=set1.iterator(); 29 while(it.hasNext()){ 30 String a=it.next(); 31 Integer b=map.get(a); 32 System.out.println(a+b); 33 } 34 } 35 }
如图所示:
Entry键值对对象
Entry将键值对的对应关系封装成了键值对对象,遍历Map集合时,可以从每一个键值对对象中获取相应的键与值。
例:
1 public static void main(String[] args) { 2 Map<String, Integer> map=new HashMap<String, Integer>(); 3 //添加元素 4 map.put("a", 1); 5 map.put("b", 2); 6 map.put("d", 2); 7 map.put("a", 9); 8 //entrySet+增强for遍历 9 //1.获取所有键值对应的结婚证对象的set集合 10 Set<Map.Entry<String, Integer>> set=map.entrySet(); 11 //2.从结婚证集合中取出每一个结婚证 12 for(Map.Entry<String, Integer> entry:set){ 13 //从每一个结婚对象中取出key 14 String key=entry.getKey(); 15 //从每一个结婚对象中取出value 16 int value=entry.getValue(); 17 System.out.println(key+value); 18 } 19 System.out.println("==============="); 20 Set<Map.Entry<String, Integer>> set1=map.entrySet(); 21 Iterator<Map.Entry<String, Integer>> it=set1.iterator(); 22 while(it.hasNext()){ 23 Map.Entry<String, Integer> map1=it.next(); 24 String a=map1.getKey(); 25 int b=map1.getValue(); 26 System.out.println(a+b); 27 } 28 }
集合嵌套示例
先写一个person类
1 public class Person { 2 private String name; 3 private Integer age; 4 public Person() { 5 super(); 6 } 7 public Person(String name, Integer age) { 8 super(); 9 this.name = name; 10 this.age = age; 11 } 12 public String getName() { 13 return name; 14 } 15 public void setName(String name) { 16 this.name = name; 17 } 18 public Integer getAge() { 19 return age; 20 } 21 public void setAge(Integer age) { 22 this.age = age; 23 } 24 @Override 25 public String toString() { 26 return "Person [name=" + name + ", age=" + age + "]"; 27 } 28 @Override 29 public int hashCode() { 30 final int prime = 31; 31 int result = 1; 32 result = prime * result + ((age == null) ? 0 : age.hashCode()); 33 result = prime * result + ((name == null) ? 0 : name.hashCode()); 34 return result; 35 } 36 @Override 37 public boolean equals(Object obj) { 38 if (this == obj) 39 return true; 40 if (obj == null) 41 return false; 42 if (getClass() != obj.getClass()) 43 return false; 44 Person other = (Person) obj; 45 if (age == null) { 46 if (other.age != null) 47 return false; 48 } else if (!age.equals(other.age)) 49 return false; 50 if (name == null) { 51 if (other.name != null) 52 return false; 53 } else if (!name.equals(other.name)) 54 return false; 55 return true; 56 }
嵌套:
1 public class Demo04 { 2 public static void main(String[] args) { 3 //大Map 4 HashMap<String,HashMap<Person,String > >oracle 5 =new HashMap<String,HashMap<Person,String > >(); 6 //小Map1 7 HashMap<Person, String> java0723= 8 new HashMap<Person, String>(); 9 java0723.put(new Person("小红",18), "女"); 10 java0723.put(new Person("小黑",19), "男"); 11 //小Map2 12 HashMap<Person, String> java0611= 13 new HashMap<Person, String>(); 14 java0611.put(new Person("小蓝",18), "男"); 15 java0611.put(new Person("小绿",18), "男"); 16 //封装大Map 17 oracle.put("java0723", java0723); 18 oracle.put("java0611", java0611); 19 //ketset+增强for 20 //1.获取装有大Map中所有key的set集合 21 get1(oracle); 22 } 23 public static void get1(HashMap<String,HashMap<Person,String > >oracle){ 24 //ketset+增强for 25 //1.获取装有大Map中所有key的set集合 26 Set<String> bigkeys=oracle.keySet(); 27 //2.循环遍历取到大Map中每一个key 28 for(String bigkey:bigkeys){ 29 //根据每一个key获取对应的值 30 HashMap<Person, String> bigValue=oracle.get(bigkey); 31 //获取小Map的所有key所在的jihe 32 Set<Person> keys=bigValue.keySet(); 33 //循环遍历每一个xiaokey 34 for(Person key:keys){ 35 System.out.println(bigkey+key+bigValue.get(key)); 36 } 37 } 38 //获取大Map的结婚证对象集合 39 Set<Map.Entry<String,HashMap<Person,String >>> bigkey1=oracle.entrySet(); 40 Iterator<Map.Entry<String,HashMap<Person,String >>> it=bigkey1.iterator(); 41 while(it.hasNext()){ 42 Map.Entry<String,HashMap<Person,String >> map=it.next(); 43 String a=map.getKey(); 44 HashMap<Person, String> bigValue2=map.getValue(); 45 //获取小Map 46 Set<Map.Entry<Person, String>> keys1=bigValue2.entrySet(); 47 Iterator<Map.Entry<Person, String>> it1=keys1.iterator(); 48 while(it1.hasNext()){ 49 Map.Entry<Person, String> map2=it1.next(); 50 Person x=map2.getKey(); 51 String y=map2.getValue(); 52 //System.out.println(a+x+y); 53 System.out.println(a+map2.getKey()+map2.getValue()); 54 } 55 } 56 } 57 }