Java集合框架——Map接口 & HashMap类

Map 接口 & HashMap类

一、API中的Map集合

  • Map集合是将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
    在这里插入图片描述
    二、Map集合和Collection集合的区别

  • Collection集合是单列集合,Map集合是双列集合
    在这里插入图片描述

三、Map集合的继承体系

  • Map
    • TreeMap
    • HashMap
      • LinkedHashMap

四、Map集合的特性

  • Key:键(索引),Value:数据(值)
  • 键是不能重复的,数据是可以重复的
  • Map集合中的键和值都是引用数据类型
  • Map集合中的所有数据结构都是针对键的
  • key→value称作映射,Map集合中的映射是一一对应的

五、Map集合的方法

  • 添加功能:
  • V put(K key, V value) :将指定的值与此映射中的指定键关联
package com.map.demo;

import java.util.HashMap;
import java.util.Map;

public class MapTest {
    
    
	public static void main(String[] args) {
    
    
		//创建Map集合
		Map<String, Integer> map = new HashMap<String, Integer>();
		
		//①添加功能:V put(K key, V value)
		map.put("张三", 18);
		map.put("李四", 19);
		//如果是第一次往Map集合中添加某键值对,它的返回值为null
		System.out.println(map.put("王五", 20));  // null
		//如果第二次添加相同的键,但值不同的话,就视为修改键相对应的值,返回值为被修改的值
		System.out.println(map.put("王五", 21));  // 20
		
		System.out.println(map);  // {李四=19, 张三=18, 王五=21}
	}
}
  • 删除功能:
  • void clear() :从此映射中移除所有映射关系
  • V remove(Object key):如果存在一个键的映射关系,则将其从此映射中移除
package com.map.demo;

import java.util.HashMap;
import java.util.Map;

public class MapTest2 {
    
    
	public static void main(String[] args) {
    
    
		//创建Map集合
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("张三", 18);
		map.put("李四", 19);
		map.put("王五", 20);
		map.put("赵六", 21);
		
		//②删除功能:V remove(Object key)
		//返回值为被删除键所对应的值
		System.out.println(map.remove("赵六"));  // 21
		System.out.println(map);  // {李四=19, 张三=18, 王五=20}
		
		//②删除功能:void clear()
		map.clear();
		System.out.println(map);  // {}
	}
}
  • 判断功能:
  • boolean containsKey(Object key):如果此映射包含指定键的映射关系,则返回值为 true
  • boolean containsValue(Object value):如果此映射将一个或多个键映射到指定值,则返回 true
  • boolean isEmpty():如果此映射未包含键-值映射关系,则返回 true
package com.map.demo;

import java.util.HashMap;
import java.util.Map;

public class MapTest3 {
    
    
	public static void main(String[] args) {
    
    
		//创建Map集合
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("张三", 18);
		map.put("李四", 19);
		map.put("王五", 20);
		map.put("赵六", 21);
		
		//③判断功能:boolean containsKey(Object key)
		boolean rst1 = map.containsKey("张三");
		System.out.println(rst1);  // true
		boolean rst2 = map.containsKey("张五");
		System.out.println(rst2);  // false
		
		//③判断功能:boolean containsValue(Object value)
		boolean rst3 = map.containsValue(18);
		System.out.println(rst3);  // true
		boolean rst4 = map.containsValue(22);
		System.out.println(rst4);  // false
		
		//③判断功能:boolean isEmpty()
		System.out.println(map.isEmpty());  // false
	}
}
  • 获取功能:
  • V get(Object key):返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null
  • Set<K> keySet():返回此映射中包含的键的 Set 视图
  • Collection<V> values():返回此映射中包含的值的 Collection 视图
