Map集合是以键值对存储对象,将键映射到值,一个映射不能包含重复的键,每个键只能映射到一个值。
Map和Collection的区别:
1:Map是双列参数,Collection是单列参数;
2:Map的键是唯一的,Collection的子系里面只有Set才是唯一的,其他的子系中可以重复;
3:Map集合的数据结构是针对键有效,跟值无关,Collection集合的数据结构是针对元素有效;
举个例子说明:
学校里面,是根据学号来区分不同学生的,知道了学生的学号,就能根据学号获取学生的姓名,学生的姓名可以重复,但学号是不可能重复的,这么来看,学号,学生的姓名和Map集合里面的键和值都是一种映射关系,学号就是键,学生的姓名就是值。
Map集合的功能概述:
添加功能:
V put(K key,V value):添加元素,K是键的类型,V是值的类型,
删除功能:
void clear():移除所有键值对的元素
V remove(Object key):根据键删除对应的值,并把值返回
判断功能:
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
获取功能:
Set<Map.Entry<k,v>> entrySet():
V get(Object key):根据键获取值
Set<k> keySet():获取集合中的所有键的集合
Collection<v> values():获取集合中多有值的集合
int size():获取集合中的键值对的对数
Map集合是接口,它有两个常用的实现类,分别是HashMap和TreeMap.
HashMap:是基于哈希表的Map集合,线程不同步
TreeMap:是基于红黑树的Map集合,它可以对键实现自然排序,或者根据创建映射时提供的Comparator进行排序。
hashMap:
public static void main(String[] args) {
//创建HashMap集合对象
HashMap<String,String> map=new HashMap<String ,String>();
//添加元素:public V put(K key, V value)
//如果是第一次存储,就直接存储元素,然后返回Null
System.out.println(map.put("001","QQ"));//打印null
//如果有值,就用新值替换旧值,再把旧值返回
System.out.println(map.put("001","微信"));//QQ
System.out.println(map.put("002","QQ"));
System.out.println(map.put("003","windows"));
//判断集合是否包含指定的键
//boolean ContainsKey(Object key)
System.out.println("ContainsKey="+map.containsKey("001"));
//判断集合是否为空
//boolean isEmpty()
System.out.println("isEmpty="+map.isEmpty());
//获取集合中键值对的个数
System.out.println("size="+map.size());
//根据键获取值:v get(Object key)
System.out.println("get="+map.get("003"));
//获取Map集合中的所有键,然后存入set集合
Set<String> set=map.keySet();
//遍历键的集合,获取每一个值
for(String key:set){
System.out.println("values="+map.get(key));
}
//获取集合中所有值的集合:Collection<v> values()
Collection<String> coll=map.values();
//获取所有键值对对象的集合
//Set<Map.Entry<k,v>> entrySet():返回所有键值对的集合
Set<Map.Entry<String, String>> setmap=map.entrySet();
//遍历键值对对象集合,得到每一个键值对
for(Map.Entry<String, String> m:setmap){
System.out.println(m.getKey()+"==="+m.getValue());
}
TreeMap排序:
public static void main(String[] args) {
//使用元素自身的比较性
TreeMap<String,String> tm=new TreeMap<String,String>();
//添加元素
tm.put("c","嘿嘿");
tm.put("a","哈哈");
tm.put("b","呵呵");
tm.put("d","嘻嘻");
//遍历集合,查看输出顺序
Set<Map.Entry<String, String>> set=tm.entrySet();
for(Map.Entry<String,String> s:set){
System.out.println(s.getKey()+"==="+s.getValue());
}
/*结果:
* a===哈哈
b===呵呵
c===嘿嘿
d===嘻嘻
* 这使用字符串的自然排序
*/
//使用自定义的比较器
//创建集合对象并创建比较器
TreeMap<Student,String> tm=new TreeMap<Student,String>(
new Comparator<Student>(){
public int compare(Sudent s1,Student s2){
//主要条件
int num=s1.getAge()-s2.getAge();
//次要条件
int num2=num==0:s1.getName().comparaTo(
s2.getName()):num;
return num2;
}
}
);
//创建学生对象
Student s1=new Student("王小明",17);
Student s2=new Student("张小明",13);
Student s3=new Student("李小明",15);
Student s4=new Student("赵小明",19);
//往集合中添加元素
tm.put(s1,"六班");
tm.put(s4,"四班");
tm.put(s3,"一班");
tm.put(s2,"九班");
//遍历集合
Set<Map.Entry<Student, String>> set=tm.entrySet();
for(Map.Entry<Student, String> m:set){
System.out.println(m.getKey()+"==="+m.getValue());
}