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

java-集合框架-Map

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

java-集合框架-Map

Map集合

Map接口特点:

  1. 用于存储任意键值对(Key-Value);
  2. 键:无序、无下标、不允许重复(唯一);
  3. 值:无序、无下标、允许重复。

 特点:存储一对数据(Key-Value),无序、无下标、键不可重复、值可以重复。

方法:

V put(K key,V value);//将对象存储到集合中,关联键值。key重复则覆盖原值。

Object get (Object key);//根据键获取对应的值。

keySet ;//返回所有key。

Collection values();//返回包含所有值的Collection集合。

Set >;  //键值匹配的Set集合。

基本使用:

import java.util.HashMap;
import java.util.Map;


public class Demo01 {
    public static void main(String[] args) {
        //创建Map集合
        Map map = new HashMap<>();
        //添加元素
        map.put("CN","中国");
        map.put("UK","英国");
        map.put("USA","美国");
//        map.put("CN","zhongguo");//值会被替换,键不可以重复
//        map.put("China","zhongguo");//值可以重复

        System.out.println("元素个数:"+map.size());
        System.out.println(map.toString());

        //删除
        map.remove("USA");
        System.out.println("删除之后:"+map.size());
        System.out.println(map.toString());

        //遍历
        //使用keySet();
        System.out.println("———————————————keySet()遍历—————————————————");
        for (String s : map.keySet()) {
            System.out.print(s+"——>"+map.get(s)+"t");
        }

        System.out.println("n———————————————entrySet()遍历(效率更高 )—————————————————");
        for (Map.Entry entry : map.entrySet()) {
            System.out.print(entry.getKey()+"-->"+entry.getValue()+"t");
        }
        System.out.println();

        //判断
        System.out.println(map.containsKey("CN"));
        System.out.println(map.containsValue("泰国"));
        System.out.println(map.isEmpty());


    }
}
Map集合实现类
  • HashMap【重点】:
    • JDK1.2版本,线程不安全,运行效率高 ,允许使用null作为key或者value;
      • HashMap();构造一个初始容量为16和默认加载因子为0.75的空HashMap。
  • HashTable:
    • JDK1.0版本,线程安全,运行效率慢;不允许null作为key或者value。
  • Properties:
    • HashTable的子类,要求key和value都是String。通常用于配置文件的读取。
  • TreeMap:
    • 实现了SortedMap接口(是Map的子接口),可以对key自动排序。
      • 实现TreeSet内部调用的就是TreeMap接口。
      • TreeSet.add内部调用的就是TreeMap的put方法。
import java.util.HashMap;
import java.util.Map;


public class Demo02 {
    public static void main(String[] args) {
        //创建对象
        HashMap hashMap = new HashMap<>();
        Student p1 = new Student("张学友", 20);
        Student p2 = new Student("刘德华", 19);
        Student p3 = new Student("郭富城", 18);
        Student p4 = new Student("黎明", 18);
        //添加元素
        hashMap.put(p1,"歌神");
        hashMap.put(p2,"综合王");
        hashMap.put(p3,"舞王");
        hashMap.put(p4,"文艺王");
        hashMap.put(new Student("张学友",20),"music");
        System.out.println("元素个数:"+hashMap.size());
        System.out.println(hashMap.toString());

        //删除元素
//        hashMap.remove(p1);
//        hashMap.clear();
//        System.out.println("元素个数:"+hashMap.size());
//        System.out.println(hashMap.toString());

        //遍历
        System.out.println("——————————————————keySet方法——————————————————————————");
        for (Student student : hashMap.keySet()) {
            System.out.print(student.toString()+"-->"+hashMap.get(student)+"t");
        }
        System.out.println();

        System.out.println("——————————————————entrySet方法——————————————————————————");
        for (Map.Entry entry : hashMap.entrySet()) {
            System.out.print(entry.getKey()+"-->"+entry.getValue()+"t");
        }
        System.out.println();

        //判断
        System.out.println(hashMap.containsKey(new Student("张学友", 20)));
        System.out.println(hashMap.containsKey(p1));
        System.out.println(hashMap.containsValue("文艺王"));
    }
}

 TreeMap:

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;


public class Demo03 {
    public static void main(String[] args) {
        //创建对象(定制比较)
        TreeMap treeMap = new TreeMap<>(new Comparator() {
            @Override
            public int compare(Student o1, Student o2) {
                int n1 = o1.getName().compareTo(o2.getName());
                int n2 = o1.getStuNo()-o2.getStuNo();
                return n1==0?n2:n1;
            }
        });
        Student s1 = new Student("张学友", 20);
        Student s2 = new Student("刘德华", 19);
        Student s3 = new Student("郭富城", 18);
        Student s4 = new Student("黎明", 18);
        //新增
        treeMap.put(s1,"歌神");
        treeMap.put(s2,"综合王");
        treeMap.put(s3,"舞王");
        treeMap.put(s4,"文艺王");
        treeMap.put(new Student("黎明", 18),"永远滴神");
        System.out.println("元素个数:"+treeMap.size());
        System.out.println(treeMap.toString());
        //删除
//        treeMap.remove(s1);
//        treeMap.remove(new Student("刘德华", 19));
//        treeMap.clear();
//        System.out.println("元素个数:"+treeMap.size());
//        System.out.println(treeMap.toString());

        //遍历
        System.out.println("—————————————KeySet———————————————————");
        for (Student student : treeMap.keySet()) {
            System.out.println(student+"->"+treeMap.get(student));
        }
        System.out.println("—————————————EntrySet———————————————————");
        for (Map.Entry entry : treeMap.entrySet()) {
            System.out.println(entry.getKey()+"->"+entry.getValue());
        }

        //判断
        System.out.println(treeMap.containsKey(s1));
        System.out.println(treeMap.containsKey(new Student("黎明",18)));
        System.out.println(treeMap.containsValue("永远滴神"));
        System.out.println(treeMap.isEmpty());


    }
}

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

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

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