Map(双列集合的根接口)

本文详细介绍了Java中Map接口的基本概念及其子类HashMap、TreeMap、LinkedHashMap的特点与使用方法,包括键值对的添加、查询、删除等操作,并演示了如何通过不同方式遍历Map。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Map(双列集合的根接口)

1.介绍:

这里写图片描述

1.以键值对形势保存数据
2.键值唯一
2.Hashset 和 HashMap 之间的联系
1.底层都是哈希算法 根据面向对象来分析 一套算法 俩类使用
2.HashSet底层 依赖 HashMap 去实现 
3.添加到set中的值 实际上是添加 到 map中健的值
4.HashMap:有去重功能 说的是健
5.TreeMap:有排序功能 说的也是健
3.Map的添加方法
public class text {
public static void main(String[] args) {
    HashMap<String, Integer>map = new HashMap<>();
    Integer i1 = map.put("东", 19);
    Integer i2 = map.put("南", 20);
    Integer i3 = map.put("西", 21);
    Integer i4 = map.put("北", 22);
    Integer i5 = map.put("北", 23);

    System.out.println(map);
    System.out.println(i1);
    System.out.println(i2);
    System.out.println(i3);
    System.out.println(i4);
    System.out.println(i5);
}
}

运行结果:
{南=20, 北=23, 东=19, 西=21}
null
null
null
null
22

结论: 返回的是被替换(覆盖)的值
4.判断键值key是否存在map;
public class text {
public static void main(String[] args) {
    HashMap<String, Integer>map = new HashMap<>();
    Integer i1 = map.put("东", 19);
    Integer i2 = map.put("南", 20);
    Integer i3 = map.put("西", 21);
    Integer i4 = map.put("北", 22);

    boolean b1 = map.containsKey("南");
    System.out.println(b1);
    System.out.println(map);

运行结果:
true
{南=20, 北=22, 东=19, 西=21}
5.判断值value是否存在map
    boolean b2 = map.containsValue(20);
    System.out.println(b2);

运行结果: true
6.通过键值查询对应的值
    Integer c1 = map.get("东");
    Integer c2 = map.get("中");
    System.out.println(c1);
    System.out.println(c2); 

运行结果:
19
null
7.获取所有key键值的Set集合
    Set<String>keySet = map.keySet();
    System.out.println(keySet);

运行结果:
[南, 北, 东, 西]
8.获取所有值value的集合
    Collection<Integer>values =  map.values();
    System.out.println(values);
运行结果:
[20, 22, 19, 21]
9.通过键值key删除整个键值对
    Integer remove = map.remove("振南");
    System.out.println(remove);

运行结果:
20
{北=22, 东=19, 西=21}
10.清空
    map.clear();
    System.out.println(map);
运行结果:
{}


}
}
11.map遍历
public class text {
public static void main(String[] args) {
    HashMap<String, Integer>map = new HashMap<>();
//  通过keySet遍历
    map.put("东", 19);
    map.put("南", 20);
    map.put("西", 21);
    map.put("北", 22);
    System.out.println(map);

//  获取所有key的Set集合
    Set<String>keySet = map.keySet();
    System.out.println(keySet);

//  获取迭代器
    Iterator<String> iterator = keySet.iterator();
//  遍历所有key
    while(iterator.hasNext()) {
        String key = iterator.next();
//      通过key获取对应的value从map
        Integer value = map.get(key);

        System.out.println(key + "=" + value);
    }

//  获取所有value的集合
    Collection<Integer>values =  map.values();
    System.out.println(values);
}
}
运行结果:
{南=20, 北=22, 东=19, 西=21}
[南, 北, 东, 西]
南=20
北=22
东=19
西=21
[20, 22, 19, 21]
12.增强for循环遍历for
public class text {
public static void main(String[] args) {
    HashMap<String, Integer>map = new HashMap<>();
    map.put("东", 19);
    map.put("南", 20);
    map.put("西", 21);
    map.put("北", 22);   
//  增强for循环
    Set<String>keySet = map.keySet();
    for (String key : keySet) {
        Integer integer = map.get(key);
        System.out.println(key+integer);
    }
}
}
运行结果:
南202219
西21
13.迭代器遍历map(Entry==从map中取出整个键值对)
public class text {
public static void main(String[] args) {
    HashMap<String, Integer>map = new HashMap<>();
    map.put("东", 19);
    map.put("南", 20);
    map.put("西", 21);
    map.put("北", 22);   
//  获取键值对象 的Set集合 来遍历
    Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
    Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();
    while (iterator.hasNext()) {
//      获取Entry对象
        Entry<String, Integer> entry = iterator.next();
//      获取value
        Integer value = entry.getValue();
        String key = entry.getKey();
        System.out.println(value+"--"+ key);
    }
}
}

