Map体系集合
1.Map接口
Map接口的特点:
- 用于存储任意键值对
- 键:无序、无下标、不允许重复
- 值:无序、无下标、允许重复
方法:
- V put(K key,V value)//将对象存入到集合中,关联键值.
- Object get(Object key)//根据键获取对应的值;
- Set//返回所有key
- Collection values()//返回包含所有值的Collection集合;
- Set<Map.Entry<K,V>>//键值匹配的Set集合
/**
* Map接口的使用
* 特点:(1)存储键值对(2)键不能重复,值可以重复 (3)无序
*/
public class Demo01 {
public static void main(String[] args) {
//创建Map集合
Map<String,String> map = new HashMap<>();
//1.添加集合
map.put("cn","中国");
map.put("uk","英国");
map.put("usa","美国");
map.put("cn","zhonguo");//添加重复的键,值会进行覆盖
System.out.println("Num:"+map.size());
System.out.println(map.toString());
//2.删除
// map.remove("usa");
// System.out.println("Num:"+map.size());
//3.遍历
//3.1使用KetSet();
System.out.println("====使用KetSet方法");
Set<String> keyset= map.keySet();
for (String str:keyset) {
System.out.println(str+"-"+map.get(str));
}
//3.2使用entrySet()方法
//Entry就是映射对
System.out.println("=====entrySet方法");
Set<Map.Entry<String,String>> entryset= map.entrySet();
for (Map.Entry<String,String> entry:entryset) {
System.out.println(entry.getKey()+"-"+entry.getValue());
}
//4.判断
System.out.println(map.containsKey("cn"));
System.out.println(map.containsValue("泰国"));
}
}
2.HashMap
- JDK1.2版本,线程不安全,运行效率快;允许用null作为key或是value
public class Student {
private String name;
private int stuNo;
public Student() {
}
public Student(String name, int stuNo) {
this.name = name;
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
@Override
public String toString() {
return "[name:"+this.name+" num:"+this.stuNo+"]";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return stuNo == student.stuNo &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, stuNo);
}
}
/**
* HaspMap集合的使用
* 存储结构:哈希表
* 使用key的hashcode和equals作为重复
*/
public static void main(String[] args) {
//创建集合
HashMap<Student,String> hashMap = new HashMap<Student,String>();
//刚创建hashmap没有添加元素 table 为null , size为0 目的节省空间
//1.添加元素
Student s1 = new Student("A",100);
Student s2 = new Student("B",99);
Student s3 = new Student("C",101);
hashMap.put(s1,"beijing");
hashMap.put(s2,"shanghai");
hashMap.put(s3,"hangzhou");
hashMap.put(new Student("A",100),"guangzhou");
System.out.println("Num:"+hashMap.size());
System.out.println(hashMap.toString());
//2.删除
// hashMap.remove(s1);
// System.out.println("Delete:"+hashMap.size());
//3.遍历
//3.1 keySet
System.out.println("===KeySet");
for (Student s:hashMap.keySet()) {
System.out.println(s+"-"+hashMap.get(s));
}
System.out.println("===迭代器");
Set<Student> keySet = hashMap.keySet();
Iterator<Student> iterator = keySet.iterator();
while (iterator.hasNext()){
Student stu = iterator.next();
System.out.println(stu+"-"+hashMap.get(stu));
}
//3.2 entrySet
System.out.println("===entrySet");
//Set< Map.Entry<Student,String> > set = hashMap.entrySet();
for (Map.Entry<Student,String> entry:hashMap.entrySet()) {
System.out.println(entry.getKey()+"-"+entry.getValue());
}
System.out.println("====迭代器");
Set<Map.Entry<Student,String>> entrySet = hashMap.entrySet();
Iterator<Map.Entry<Student,String>> entryIterator = entrySet.iterator();
while (entryIterator.hasNext()){
System.out.println(entryIterator.next());
}
//4.判断
System.out.println(hashMap.containsKey(s1));
System.out.println(hashMap.containsValue("beijing"));
}
总结
(1)HashMap刚创建时,table是null,为了节省空间,当添加第一个元素时,table的容量调整为16
(2)当元素个数大于阈值(16*0.75=12)时,会进行扩容,扩容大小为原来的2倍。目的是减少调整元素的个数
(3)JDK1.8 当每个链表长度大于8,并且数组元素个数大于等于64时,会调整为红黑树,目的是提高执行效率
(4)JDK1.8 当链表长度小于6时,调整为链表
(5)JDK1.8以前,链表是头插入;JDK1.8以后是尾插入
Hashtable:JDK1.0版本,线程安全,运行效率慢,不允许null作为key或是value.
Properties:Hashtable的子类,要求key和value都是String。通常用于配置文件的读取.
3.TreeMap
实现了SortedMap接口,可以对key自动排序
TreeSet由TreeMap实现
/**
* TreeMap的使用
* 存储结构:红黑树
*/
public class Demo01 {
public static void main(String[] args) {
//创建集合
TreeMap<Student,String> treeMap = new TreeMap<Student,String>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//int n1 = o1.getName().compareTo(o2.getName());
//int n2 = o1.getStuNo() - o2.getStuNo();
//return n1==0?n2:n1;
int n2 = o1.getStuNo() - o2.getStuNo();
return n2;
}
});
//1.添加元素
Student s1 = new Student("A",100);
Student s2 = new Student("B",99);
Student s3 = new Student("C",101);
treeMap.put(s1,"beijing");
treeMap.put(s2,"nanjing");
treeMap.put(s3,"shanghai");
System.out.println("NUM:"+treeMap.size());
System.out.println(treeMap.toString());
//2.删除
// treeMap.remove(s1);
// System.out.println("Num:"+treeMap.size());
//3.遍历
//3.1使用KEYset
System.out.println("===KetSet");
for (Student s : treeMap.keySet()) {
System.out.println(s+"-"+treeMap.get(s));
}
//3.2使用entryset
System.out.println("===EntrySet");
for (Map.Entry<Student,String> entry:treeMap.entrySet()) {
System.out.println(entry.getKey()+"-"+entry.getValue());
}
//4.判断
System.out.println(treeMap.containsKey(new Student("A",100)));
}
}