一、概述
Map集合是双列集合的顶层接口,它是用来存储键值对对象的,其中键具有唯一性,而值是可以重复的。即:Map集合的数据结构只针对于键有效
二、格式
public interface Map<K,V> //K:键的类型,V:值的类型
三、创建对象
因为Map是接口,不能通过new关键字直接创建它的对象,为我们可以通过多态的形式,创建其子类对象。从而实现创建Map集合对象的这个需求
注意:Map集合的常用子类主要有两个,分别是:HashMap和TreeMap
/**
* 定义Map集合,键是学号,值是学生的名字(键值都是字符串类型)
* 往Map集合中添加3对元素
* 打印Map集合对象
*/
public class Test{
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>(3);
map.put("European","21");
map.put("Sheik","22");
map.put("欧洲酋长","23");
System.out.println(map);
}
}
四、Map集合的成员方法
-
方法描述
V put (K key, V value):添加元素 V remove(Object key):根据键删除键值对元素 void clear():移除所有的键值对元素 boolean containsKey(Object key):判断集合是否包含指定的键 boolean containsValue(Object value):判断集合是否包含指定的值 boolean isEmpty():判断集合是否为空 int size():集合的长度,也就是集合中键值对的个数据
public class Test{ public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("EuropeanSheik","20"); map.remove("EuropeanSheik"); map.put("European","21"); map.put("Sheik","22"); map.clear(); map.put("欧洲酋长","23"); System.out.println(map.containsKey("欧洲酋长")); System.out.println(map.containsValue("23")); System.out.println(map.isEmpty()); System.out.println(map.size()); } }
五、Map集合的获取功能
-
方法描述
V get(Object key):根据键获取值 Set keySet():获取所有键的集合 Collection values():获取所有值的集合
public class Test{ public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("European","21"); map.put("Sheik","22"); System.out.println(map.get("European")); System.out.println(map.keySet()); System.out.println(map.values()); } }
/** * 定义Map集合,键值都是字符串类型 * 往集合中添加3对键值对元素 * 遍历Map集合 */ public class Test{ public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("European","21"); map.put("Sheik","22"); map.put("欧洲酋长","23"); Set<String> set = new HashSet<String>(); set = map.keySet(); Iterator it = set.iterator(); while (it.hasNext()){ String temp = (String) it.next(); System.out.println(temp+map.get(temp)); } } }
/** * 创建HashMap集合,键是学号(String),值是学生对象(Student) * 往HashMap集合中添加3组数据 * 遍历HashMap集合 */ public class Student { String name; int age; public Student(String name, int age){ this.name = name; this.age = age; } } class StudentTest{ public static void main(String[] args) { Map<String,Student> map = new HashMap<String,Student>(); map.put("201801", new Student("European",21)); map.put("201802", new Student("Sheik",22)); map.put("201803", new Student("欧洲酋长",23)); for (Map.Entry<String,Student> entry:map.entrySet()){ System.out.println(entry.getKey()+entry.getValue().name+entry.getValue().age); } } }
/** * 创建HashMap集合,键是学生对象(Student),值是居住地 * 往HashMap集合中添加3组属于 * 通过两种方式,遍历HashMap集合 */ public class Student { String name; int age; public Student(String name, int age){ this.name = name; this.age = age; } } class StudentTest{ public static void main(String[] args) { Map<Student,String> map = new HashMap<Student,String>(); map.put(new Student("European",21), "201801"); map.put(new Student("Sheik",22), "201802"); map.put(new Student("欧洲酋长",23), "201803"); for(Map.Entry<Student,String> entry:map.entrySet()){ System.out.println(entry.getKey().name+entry.getKey().age+entry.getValue()); } Set<Student> set = map.keySet(); for (Student s:set){ System.out.println(map.get(s)+s.name+s.age); } } }
/** * 键盘录入一个字符串,要求统计字符串中每个字符出现的次数 */ public class Test { public static void main(String[] args) { Map<String,Integer> map = new HashMap<String,Integer>(); Scanner sc = new Scanner(System.in); System.out.print("请输入一个字符串:"); String input = sc.nextLine(); for (int i=0;i<input.length();i++){ String temp = String.valueOf(input.charAt(i)); if (map.containsKey(temp)){ map.put(temp,map.get(temp)+1); }else{ map.put(temp,1); } } System.out.println(map); } }