运行结果:
20--南
22--北
19--东
21--西
13.增强for循环遍历map(Entry==从map中取出整个键值对)
public class text {
public static void main(String[] args) {
    HashMap<String, Integer> map = new HashMap<>();
    map.put("振北", 20);
    map.put("振南", 20);
    map.put("振西", 21);
    map.put("振东", 22);
    Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
    for(Map.Entry<String, Integer> entry : entrySet ) {
        String string = entry.getKey();
        Integer integer = entry.getValue();
        System.out.println(string + "=" + integer);

}
}}
运行结果:
振东=22
振北=20
振南=20
振西=21
14.map存对象
public class text {
public static void main(String[] args) {
    HashMap<Student , String>hashMap = new HashMap<>();
    hashMap.put(new Student("哈哈",12),"安徽");
    hashMap.put(new Student("小小",13),"安徽");
    hashMap.put(new Student("额额",15),"安徽");
    hashMap.put(new Student("小小",13),"安心");

//  重写HashCode和Equals
//  重复之后key相同 value会重写覆盖
    System.out.println(hashMap);

}}
运行结果:
{Student [name=额额, age=15]=安徽, Student [name=哈哈, age=12]=安徽, Student [name=小小, age=13]=安心}

Student类重写部


Student类重写部分:
@Override
public int compareTo(Student o) {
     int num = this.age - o.getAge();
     return num == 0 ? 1 : num;

 @Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + age;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Student other = (Student) obj;
    if (age != other.age)
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}   


1.LinkedHashMap储存信息(按原输入顺序排序)
public class text {
public static void main(String[] args) {
    LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
    map.put("哈哈", 11);
    map.put("小小", 12);
    map.put("多多", 11);
    map.put("欧欧", 11);
    System.out.println(map);

}}

运行结果:
{哈哈=11, 小小=12, 多多=11, 欧欧=11}
1.1创建一个TreeMaap 保存学生 value保存户籍,按年龄排序
public class text {
public static void main(String[] args) {
        TreeMap<Student, String>map = new TreeMap<>();
        map.put(new Student("丁鹏",11),"安徽");
        map.put(new Student("哈哈",15),"阿萨斯");
        map.put(new Student("小小",145),"斯蒂芬");
        map.put(new Student("人人",14),"塞上风");
        map.put(new Student("乖乖",71),"人工岛");
        System.out.println(map);

}}

运行结果:
{Student [name=丁鹏, age=11]=安徽,
Student [name=人人, age=14]=塞上风,
Student [name=哈哈, age=15]=阿萨斯,
Student [name=乖乖, age=71]=人工岛,
Student [name=小小, age=145]=斯蒂芬}
2.随机交换集合中值的位置(洗牌)
public class text {
public static void main(String[] args) {
    //collecttions类的方法(静态方法)
    ArrayList<Integer> list = new ArrayList<>();
    list.add(12);
    list.add(12);
    list.add(12);
    list.add(16);
// 随机交换集合中值的位置(洗牌)
    Collections.shuffle(list);
    System.out.println(list);
运行结果:
[12, 12, 16, 12]
3.排序
    Collections.sort(list);
    System.out.println(list);
运行结果:
[12, 12, 12, 16]
4.二分查找

    int index = Collections.binarySearch(list, 16);
    System.out.println(index);

运行结果:
3
5.反转集合
    Collections.reverse(list);
    System.out.println(list);

运行结果:
[16, 12, 12, 12]
}}
6.排序
    ArrayList<Student> list = new ArrayList<>();
    list.add(new Student("小小",12));
    list.add(new Student("多多",13));
    list.add(new Student("哈哈",11));
    Collections.sort(list);
    System.out.println(list);
运行结果:
[Student [name=哈哈, age=11], Student [name=小小, age=12], Student [name=多多, age=13]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值