Map集合的特点

  • 键值对映射关系
  • 一个键对应一个值
  • 键不能重复,值可以重复
  • 元素存取无序
    Map集合的基本使用
public class MapDemo {
    public static void main(String[] args) {
        //创建集合对象
        Map<String,String> map = new HashMap<String,String>();
        //V put(K key, V value) 将指定的值与该映射中的指定键相关联
        map.put("1","一");
        map.put("2","二");
        map.put("3","三");
        map.put("3","四");
        //输出集合对象
        System.out.println(map);//{1=一, 2=二, 3=四}
    }
}

Map集合的基本功能

方法名

说明

V put(K key,V value)

添加元素

V remove(Object key)

根据键删除键值对元素

void clear()

移除所有的键值对元素

boolean containsKey(Object key)

判断集合是否包含指定的键

boolean containsValue(Object value)

判断集合是否包含指定的值

boolean isEmpty()

判断集合是否为空

int size())

集合的长度,也就是集合中键值对的个数

public class MapDemo {
    public static void main(String[] args) {
        //创建集合对象
        Map<String, String> map = new HashMap<String, String>();
        //V put(K key,V value):添加元素
        map.put("詹姆斯", "湖人");
        map.put("杜兰特", "篮网");
        map.put("库里", "勇士");

        //V remove(Object key):根据键删除键值对元素
        System.out.println(map.remove("库里")); // 勇士
        //boolean containsKey(Object key):判断集合是否包含指定的键
        System.out.println(map.containsKey("詹姆斯"));//true
        //boolean containsValue(Object value) 判断集合是否包含指定的值
        System.out.println(map.containsValue("篮网"));//true
        //boolean isEmpty():判断集合是否为空
        System.out.println(map.isEmpty());//false
        //void clear():移除所有的键值对元素
        map.clear();
        //int size():集合的长度,也就是集合中键值对的个数
        System.out.println(map.size());//0
        //输出集合对象
        System.out.println(map);
    }
}

Map集合的获取功能

方法名

说明

V get(Object key)

根据键获取值

Set keySet()

获取所有键的集合

Collection values()

获取所有值的集合

Set<Map.Entry<K,V>> entrySet()

获取所有键值对对象的集合

public class MapDemo01 {
    public static void main(String[] args) {
        //创建集合对象
        Map<String, String> map = new HashMap<String, String>();
        //添加元素
        map.put("科比", "湖人");
        map.put("姚明", "火箭");
        map.put("诺维斯基", "小牛");
        //V get(Object key):根据键获取值
        System.out.println(map.get("科比"));//湖人
        System.out.println(map.get("詹姆斯"));//null

        //Set<K> keySet():获取所有键的集合
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println(key);
        }
        //Collection<V> values():获取所有值的集合
        Collection<String> values = map.values();
        for (String value : values) {
            System.out.println(value);
        }
      //Set<Map.Entry<K,V>> entrySet() 获取所有键值对对象的集合
        Set<Map.Entry<String, String>> keyValues = map.entrySet();
        for (Map.Entry<String, String> m : keyValues) {
            //根据键值对对象获取键和值
            String key = m.getKey();
            String value = m.getValue();
            System.out.println(key + "," + value);

        }
    }
}

Map集合的遍历
方式一:

  • 用keySet()方法获取所有键的集合
  • 用增强for实现遍历键的集合,获取到每一个键
  • 用get(Object key)方法根据键去找值
public class MapDemo02 {
    public static void main(String[] args) {
        //创建集合对象
        Map<String, String> map = new HashMap<String, String>();
        //添加元素
        map.put("张无忌", "赵敏");
        map.put("郭靖", "黄蓉");
        map.put("杨过", "小龙女");
        //获取所有键的集合。用keySet()方法实现
        Set<String> keySet = map.keySet();
        //遍历键的集合,获取到每一个键。用增强for实现
        for (String key : keySet) {
            //根据键去找值。用get(Object key)方法实现
            String value = map.get(key);
            System.out.println(key + "," + value);
        }
    }

}

方式二

  • 使用Set<Map.Entry<K,V>> entrySet() 获取所有键值对对象的集合
  • 用增强for得到每一个Map.Entry
  • 根据键值对对象获取键和值
public class MapDemo03 {
    public static void main(String[] args) {
        //创建集合对象
        Map<String, String> map = new HashMap<String, String>();
        //添加元素
        map.put("张无忌", "赵敏");
        map.put("郭靖", "黄蓉");
        map.put("杨过", "小龙女");
        //获取所有键值对对象的集合
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        //遍历键值对对象的集合,得到每一个键值对对象
        for (Map.Entry<String, String> me : entrySet) {
        //根据键值对对象获取键和值
            String key = me.getKey();
            String value = me.getValue();
            System.out.println(key + "," + value);
        }
    }
}