• Java集合(四) --- Map


    好潦草的一篇文章,不想学习想摆烂了又 ,凑合看


    提示:以下是本篇文章正文内容,下面案例可供参考

    一、Map的实现类的结构

    在这里插入图片描述

    二、Map结构的理解

    在这里插入图片描述

    三、HashMap的底层实现原理? 以jdk7的说明:

    在这里插入图片描述

    四、Map中定义的方法

    在这里插入图片描述

    五、总结:常用方法

    在这里插入图片描述

    六、代码

    package com.tyust.edu;
    
    import org.junit.Test;
    
    import java.util.*;
    
    /**
     * @author YML TYUST-XDU 2019-2026
     * @create 2023-10-12 8:45
     */
    public class MapTest {
    
        @Test
        public void test1(){
            Map map = new HashMap();
    
    //       map = new Hashtable();
    
            map.put(null,null);
        }
    
        @Test
        public void test3() {
            Map map = new HashMap();
            //添加
            map.put("AA",123);
            map.put(45,123);
            map.put("nn",23);
            //修改
            map.put("AA",37);
    
            System.out.println(map);
    
            Map map1 = new HashMap();
            map1.put("CC",123);
            map1.put("DD",123);
    
            map.putAll(map1);
            System.out.println(map);
    
            Object value = map.remove("CC");
            System.out.println(value);
            System.out.println(map);
    
            map.clear();
            System.out.println(map.size());
    
        }
    
        @Test
        public void test4() {
            Map map = new HashMap();
            //添加
            map.put("AA", 123);
            map.put(45, 123);
            map.put("nn", 23);
            //修改
            map.put("AA", 37);
    
            System.out.println(map.get(45));
            boolean isExit = map.containsKey("nn");
            System.out.println(isExit);
    
            isExit = map.containsValue(123);
            System.out.println(isExit);
            map.clear();
            System.out.println(map.isEmpty());
        }
    
        @Test
        public void test5() {
            Map map = new HashMap();
            //添加
            map.put("AA", 123);
            map.put(45, 123);
            map.put("nn", 23);
            //修改
            map.put("AA", 37);
    
    
            //遍历所有的key集keySet()
            Set set = map.keySet();
            Iterator iterator = set.iterator();
            while(iterator.hasNext()){
                System.out.println(iterator.next());
            }
    
            //遍历所有的value集:values()
            Collection values = map.values();
            for(Object obj:values){
                System.out.println(obj);
            }
    
    
            System.out.println();
            //遍历所有的key-value
            //entrySet()
            Set entrySet = map.entrySet();
            Iterator iterator1 = entrySet.iterator();
            while(iterator1.hasNext()){
                Object obj = iterator1.next();
                //entrySet集合中的元素都是entry
                Map.Entry entry = (Map.Entry)obj;
                System.out.println(entry.getKey()+"--->"+entry.getValue());
            }
    
    
            System.out.println();
            Set keySet = map.keySet();
            Iterator iterator2 = keySet.iterator();
            while(iterator2.hasNext()){
                //System.out.println(iterator2.next());
                Object key = iterator2.next();
                Object value = map.get(key);
                System.out.println(key+"--->"+value);
            }
    
    
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123

    七、Collections操作 Collection、Map的工具类

    //Collections 类中提供了多个 synchronizedXxx() 方法,
    //该方法可使将指定集合包装成线程同步的集合,从而可以解决
    //多线程并发访问集合时的线程安全问题

    package com.tyust.edu;
    
    import org.junit.Test;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    /**
     * Collections操作 Collection、Map的工具类
     * @author YML TYUST-XDU 2019-2026
     * @create 2023-10-13 7:57
     */
    public class CollectionsTest {
        @Test
        public void test1(){
            List list = new ArrayList();
            list.add(456);
            list.add(123);
            list.add(6);
            list.add(46);
            list.add(56);
    
            System.out.println(list);
            //Collections.reverse(list);
    //        Collections.shuffle(list);
            Collections.sort(list);
            Collections.swap(list,1,2);
    
            int count = Collections.frequency(list,123);
            System.out.println(count);
    
    
            System.out.println(list);
    
        }
        @Test
        public void test2() {
            List list = new ArrayList();
            list.add(456);
            list.add(123);
            list.add(6);
            list.add(46);
            list.add(56);
    
            //报异常
    //        IndexOutOfBoundsException
    //        List desc = new ArrayList();
    //
    //        Collections.copy(desc,list);
    
            List dest = Arrays.asList(new Object[list.size()]);
            System.out.println(dest.size());
            Collections.copy(dest,list);
            System.out.println(dest);
    
            //Collections 类中提供了多个 synchronizedXxx() 方法,
            //该方法可使将指定集合包装成线程同步的集合,从而可以解决
            //多线程并发访问集合时的线程安全问题
            List list1 = Collections.synchronizedList(list);
    
    
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    八、Properties 用来处理配置文件

    package com.tyust.edu;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    /**
     * @author YML TYUST-XDU 2019-2026
     * @create 2023-10-12 11:19
     */
    public class ProperitesTest {
    
    //    Properties 用来处理配置文件
        public static void main(String[] args) throws IOException {
            Properties pros = new Properties();
            FileInputStream fis = new FileInputStream("jdbc.properties");
            pros.load(fis); //加载流对应的文件
    
            String name = pros.getProperty("name");
            String password = pros.getProperty("password");
    
            System.out.println(name + password);
    
            fis.close();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    九、TreeSet两种排序方式:自然排序(实现Comparable接口)和定制排序

    package com.tyust.edu;
    
    import org.junit.Test;
    
    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.TreeSet;
    
    /**
     * @author YML TYUST-XDU 2019-2026
     * @create 2023-10-10 9:45
     */
    public class TreeSetTest {
        //1.向TreeSet中添加的数据,要求是相同类的对象
        //2.两种排序方式:自然排序(实现Comparable接口)和定制排序
    
        //3.自然排序中,比较两个对象是否相同的标准为:compareTo()返回0,不再是equals()
    
        //4.定制排序中,比较两个对象是否相同的标准为:compare()返回0,不再是equals()
        @Test
        public void test1(){
            TreeSet set = new TreeSet();
    
            //失败 :不能添加不同类的对象
    //        set.add(123);
    //        set.add(345);
    //        set.add(655);
    //        set.add("AAA");
    //        set.add(123);
    
    
            //举例一:
    //        set.add(432);
    //        set.add(32);
    //        set.add(2);
    //        set.add(42);
    
    
            //举例二
            set.add(new User("Tom",12));
            set.add(new User("Mary",18));
            set.add(new User("PPP",51));
            set.add(new User("WWW",23));
            set.add(new User("DD",28));
    
            Iterator iterator = set.iterator();
            while(iterator.hasNext()){
                System.out.println(iterator.next());
            }
    
        }
    
    
        @Test
        public void test2(){
            Comparator com = new Comparator() {
                @Override
                public int compare(Object o1,Object o2) {
                    if(o1 instanceof User&& o2 instanceof User){
                        User u1 = (User) o1;
                        User u2 = (User) o2;
                        return Integer.compare(u1.getAge(),u2.getAge());
                    }else {
                        throw new RuntimeException("输入的类型不匹配");
                    }
                }
    
    
            };
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
  • 相关阅读:
    Centos生命周期,Centos和Centos Stream区别
    Fabric.js 控制元素层级
    化繁为简,聊一聊复制状态机系统架构抽象
    wsl [Ubuntu20.04.6] 安装 Hadoop
    C语言编程陷阱(四)
    【数据分析】2020年北京交通大学计算机学院学术型博士录取数据分析
    树的统计问题
    从键盘输入一个 4 位的整数,用 for 循环判断其中含有 3 的位的个数,用C及C++分别实现。
    【代码随想录】算法训练营 第八天 第四章 字符串 Part 1
    南大通用GBase8s 常用SQL语句(257)
  • 原文地址:https://blog.csdn.net/lalalalalab/article/details/133799275