Map集合(调用迭代器、比较器)

map集合是指提供key到value的映射的集合。以键值对形式存储对象,key-value既有联系也无联系,有联系在于一个key值对应一个value值,通过key值可以找到对应的value值,类似于我们在查字典的时候,根据页码查找我们所需要的资料。以学生类来说,key相当于学生的学号,value可以对应该学生的学生姓名,性别,年龄等信息,用这种关系可以方便查找。

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

//                Map:以键值对形式存储对象,
//                    key-value:key是唯一,value是可重复的
//                        HashMap:key是无序不可重复

public class Demo01 {
    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("小明",18);
        map.put("小红",16);
        map.put("小方",19);
        map.put("小刚",18);
        System.out.println("---KeySet()---");
        //map.keySet() 返回HashMap的键值
        Set<String> set = map.keySet();
        //迭代器获取 set 集合的 key
        Iterator<String> it = set.iterator();
        while (it.hasNext()){
            String key = it.next();
            Integer value = map.get(key);
            System.out.println(key+":"+value);
        }
        System.out.println("---entrySet()---");
        //把Entry存放到Set集合中
        Set<Map.Entry<String, Integer>> set1 = /*把key和value存放到Entry对象中*/map.entrySet();
        //迭代Set集合获取Entry对象
        Iterator<Map.Entry<String, Integer>> it1 = set1.iterator();
        while (it1.hasNext()){
            Map.Entry<String, Integer> entry = it1.next();
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
        System.out.println("---删除---");
        map.remove("小刚");
        System.out.println(map);

        System.out.println("---修改---");
        //修改key     先删除后添加
        //修改value   覆盖
        map.put("小婷",18);
        System.out.println(map);
    }
}

TreeMap 存储自定义对象
当作 key 的对象需要实现自然排序或者传递比较器

import java.util.TreeMap;

public class Demo02 {
    public static void main(String[] args) {
        TreeMap<User, String> map = new TreeMap<>();
        map.put(new User(1),"张三");
        map.put(new User(3),"李四");
        map.put(new User(2),"王五");
        map.put(new User(1),"张三");
        //根据id从大到小排序
        System.out.println(map);

    }
}

//------------------------------------------------------
public class User implements Comparable<User>{
    private int id;

    public User(int id) {
        this.id = id;
    }

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                '}';
    }

    @Override
    public int compareTo(User user) {
        return user.getId() - this.getId();
    }
}

使用 HashMap 存储自定义对象

当作 key 的对象需要重写 hashCode() 和 equals()

import java.util.HashMap;


public class Demo03 {
    public static void main(String[] args) {
        //key对象是Person,因此需要重写对象Person类的方法
        HashMap<Person, User> map = new HashMap<>();
        map.put(new Person(1),new User(1));
        map.put(new Person(3),new User(4));
        map.put(new Person(1),new User(5));
        map.put(new Person(3),new User(3));
        map.put(new Person(5),new User(2));
        System.out.println(map);
    }
}

//-------------------------------------------------------
import java.util.Objects;

public class Person {
    private int id;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return id == person.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Person(int id) {
        this.id = id;
    }

    public Person() {
    }
}
//-----------------------------------------------------
public class User {
    private int id;

    public User() {
    }

    public User(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                '}';
    }
}

键值为Integer类型的,使用TreeMap排序只需要打印map集合即可

import java.util.TreeMap;
public class Demo04 {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(2,"a");
        map.put(4,"b");
        map.put(3,"c");
        map.put(6,"d");
        map.put(1,"e");
        map.put(5,"f");
        System.out.println(map);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值