【Java】——Map集合

it2022-06-23  81

java.util.Map<k,v>集合

Map集合的特点:

Map集合是一个双列集合,一个元素包含两个值(一个key,一个value)Map集合中的元素,key和value的数据类型可以相同,也可以不同Map集合中的元素,key是不允许重复的,value是可以重复的Map集合中的元素,key和value是一一对应

java.util.HashMap<k,v>集合 Implements Map<k,v>接口

HashMap集合的特点:

HashMap集合底层是哈希表:查询的速度特别的快HashMap集合是一个无序的集合,存储元素和取出元素的顺序有可能不一致

java.util.LinkedHashMap<k,v>集合 extends HashMap<k,v>集合

LinkedHashMap的特点:

LinkedHashMap集合底层是哈希表+链表(保证迭代的顺序)LinkedHashMap集合是一个有序的集合, 存储元素和取出元素的顺序是一致的

【Map】

常用的方法:

public V put (K key, V value):把指定的键与制定的值添加到Map集合中

返回值:v

存储键值对的时候,key不重复,返回值v是null

存储键值对的时候,key重复,会使用新的value替换map重复的value,返回被替换的value值

private static void show01() { // 创建Map集合对象,多态 Map<String,String> map = new HashMap<>(); String v1 = map.put("韩商言","鱿小鱼"); System.out.println("v1"+v1);//v1:null String v2 = map.put("韩商言","张文慧"); System.out.println("v2"+v2);//v2:张文慧 System.out.println(map);//{韩商言=张文慧} }

public V remove(Object key):把制定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值

返回值:V

key存在,v返回被删除的值

key不存在,v返回null

public static void show02() { //创建map集合对象 Map<String,Integer> map = new HashMap<>(); Map.put("赵丽颖",168); Map.put("林志玲",168); Map.put("张钧蜜",168); System.out.println(map); Integer v1 = map.remove("赵丽颖"); System.out.println("v1"+v1); System.out.println(map); Integer v2 = map.remove("迪丽热巴"); System.out.println("v2"+v2);//v2:null System.out.println(map); }

public V get(Object key) 根据指定的键,在Map集合中获取对应的值。

返回值:

key存在,返回对应的value值

key不存在,返回null

public static void show03() { //创建map集合对象 Map<String,Integer> map = new HashMap<>(); Map.put("赵丽颖",168); Map.put("林志玲",169); Map.put("张钧蜜",170); Integer v1 = map.get("赵丽颖"); System.out.println("v1"+v1);//v1:168 Integer v2 = map.get("迪丽热巴"); System.out.println("v2"+v2);//v2:null }

boolean containsKey (Object key) 判断集合中是否包含指定的键

包含返回true,不包含返回false

private static void show04() { //创建map集合对象 Map<String,Integer> map = new HashMap<>(); Map.put("赵丽颖",168); Map.put("林志玲",169); Map.put("张钧蜜",170); boolean b1 = map.containsKey("赵丽颖"); System.out.println("b1"+b1);//b1:true boolean b2 = map.containsKey("迪丽热巴"); System.out.println("b2"+b2);//v2:false }

Map集合遍历键查找值

方法:

Set<K> keySet() 返回此映射中包含的键的Set视图

实现步骤:

public static void main(String[] args) { //创建Map集合对象 Map<String,Integer> map =new HashMap<>(); map.put("",168); map.put("",168); map.put("",168); //1.使用map集合汇总的方法keySet(),把map集合所有的key取出来,存储到一个set集合中 Set<String> set = map.keySet(); // 或者:Set<Map.Entry<String,Integer>> set = map.entrySet(); //2.遍历set集合,获取map集合中的每一个key //使用迭代器遍历set集合 Iterator<String> it = set.iterator(); // 或者:Map.Entry<Map.Entry<String,Integer>> it = set.iterator(); while (it.hasNext()) { String key = it.next(); //或者:Map.Entry<String,Integer> entry = it.next(); //3.通过map集合中的方法get(key),通过key找到value Integer value = map.get(key); //或者:String key = entry.getKey(); //Integer value = entry.getValue(); System.out.println(key+"="+value); } }

 【HashMap存储自定义类型键值】

key:Sting类型

         String类重写hashCode方法和equals方法,可以保证key唯一

value:Person类型

            value可以重复(同名同龄的人是为同一个)

private static void show01() { // 创建HashMap集合 HashMap<String,Person> map = new HashMap<>(); // 往集合中添加元素 map.put("北京",new Person("李现",18)); map.put("上海",new Person("韩商言",19)); map.put("广州",new Person("现男友",20)); map.put("北京",new Person("宝宝",18)); //使用keySet加增强for遍历Map集合 Set<String> set = map.keySet(); for(String key : set ) { Person value = map.get(key); System.out.println(key+"--->"+value); } }

key:Person类型

           Person类就是必须重写hashCode方法和equals方法,以保证key唯一

value:String类型

            可以重复

private static void show02() { // 创建HashMap集合 HashMap<Person,String> map = new HashMap<>(); // 往集合中添加元素 map.put(new Person("李现",18),"北京"); map.put(new Person("韩商言",19),"上海"); map.put(new Person("现男友",20),"广州"); map.put(new Person("宝宝",18),"北京"); //使用keySet加增强for遍历Map集合 Set<Map.Entry<Person,String> set = map.entrySet(); for(Map.Entry<Person,String> entry : set ) { Person key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"--->"+value); } }

【LinkedHashMap集合】

java.util.LinkedHashMap<K,V> entends HashMap<K,V>

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

底层原理:

        哈希表+链表(记录元素的顺序)

HashMap的key不允许重复,无序

LinkedHashMap的key不允许重复,有序

【番外】

 


最新回复(0)