Map集合

Map集合

Map集合是双列集合, 是一种一一对应关系,就是映射,Java提供 java,util.Map接口供我们使用。

Map集合常用子类

我们最常用的是HashMap和LinkedHashMap

  • HashMap<K, V> :存储数据采用的哈希表结构, 元素存储的顺序不能保证一致, 由于要保证键的唯一、不重复,需要重写键的hashCode()方法、equals方法()。

Map接口常用方法

  • public V put(K key, V value); 把指定的键和值添加到集合中
  • public V get(Object key); 根据键获取对应的值
  • public V remove(Object key); 根据键删除对应的值,返回被删除元素的值
  • public boolean containsKey(Object key);判断集合中是否包含指定的键
public class Demo {
    public static void main(String[] args) {
        // 创建map对象
        HashMap<String, String> map = new HashMap<>();
        map.put("吕布", "貂蝉");
        map.put("孙策", "大乔");
        map.put("周瑜", "二乔");
        map.put("刘备", "甘夫人");
        // 如果put时间存在,那么会覆盖之前的值
        map.put("刘备", "孙尚香");
        System.out.println(map);
        // 访问
        String s = map.get("刘备");
        String s2 = map.get("吕布");
        System.out.println(s);
        System.out.println(s2);
        // 删除
        String remove = map.remove("孙策");
        System.out.println(remove);

        System.out.println(map.containsKey("刘备")); // true
        System.out.println(map.containsKey("孙策")); // false
    }
}

  • public Set<K> keySet() ;获取所有键,存储到set集合中
public class Demo2 {
    public static void main(String[] args) {
        // 创建map对象
        HashMap<String, String> map = new HashMap<>();
        map.put("吕布", "貂蝉");
        map.put("孙策", "大乔");
        map.put("周瑜", "二乔");
        map.put("刘备", "甘夫人");
        // 获取所有的键
        Set<String> keys = map.keySet();
        // 遍历所有的键
        for(String key : keys){
            String val = map.get(key);
            System.out.println(key + "和" + val + "是一对");
        }

    }
}

Entry键值对象

Map中两种对象, 一种key,一种是value,这一对对象又称做Map中的一个Entry项
Entry将键值对的对应关系封装成了对象, 这样遍历Map时, 这样我们可以从每一个键值对(Entry)对象中获取对应的键与对应的值。

  • public Set<Map.Entry<K,V>> entrySet();获取所有的键值对的set集合
  • K getKey(); 获取Entry对象中的键
  • V getValue();获取Entry对象中的值
public class Demo3 {
    public static void main(String[] args) {
        // 创建map对象
        HashMap<String, String> map = new HashMap<>();
        map.put("吕布", "貂蝉");
        map.put("孙策", "大乔");
        map.put("周瑜", "二乔");
        map.put("刘备", "甘夫人");
        // 获取所有的entry对象
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        // 遍历每一个entry对象
        for(Map.Entry<String, String> entry : entrySet){
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"="+value);
        }
    }
}

HashMap存储自定义类型键值

需求:学生对象作为键,家庭地址作为值,存入map集合中
这里学生姓名相同并且年龄相同视为同一个学生。

public class Student {
    private  String name;
    private  int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

测试

public class TestStudent {
    public static void main(String[] args) {
        // 创建map
        HashMap<Student, String> map = new HashMap<>();
        // 添加元素
        map.put(new Student("刘能", 34), "锦州");
        map.put(new Student("赵四", 23), "葫芦岛");
        map.put(new Student("宋小宝", 45), "大连");
        map.put(new Student("王天来", 25), "营口");
        map.put(new Student("王天来", 25), "营口");
        // 遍历
        for (Student key:map.keySet()){
            String val = map.get(key);
            System.out.println(key + "....."+val);

        }
    }
}

image
  • 当HashMap中存放自定义对象是, 如果自定义对象作为key存在, 要保证对象唯一,必须重写对象的equals方法和hasCode()方法。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。