栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 map = new HashMap<>();
        map.put("小明",18);
        map.put("小红",16);
        map.put("小方",19);
        map.put("小刚",18);
        System.out.println("---KeySet()---");
        //map.keySet() 返回HashMap的键值
        Set set = map.keySet();
        //迭代器获取 set 集合的 key
        Iterator it = set.iterator();
        while (it.hasNext()){
            String key = it.next();
            Integer value = map.get(key);
            System.out.println(key+":"+value);
        }
        System.out.println("---entrySet()---");
        Set> set1 = map.entrySet();
        Iterator> it1 = set1.iterator();
        while (it1.hasNext()){
            Map.Entry entry = it1.next();
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
        System.out.println("---删除---");
        map.remove("小刚");
        System.out.println(map);

        System.out.println("---修改---");
        map.put("小婷",18);
        System.out.println(map);
    }
}

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

import java.util.TreeMap;

public class Demo02 {
    public static void main(String[] args) {
        TreeMap 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{
    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 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 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);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/489689.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号