Java集合之Map接口及其实现类

一、Map接口

Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value;
Map中的键值对以Entry类型的对象实例形式存在;
键(key值)不可重复,value值可以重复,一个value值可以和很多key值形成对应关系,每个建最多只能映射到一个值。
Map支持泛型,形式如:Map<K,V>
Map中使用put(K key,V value)方法添加Map接口中定义的常用方法
具体使用在实现类中讨论

int size();//获取Map集合大小(即元素数量)
boolean isEmpty();//判断是否为空
boolean containsKey(Object key);//判断是否包含某个键
boolean containsValue(Object value);//判断是否包含某个值
V get(Object key);//获取某个键对应的值
V put(K key, V value);//添加键值对(K,V)
V remove(Object key);//移除某个键对应的键值对
void putAll(Map<? extends K, ? extends V> m);//添加另一个Map集合
void clear();//清空所有键值对
Set<K> keySet();//获取键的集合
Collection<V> values();//获取值的集合
Set<Map.Entry<K, V>> entrySet();//获取键值对实体的集合
interface Entry<K,V>//Map中的内部接口

二、HashMap

基于哈希表的 Map 接口实现。此实现提供所有可选的映射操作,并允许使用 null 值null 键。(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)除实现了Map接口外还实现了Cloneable,Serializable,继承了AbstractMap抽象类。此类不保证映射的顺序,特别是它不保证该顺序恒久不变。
特点:

(1)键无序,唯一,类似于Set集合
(2)值有序,可重复,类似于List
(3)底层数据结构是哈希表,保证键唯一
(4)允许键为null,值为null

//		HashMap<String, Student> hm = new HashMap<String, Student>();
//		hm.put("2018050401", new Student("2018050401", "张三", 18, 80.0));
//		hm.put("2018050402", new Student("2018050402", "李四", 18, 80.0));
//		hm.put("2018050403", new Student("2018050403", "李四", 18, 80.0));
//		hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0));
//		hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0));
//		
//		// 方式一: 通过键找值
//		Set<String> keys = hm.keySet();
//		for (String key : keys) {
//			Student s = hm.get(key);
//			System.out.println(key + "|" + s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore());
//		}

		HashMap<Student, String> hm = new HashMap<Student, String>();
		hm.put(new Student("2018050401", "张三", 18, 80.0),"2018050401");
		hm.put(new Student("2018050402", "李四", 18, 80.0),"2018050402");
		hm.put(new Student("2018050403", "李四", 18, 80.0), "2018050403");
		hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404");
		hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404");
		
		// 方式二: 通过键值对对象找键找值
		Set<Entry<Student, String>> keyValues = hm.entrySet();
		for (Entry<Student, String> keyValue : keyValues) {
			Student s = keyValue.getKey();
			String value = keyValue.getValue();
			System.out.println(s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore() + "=" + value);
		}

三、LinkedHashMap

由Map接口的哈希表链表实现,具有可预知的迭代顺序

特点:

(1)键有序,唯一,
(2)值有序,可重复,类似于List
(3)底层数据结构是哈希表和链表,哈希表保证键唯一,链表保证键有序

	LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>();
		lhm.put(01, "张三1");
		lhm.put(02, "张三2");
		lhm.put(03, "张三3");
		lhm.put(04, "张三4");
		lhm.put(05, "张三5");
		
		Set<Integer> keys = lhm.keySet();
		for (Integer key : keys) {
			System.out.println(key + "|" + lhm.get(key));
		}

四、TreeMap

基于红黑树(Red-Black tree)的 NavigableMap 实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序
具体取决于使用的构造方法。

特点:

(1)键可排序,唯一,
(2)值有序,可重复,类似于List
(3)底层数据结构是自平衡的二叉树,可排序
(4)排序方式类似于TreeSet,分为自然排序和比较器排序,具体取决于使用的构造方法

   TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
   	tm.put(24, "Hello1");
   	tm.put(14, "Hello2");
   	tm.put(34, "Hello3");
   	tm.put(124, "Hello4");
   	tm.put(24, "Hello5");
   	tm.put(24, "Hello6");
   	tm.put(24, "Hello7");
   	tm.put(244, "Hello8");
   	tm.put(624, "Hello9");
   	tm.put(24, "Hello10");
   	Set<Integer> keys = tm.keySet();
   	for (Integer key : keys) {
   		String value = tm.get(key);
   		System.out.println(key + "|" + value);
   	}

五、HashTable

此类实现一个哈希表该哈希表将键映射到相应的值,任何非 null 对象都可以用作键或值

特点:

(1)不允许null键和null值
(2)线程安全,效率低

HashMap和Hashtable的区别:

(1)HashMap是不安全的不同步的效率高的 允许null键和null值
(2)Hashtable是安全的同步的效率低的不允许null键和null值
(3)底层都是哈希表结构

Hashtable<String, String> hashtable = new Hashtable<String, String>();
		hashtable.put("刘备", "孙尚香");
		hashtable.put("孙策", "大乔");
		hashtable.put("周瑜", "小乔");
		hashtable.put("吕布", "貂蝉");
		System.out.println(hashtable);
		Enumeration<String> keys = hashtable.keys();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			String value = hashtable.get(key);
			System.out.println(key + "|" + value);
		}

六、WeakHashMap

以弱键 实现的基于哈希表的 Map。在 WeakHashMap 中,当某个键不再正常使用时,将自动移除其条目。更精确地说,对于一个给定的键,其映射的存在并不阻止垃圾回收器对该键的丢弃,这就使该键成为可终止的,被终止,然后被回收。
丢弃某个键时,其条目从映射中有效地移除,因此,该类的行为与其他的 Map 实现有所不同。

WeakHashMap<String,String> whm = new WeakHashMap<>();
		whm.put(new String("hello1"), "world1");
		whm.put(new String("hello2"), "world2");
		whm.put(new String("hello3"), "world3");
		whm.put("hello4", "world3");
		System.out.println(whm);
		System.gc();
		System.runFinalization();
		System.out.println(whm);

七、EnumMap

键是枚举类型

EnumMap<Direction, String> em = new EnumMap<>(Direction.class);
		em.put(Direction.UP, "向上移动");
		em.put(Direction.DOWN, "向下移动");
		em.put(Direction.LEFT, "向左移动");
		em.put(Direction.RIGHT, "向右移动");
		
		Set<Direction> keys = em.keySet();
		for (Direction key : keys) {
			String value = em.get(key);
			System.out.println(key + "|" + value);
		}

猜你喜欢

转载自blog.csdn.net/weixin_43520450/article/details/106952722