package com.map.demo;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapTest4 {
    
    
	public static void main(String[] args) {
    
    
		//创建Map集合
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("张三", 18);
		map.put("李四", 19);
		map.put("王五", 20);
		map.put("赵六", 21);
		
		//④获取功能:V get(Object key)
		Integer value1 = map.get("李四");
		System.out.println(value1);  // 19
		
		//④获取功能:Set\<K> keySet()
		Set<String> keySet = map.keySet();
		for (String str : keySet) {
    
    
			System.out.println(str);
			/* 李四
			 * 张三
			 * 王五
			 * 赵六
			 * */
		}
		
		//④获取功能:Collection\<V> values()
		Collection<Integer> value2 = map.values();
		for (Integer age : value2) {
    
    
			System.out.println(age);
			/* 19
			 * 18
			 * 20
			 * 21
			 * */
		}
	}
}
  • 长度功能:
  • int size():返回此映射中的键-值映射关系数
package com.map.demo;

import java.util.HashMap;
import java.util.Map;

public class MapTest6 {
    
    
	public static void main(String[] args) {
    
    
		//创建Map集合
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("张三", 18);
		map.put("李四", 19);
		map.put("王五", 20);
		map.put("赵六", 21);
		
		//⑤长度功能:int size()
		int size = map.size();
		System.out.println(size);  // 4
	}
}

六、HashMap如何判断键的相同

七、Map集合的遍历方式之一:entrySet()方法

  • Set<Map.Entry<K,V>> entrySet():返回此映射中包含的映射关系的 Set 视图
    在这里插入图片描述
  • Entry(实体,键值对)是Map集合的内部接口(以下为源码)
public interface Map {
    
    
	interface Entry<K, V> {
    
    
        K getKey();

        V getValue();

        V setValue(V value);

        boolean equals(Object o);

        int hashCode();

        public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K, V>> comparingByKey() {
    
    
            return (Comparator<Map.Entry<K, V>> & Serializable)
                (c1, c2) -> c1.getKey().compareTo(c2.getKey());
        }

        public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K, V>> comparingByValue() {
    
    
            return (Comparator<Map.Entry<K, V>> & Serializable)
                (c1, c2) -> c1.getValue().compareTo(c2.getValue());
        }

        public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
    
    
            Objects.requireNonNull(cmp);
            return (Comparator<Map.Entry<K, V>> & Serializable)
                (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
        }

         */
        public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
    
    
            Objects.requireNonNull(cmp);
            return (Comparator<Map.Entry<K, V>> & Serializable)
                (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
        }
    }
}
  • entrySet()方法的实际作用:返回Map集合中所有的键值对
  • 即Map集合的一种遍历方式
  • 可以通过entry的getKey()和getValue()方法得到键和值
package com.map.demo;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapTest4 {
    
    
	public static void main(String[] args) {
    
    
		//创建Map集合
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("张三", 18);
		map.put("李四", 19);
		map.put("王五", 20);
		map.put("赵六", 21);
		
		//Set<Map.Entry<K,V>> entrySet()
		Set<Entry<String, Integer>> set = map.entrySet();
		for (Entry<String, Integer> entry : set) {
    
    
			System.out.println("key:" + entry.getKey() + "   value:" + entry.getValue());
		}
	}
}
***执行结果:***
key:李四   value:19
key:张三   value:18
key:王五   value:20
key:赵六   value:21

八、Map集合的遍历方式之二

  • 通过Map集合的keySet方法得到key的集合并遍历,在遍历的过程中,通过key去找到对象的value,以此来遍历Map集合
package com.map.demo;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapTest5 {
    
    
	public static void main(String[] args) {
    
    
		//创建Map集合
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("张三", 18);
		map.put("李四", 19);
		map.put("王五", 20);
		map.put("赵六", 21);
		
		//遍历方式二
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
    
    
			Integer value = map.get(key);
			System.out.println("key:" + key + "   value:" + value);
		}
	}
}
***执行结果:***
key:李四   value:19
key:张三   value:18
key:王五   value:20
key:赵六   value:21

猜你喜欢

转载自blog.csdn.net/weixin_43796325/article/details/104433672