java的map集合
(map集合)双列集合的体系:
———–| Map 实现了Map接口的集合类具备的特点: 存储的数据都是以键值对的形式存在的, 键(key )不能重复,值(value)可以重复
————-| HashMap 底层是依赖于哈希码去实现的,存储的位置是根据键的哈希码算出来的
————-| TreeMap 底层也是使用了红黑树数据结构实现的 特点:会根据键的数据进行排序存储
————-| Hashtable(了解)
HasMap存储原理:
往HashMap 存储元素的时候,首先hashMap会调用键的hashCode方法得到一个哈希码值,然后通过哈希码值就可以算出该元素在哈希表中的存储位置
- 情况一:如果根据键的哈希码值算出的位置目前没有任何元素存储了,那么该元素可以直接添加到哈希表中
- 情况二:如果根据键的哈希码值算出的位置目前已经其他元素存储了,那么还会调用键的equals方法与这个位置的元素再比较一次,如果equals返回的是true 那么该元素视为重复元素,不允许添加,如果equals方法返回false,该元素可以被添加。
class Person {
int id;
String name;
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
@Override
public int hashCode() {
return this.id;
}
@Override
public boolean equals(Object obj) {
Person p = (Person) obj;
return this.id == p.id;
}
}
public class Demo2 {
public static void main(String[] args) {
HashMap<Person, String> map = new HashMap<Person, String>();
map.put(new Person(110, "狗娃"), "001");
map.put(new Person(120, "狗剩"), "002");
map.put(new Person(130, "铁蛋"), "003");
// 如果身份证编号一致,那么就视为重复元素,不允许添加
map.put(new Person(130, "天爱迪生"), "007");
System.out.println(map);
}
}
TreeMap要注意的事项:
- 往TreeMap 存储元素的时候,如果键不具备自然顺序的特性,那么键所属的类就必须去实现Comparable接口
- 往TreeMap 存储元素的时候,如果键不具备自然顺序的特性,而且键所属的类就没有实现Comparable接口,那么必须要在创建treeMap对象的时候传入一个比较器对象
class Emp {
String name;
int salary;
public Emp(String name,int salary){
super();
this.name=name;
this.salary=salary;
}
@Override
public String toString() {
return "Emp [name=" + name + ", salary=" + salary + "]";
}
/* @Override
public int compareTo(Emp o) {
return this.salary-o.salary;
}*/
}
//自定义一个比较器
class AgeComparaTor implements Comparator<Emp>{
@Override
public int compare(Emp o1, Emp o2) {
return o1.salary-o2.salary;
}
}
public class Demo3 {
public static void main(String[] args) {
TreeMap<Emp, String> tree = new TreeMap<Emp,String>(new AgeComparaTor());
tree.put(new Emp("老钟",800), "001");
tree.put(new Emp("老杨",1000), "002");
tree.put(new Emp("老张",2000), "002");
tree.put(new Emp("老黎",100000), "003");
tree.put(new Emp("文婷",800), "008"); //出现相同的键800,新的值会把旧的值替换
System.out.println("集合元素"+tree);
}
}
map集合中常见的使用方法:
添加
(1) V put(K key, V value) (可以相同的key值,但是添加的value值会覆盖前面的,返回值是前一个,如果没有就返回null)
(2)putAll(Map<>) 从指定映射中将所有映射关系复制到此映射中(可选操作)。删除
1、remove() 删除关联对象,指定key对象
2、clear() 清空集合对象- 获取
1:value get(key); 可以用于判断键是否存在的情况。当指定的键不存在的时候,返回的是null。 - 判断
1、boolean isEmpty() 长度为0返回true否则false
2、boolean containsKey(Object key) 判断集合中是否包含指定的key
3、boolean containsValue(Object value) 判断集合中是否包含指定的value - 长度
Int size()
Map集合的遍历方式
- 将map 集合中所有的键取出存入set集合。Set keySet() 返回所有的key对象的Set集合再通过get方法获取键对应的值。
- values() ,获取所有的值.Collection values()不能获取到key对象
- Map.Entry对象 推荐使用 重点
map 集合中的键值映射关系打包成一个对象
Map.Entry对象通过Map.Entry 对象的getKey,
getValue获取其键和值。
第一种方式: 使用keySet
// 需要分别获取key和value,没有面向对象的思想
// Set<K> keySet() 返回所有的key对象的Set集合
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "aaaa");
map.put(2, "bbbb");
map.put(3, "cccc");
System.out.println(map);
Set<Integer> ks = map.keySet();
Iterator<Integer> it = ks.iterator();
while (it.hasNext()) {
Integer key = it.next();
String value = map.get(key);
System.out.println("key=" + key + " value=" + value);
}
第二种方式: 通过values 获取所有值,不能获取到key对象
// 通过values 获取所有值,不能获取到key对象
// Collection<V> values()
Collection<String> vs = map.values();
Iterator<String> it = vs.iterator();
while (it.hasNext()) {
String value = it.next();
System.out.println(" value=" + value);
}
第三种方式 Map.Entry
面向对象的思想将map集合中的键和值映射关系打包为一个对象,就是Map.Entry,将该对象存入Set集合,Map.Entry是一个对象,那么该对象具备的getKey,getValue获得键和值。
// Set<Map.Entry<K,V>> entrySet()
// 返回的Map.Entry对象的Set集合 Map.Entry包含了key和value对象
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "aaaa");
map.put(2, "bbbb");
map.put(3, "cccc");
Set<Map.Entry<Integer, String>> es = map.entrySet();
Iterator<Map.Entry<Integer, String>> it = es.iterator();
while (it.hasNext()) {
// 返回的是封装了key和value对象的Map.Entry对象
Map.Entry<Integer, String> en = it.next();
// 获取Map.Entry对象中封装的key和value对象
Integer key = en.getKey();
String value = en.getValue();
System.out.println("key=" + key + " value=" + value);
}
这里写代码片