Map集合
Map集合的特征
1.Map集合里面的元素都是键值成对出现的,一个键对应着一个值。
2.每个键最多只能映射到一个值,但是值可以重复。
3.所有Map集合的数据结构,只跟键有关,跟值没关系。
Map集合是怎么实现键值对应的?
基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)此类不保证映射的顺序,特别是它不保证该顺序恒久不变。
Map接口的实现类
HashMap, Hashtable, LinkedHashMap, TreeMap, Properties;我们主要对HashMap进行应用。
Map集合的常用方法
V put(K key,V value):
添加元素(Map集合中的add);当第一次,这个键去映射一个值的时候,返回的是null;当这个键再次映射一个新值的时候,返回的是上一次映射的值。
Map 集合一个键只能映射一个值,键相同,值就覆盖。
void clear():
清空集合
V remove(Object key):
根据键,删除某个键值对,返回的是删除的这个值
boolean containsKey(Object key):
判断这个集合中有没有这个键
boolean containsValue(Object value):
判断集合中有没有这个值
boolean isEmpty():
判断这个集合是否为空
Set<Map.Entry<K,V>> entrySet():
返回一个键值对的Set集合
V get(Object key):
根据键获取值
Set keySet():
获取集合中所有键的集合
Collection values():
获取集合中所有值的集合
int size():
返回集合中的键值对的对数
具体应用
public class MyTest2 {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<>();
hm.put(1, "范冰冰");
hm.put(2, "李冰冰");
hm.put(3, "白冰冰");
//清空集合
hm.clear();
//清除某一个键值对
String s = hm.remove(1);//根据键,删除某个键值对,返回的是删除的这个值
System.out.println(hm);
//判断集合中有没有这个值
System.out.println(hm.containsValue("范冰冰"));
//判断这个集合中有没有这个键
System.out.println(hm.containsKey(3));
//判断这个集合是否为空
System.out.println(hm.isEmpty());
}
}
取出Map集合中的元素
方法1:基本思想遍历集合,通过键来找到值
public class MyTest3 {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<>();
hm.put(1, "范冰冰");
hm.put(2, "李冰冰");
hm.put(3, "白冰冰");
//map集合的遍历
int size = hm.size();//获取集合的长度
//遍历方式1 使用键找值
Set<Integer> keySet = hm.keySet();//获取键集
根据键获取值 键找值
//String s = hm.get(1);
//System.out.println(s);
for (Integer key : keySet) {
System.out.println(hm.get(key));
}
}
}
方法2:基本思想遍历集合,通过键值对对象找键和值
public class MyTest2 {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<>();
hm.put(1, "范冰冰");
hm.put(2, "李冰冰");
hm.put(3, "白冰冰");
//map集合的遍历方式1 键找值
//map集合的遍历方式2
//Map.Entry<Integer, String> //键值对,对象 Node<K<V>
//getKey()
//返回与此项对应的键。
//V getValue ()
//返回与此项对应的值。
// hm.entrySet(); 获取集合中,键值对,对象的集合
Set<Map.Entry<Integer, String>> entries = hm.entrySet();
//遍历 键值对,对象的集合 通过键值对 对象中的方法,获取键和值
for (Map.Entry<Integer, String> en : entries) {
Integer key = en.getKey();
String value = en.getValue();
System.out.println(key+"==="+value);
}
}
}
LinkedHashMap的特点:
底层的数据结构是链表和哈希表 元素有序 并且唯一,元素的有序性由链表数据结构保证 唯一性由 哈希表数据结构保证。
TreeMap的特点:
键的数据结构是红黑树, 可保证键的排序和唯一性,排序分为自然排序和比较器排序,线程是不安全的效率比较高
public class MyTest2 {
public static void main(String[] args) {
TreeMap<Student, String> hm = new TreeMap<Student, String>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//按照姓名长度来排序
int num = s1.getName().length() - s2.getName().length();
//还得比较内容
int num2=num==0?s1.getName().compareTo(s2.getName()):num;
//还得比较年龄
int num3=num2==0?s1.getAge()-s2.getAge():num2;
return num3;
}
});
hm.put(new Student("张三1", 233), "s001");
hm.put(new Student("张三122", 233), "s001");
hm.put(new Student("张三", 233), "s001");
hm.put(new Student("张三1222", 23), "s001");
hm.put(new Student("张三2221", 231), "s001111");
hm.put(new Student("张三2222222222", 232), "s001");
hm.put(new Student("张三223", 23), "s001");
hm.put(new Student("张三4", 230), "s001");
hm.put(new Student("张三5", 239), "s001");
hm.put(new Student("张三6", 236), "s001");
hm.put(new Student("张三7", 23), "s001");
Set<Map.Entry<Student, String>> entries = hm.entrySet();
for (Map.Entry<Student, String> en : entries) {
Student key = en.getKey();
String value = en.getValue();
System.out.println(key.getName()+"=="+key.getAge()+"========"+value);
}
}
}
HashMap和Hashtable的区别(面试):
HashMap: 线程不安全,效率高.允许null值和null键。
Hashtable: 线程安全 , 效率低.不允许null值和null键。