4. HashMap
●HashMap集合特点
。是哈希表结构
。保证键唯- -性用于键的对象必须重写hashCode,equals方法
。线程不安全集合,运行速度快
。集合运行使用nul.作为键或者值
package com.sdjzu.hashmap;
import java.util.HashMap;
import java.util.Map;
public class HashMapTest {
public static void main(String[] args) {
method();
}
public static void method(){
Map<String,Person> map=new HashMap<>();
map.put("a",new Person("张三",78));
map.put("b",new Person("张三",78));
map.put("c",new Person("张三",78));
map.put("d",new Person("张三",78));
//将键值放在set集合,遍历set集合
//Set<String> set= map.keySet();
//for循环
for(String key:map.keySet()){
//通过键值找到值get方法
//Person person = map.get(key);
//输出
System.out.println(key+"="+map.get(key));
}
System.out.println("*****************");
for (Map.Entry<String, Person> stringPersonEntry : map.entrySet()) {
System.out.println( stringPersonEntry.getKey()+"="+stringPersonEntry.getValue());
}
}
}
D:\developertool\Java\jdk1.8.0\bin\java.exe "-javaagent:D:\developertool\Java\IntelliJ IDEA 2021.2.2\lib\idea_rt.jar=59807:D:\developertool\Java\IntelliJ IDEA 2021.2.2\bin" -Dfile.encoding=UTF-8 -classpath D:\developertool\Java\jdk1.8.0\jre\lib\charsets.jar;D:\developertool\Java\jdk1.8.0\jre\lib\deploy.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\access-bridge-64.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\cldrdata.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\dnsns.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\jaccess.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\jfxrt.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\localedata.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\nashorn.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\sunec.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\sunjce_provider.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\sunmscapi.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\sunpkcs11.jar;D:\developertool\Java\jdk1.8.0\jre\lib\ext\zipfs.jar;D:\developertool\Java\jdk1.8.0\jre\lib\javaws.jar;D:\developertool\Java\jdk1.8.0\jre\lib\jce.jar;D:\developertool\Java\jdk1.8.0\jre\lib\jfr.jar;D:\developertool\Java\jdk1.8.0\jre\lib\jfxswt.jar;D:\developertool\Java\jdk1.8.0\jre\lib\jsse.jar;D:\developertool\Java\jdk1.8.0\jre\lib\management-agent.jar;D:\developertool\Java\jdk1.8.0\jre\lib\plugin.jar;D:\developertool\Java\jdk1.8.0\jre\lib\resources.jar;D:\developertool\Java\jdk1.8.0\jre\lib\rt.jar;D:\developertool\Java\idea202122space\JAVAStage\out\production\module10-map com.sdjzu.hashmap.HashMapTest
a=Person{name='张三', age=78}
b=Person{name='张三', age=78}
c=Person{name='张三', age=78}
d=Person{name='张三', age=78}
*****************
a=Person{name='张三', age=78}
b=Person{name='张三', age=78}
c=Person{name='张三', age=78}
d=Person{name='张三', age=78}
Process finished with exit code 0
5. LinkedHashMap
LinkedHashMap继承HashMap实现Map接口,LinkedHashMap底层实现原理是哈
希表,双向链存取有序.其它的特性和父类HashMap-样
6. Hashtable
Map接口的实现类Hashtable, Hashtable类诞生于JDK1.0版本, Map接口诞生于
JDK1.2版本. Hashtable类从DK1 .2开始,改进为实现Map接口
●Hashtable类的特点
。底层数据结构是哈希表
。线程安全的运行速度慢被更加先进的HashMap取代
。不允许null值null键存储null直接抛出空指针异常
7. Vector集合类
List接口的实现Vector,命运和Hashtable-样.
●Vector类的特点”
。底层实现结构是数组
。数组的默认容量是10,每次扩容是原来的长度*2
。线程安全,运行速度慢被ArrayList取代
8. TreeMap集合
●TreeMap集合的特点
。底层实现是红黑树结构(添加登询速度比较快)
。存储到TreeMap中元素,对键进行排序
。排序依据:
■对象的自然顺序,作为键的对象,实现了接口Comparable
■自己提供比较器实现接口Comparator