你是我的 我不是你的 Map.Key就是这么霸道

key就能指value,可你value就不能指我key

key--------------value
键 就会 映射到 值
学号-------------姓名
s001-------------张三
s002-------------李四
s003--------------王五

HashMap 底层数据结构是哈希表,所有的Map集合的数据结构只跟***键有关***跟***值没关系***

HashMap<String, String> map = new HashMap<>();
    map.put("s001","张三");
    map.put("s002", "李四");
    map.put("s003", "王五");

    System.out.println(map);
取值
 //去张三
    String name = map.get("s001");
    System.out.println(name);

键相同,值覆盖 键一定是唯一的

HashMap<String, String> map = new HashMap<>();
    String s = map.put("文章", "马伊琍"); //第一次存储返回值是null
    String s1 = map.put("文章", "姚笛"); //再次存储相同的键,新的值,会覆盖旧值,返回旧值
    map.put("陈羽凡","白百合");
    map.put("陈思成","佟丽娅");
    map.put("武大","金莲");

    System.out.println(map);
    System.out.println(s);
    System.out.println(s1);

{文章=姚笛, 陈思成=佟丽娅, 武大=金莲, 陈羽凡=白百合}
null
马伊琍
Map集合的数据结构只跟键有关,跟值没关系

键是Student 值是 String
//键相同,值覆盖

HashMap<Student,String> map = new HashMap<>();
    Student s1 = new Student("张三", 23333);
    map.put(s1,"s001");
    map.put(new Student("李四", 24),"s002");
    map.put(new Student("王五", 25),"s003");
    map.put(new Student("赵六", 26),"s004");
    map.put(new Student("张三", 23),"s005");
    System.out.println(map);
    //map.clear();//清空集合中的所有元素
    //根据键删除一对,返回的是键对应得值
    String remove = map.remove(s1);
    System.out.println(map);
    System.out.println(remove);
方法

判断功能

boolean empty = map.isEmpty(); //判断集合是否为空

判断有没有这个键

 boolean b = map.containsKey("文章");
    boolean b1 = map.containsValue("马伊琍");
    System.out.println(b);
    System.out.println(b1);

获取集合长度

 int size = map.size();
    System.out.println(size);

根据键找值

获取所有得键得集合
    Set<String> keySet = map.keySet();
    for (String key : keySet) {
        //键找值
        String value = map.get(key);
        System.out.println(key+"===="+value);
    }

获取键值对 对象 Entry 也就是说把键值统一看作一个整体 Map.Entry<String, String>

Map.Entry<String, String>
Set<Map.Entry<String, String>> entries = map.entrySet();
        //Entry 键值对,对象里面得方法
        //K getKey ()
        //返回与此项对应的键。
        //V getValue ()
        //返回与此项对应的值。

for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"====="+value);
}
 //遍历方式1:键找值
        Set<Student> students = map.keySet();
        for (Student key : students) {
            String value = map.get(key);
            System.out.println(key.getName()+"=="+key.getAge()+"========="+value);
        }
        System.out.println("========================");
        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key.getName() + "==" + key.getAge() + "=========" + value);
        }
        //可以所有值得集合
        Collection<String> values = map.values();
        System.out.println(values);

LinkedHashMap 键的数据结构是哈希表和链表,键唯一,而且有序, 哈希表保证唯一,链表保证有序

LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
        map.put(10,"aaa");
        map.put(20, "aaa");
        map.put(30, "aaa");
        map.put(40, "aaa");
        map.put(50, "aaa");
        map.put(60, "aaa");

    	Set<Integer> integers = map.keySet();
        for (Integer integer : integers) {
            String value = map.get(integer);
            System.out.println(integer+"==="+value);
        }

TreeMap:键的数据结构是***二叉树***,可以对键进行排序,排序:自然排序,比较器排序

TreeMap<Integer, String> map = new TreeMap<>();
        map.put(10, "aaa");
        map.put(200, "aaa");
        map.put(30, "aaa");
        map.put(4330, "aaa");
        map.put(150, "aaa");
        map.put(160, "aaa");

        Set<Integer> integers = map.keySet();
        for (Integer integer : integers) {
            String value = map.get(integer);
            System.out.println(integer + "===" + value);
        }
TreeMap:键的数据结构是二叉树,可以对键进行排序,排序:自然排序,比较器排序
Map:集合的数据结构只跟键有关,跟值没关系
Set<Map.Entry<Student, String>> entries = map.entrySet();
    for (Map.Entry<Student, String> en : entries) {
        System.out.println(en);
        Student key = en.getKey();
        String value = en.getValue();
        System.out.println(key+"===="+value);
        }
TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int i = s1.getName().length() - s2.getName().length();
                int j=i==0?s1.getName().compareTo(s2.getName()):i;
                int h=j==0?s1.getAge()-s2.getAge():j;
                return h;
            }
        });

Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> en : entries) {
            Student key = en.getKey();
            String value = en.getValue();
            System.out.println(key + "====" + value);
        }

键的数据结构是一样的

HashMap:线程不安全效率高,允许null值null键

Hashtable 线程安全效率低,不允许存null值null键

HashMap<String, String> map = new HashMap<>();
    map.put(null,null);
    Hashtable<String, String> table = new Hashtable<>();

    // table.put(null,"abc");
    //table.put("aaa",null);
    //table.put(null,null);
    //HashSet:底层拿 HashMap 存的

    HashSet<Integer> integers = new HashSet<>();
    integers.add(100);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值