Map集合的一些知识

    技术2022-07-11  123

    概述

    Map集合是双列集合,一个元素包含两个值(key和value)

    Map集合中的元素,key和value数据类型可以相同也可以不同

    Map集合中的元素,key不允许重复,value可以重复

    key和value是一一对应的

    Map公用方法

    public V put(K key, V value):把指定的键与指定的值添加到Map集合中,key不重复时,返回值V为null;key重复的时候,会使用新的value替换Map中的value,返回被替换的value值

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

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

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

    public Set keySet():获取Map集合中所有的键,储存到Set集合中

    public Set<Map.Entry<K, V>> entrySet():获取到Map集合中所有键值对对象的集合(Set集合)

    遍历Map集合

    键找值的方法

    使用Map集合中 keySet()方法将集合中的key取出,储存到Set集合中

    利用遍历器或增强for循环遍历Set集合,获取每一个key

    使用Map集合中get(Object key)方法,通过key获取value

    public class IteratorDemo01 { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put(“garen”, 100); map.put(“temmo”, 300); map.put(“angel”, 100);

    Set<String> key = map.keySet(); //迭代器的方法 Iterator<String> iterator = key.iterator(); while (iterator.hasNext()){ String next = iterator.next(); Integer value = map.get(next); System.out.println(next+"="+value); } System.out.println("================="); //增强for循环方法 for (String s : map.keySet()) { Integer value = map.get(s); System.out.println(s+"="+value); } }

    } 键值对方法

    使用Map集合中 entrySet()方法将集合中的Entry对象取出,储存到Set集合中

    利用遍历器或增强for循环遍历Set集合,获取每一个Entry对象

    使用Entry对象中的方法getKey()和getValue()方法获取键和值

    public class IteratorDemo02 { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put(“garen”, 100); map.put(“temmo”, 300); map.put(“angel”, 100);

    Set<Map.Entry<String, Integer>> entry = map.entrySet(); //迭代器方法 Iterator<Map.Entry<String, Integer>> iterator = entry.iterator(); while (iterator.hasNext()){ Map.Entry<String, Integer> next = iterator.next(); String key = next.getKey(); Integer value = next.getValue(); System.out.println(key+"="+value); } System.out.println("================="); //增强for循环方法 for (Map.Entry<String, Integer> m :entry) { String key = m.getKey(); Integer value = m.getValue(); System.out.println(key+"="+value); } }

    }

    HashMap集合

    HashMap集合底层是哈希表(数组+单向链表/红黑树),查询速度快

    HashMap集合是一个无序的集合,存储元素和取出元素的顺序可能不一致

    HashMap集合存储自定义键值,key元素的hashCode和equals方法需要覆盖重写

    public class Hero { private String name; float hp; public Hero(String name, float hp) { this.name = name; this.hp = hp; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getHp() { return hp; } public void setHp(float hp) { this.hp = hp; } @Override public String toString() { return "Hero{" + "name='" + name + '\'' + ", hp=" + hp + '}'; } //覆盖重写hashCode和equals方法 @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Hero hero = (Hero) o; return Float.compare(hero.hp, hp) == 0 && Objects.equals(name, hero.name); } @Override public int hashCode() { return Objects.hash(name, hp); } } / import java.util.HashMap; import java.util.Map; import java.util.Set; public class HashMapDemo01 { public static void main(String[] args) { HashMap<Hero, String> hashMap = new HashMap<>(); //key不能重复,value可以重复 hashMap.put(new Hero("garen", 100), "Hero01"); hashMap.put(new Hero("temmo", 200), "Hero01"); hashMap.put(new Hero("angel", 300), "Hero02"); hashMap.put(new Hero("garen", 100), "ADHero"); Set<Hero> set = hashMap.keySet(); for (Hero key : set) { String value = hashMap.get(key); System.out.println(key+"-->"+value); } System.out.println("============================"); Set<Map.Entry<Hero, String>> entrySet = hashMap.entrySet(); for (Map.Entry<Hero, String> entry : entrySet) { Hero key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"-->"+value); } } }

    LinkedHashMap集合

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

    静态方法of

    ** List接口,Set接口,Map接口内有静态方法of,可以给集合一次性添加多个元素

    List list = List.of(E…);

    Set set = Set.of(E…);

    Map map = Map.of(E…);

    of方法只适用于List接口,Set接口和Map接口,不适用于它们的实现类

    of方法返回值是一个不能改变的集合,集合不能再使用add,put等方法添加元素,否则会抛出异常

    Set接口和Map接口在调用of方法时不能有重复的元素,否则会抛出异常

    转自:https://www.cnblogs.com/geqianLee/p/13215731.html

    Processed: 0.011, SQL: 9