首页 > 编程语言 >java-collections-map t

java-collections-map t

时间:2024-04-18 17:55:51浏览次数:19  
标签:map 顺序 java HashMap demo 线程 Key entry collections

Map

Map常用子类

  • HashMap:HashMap 是最常用的 Map 实现之一,它基于哈希表实现,提供了 O(1) 时间复杂度的插入、删除和查找操作。
  • Hashtable:Hashtable 是较早期的 Java 集合类,它是线程安全的,但性能通常比 HashMap 差,因为它的方法是同步的。
  • TreeMap:TreeMap 是基于红黑树实现的有序 Map,它根据键的自然顺序或自定义顺序进行排序。TreeMap 提供了一些附加的方法来处理有序映射。
  • LinkedHashMap:LinkedHashMap 继承自 HashMap,它使用双向链表维护插入顺序或者访问顺序,因此迭代顺序可以预测。
  • ConcurrentHashMap:ConcurrentHashMap是Hashtable替代,相比性能较好,线程安全,无序。
  • ConcurrentSkipListMap:线程安全,有序。

扩展:还有其他子类比如ConcurrentHashMap、WeakHashMap、IdentityHashMap、EnumMap...
EnumMap 是针对枚举类型的特定实现,它的键必须是枚举类型的值。

HashMap

    /**
     * hashMap_demo                       
     * 实现方式:数组 + 链表 + (1.8加的红黑树)
     * 遍历顺序:无序
     * 是否线程安全:线程不安全
     * 扩容机制: 当HashMap容量大小(默认16)达到容量的加载因子的大小(默认0.75)自动扩容两倍
     * null值操作:允许使用一个 null 键,多个 null 值。
     */
    @Test
    public void hashMap_demo() {
        HashMap<String,String> hashMap = new HashMap<>(16, 0.75f);
        for (int i = 1; i <= 3; i++) {
            hashMap.put("test"+i,"test");
        }
        hashMap.put(null,"test");
        for (Map.Entry<String, String> entry : hashMap.entrySet()) {
            System.out.print(" Key: " + entry.getKey());
        }
    }

执行打印

Key: null Key: test2 Key: test3 Key: test1

Hashtable

    /**
     * hashtable_demo               
     * 实现方式:数组 + 链表
     * 遍历顺序:无序
     * 是否线程安全:线程安全,所有公共方法都是同步的
     * 扩容机制: 当HashMap容量大小(默认11)达到容量的加载因子的大小(默认0.75)自动扩容两倍
     * 设置扩容参数方法:Map<String,String> map = new HashMap<>(11,0.75f);
     * null值操作:不允许使用 null 键或 null 值
     */
    @Test
    public void hashtable_demo() {
        Hashtable<String,String> hashtable = new Hashtable<>(11, 0.75f);
        for (int i = 1; i <= 3; i++) {
            hashtable.put("test"+i,"test");
        }
        //hashtable.put(null,"test");//键设置为null 抛出java.lang.NullPointerException
        for (Map.Entry<String, String> entry : hashtable.entrySet()) {
            System.out.print(" Key: " + entry.getKey());
        }
    }

执行打印

Key: test2 Key: test3 Key: test1

LinkedHashMap

    /**
     * linkedHashMap_demo          linkedHashMap  demo
     * LinkedHashMap:LinkedHashMap 继承自 HashMap,它使用双向链表维护插入顺序或者访问顺序,因此迭代顺序可以预测。
     * 实现方式:哈希表 + 双向链表
     * 是否线程安全:线程不安全
     * 遍历顺序:当元素被添加到 LinkedHashMap 中时,添加到链表末尾,遍历时,按照插入的顺序返回
     * 设置访问顺序参数策略: 默认是false,设置为true的是如果有get或put操作,将被操作的元素追加到链表的末尾,顺序排后
     * 扩容机制:与 HashMap 相同
     * null值操作:允许使用一个 null 键,多个 null 值。
     */
    @Test
    public void linkedHashMap_demo(){
        //设置顺序方式 默认是false
        LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>(16, 0.75f, false);
        //LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>(16, 0.75f, true);
        for (int i = 1; i <= 3; i++) {
            linkedHashMap.put("test"+i,"test");
        }
        // 默认按照插入顺序,遍历所有的键和值
        for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
            System.out.print(" Key: " + entry.getKey());
        }
        System.out.println("\n --------------");
        linkedHashMap.put("test0","test");
        linkedHashMap.get("test1");
        for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
            System.out.print(" Key: " + entry.getKey());
        }
    }

执行打印

1.设置为false的结果:
Key: test1 Key: test2 Key: test3
//--------------
Key: test1 Key: test2 Key: test3 Key: test0
2.设置为true的结果:
Key: test1 Key: test2 Key: test3
//--------------
Key: test2 Key: test3 Key: test0 Key: test1

ConcurrentHashMap

    /**
     * concurrentHashMap_demo
     * 实现方式:数组 + 链表 + 红黑树
     * 是否线程安全:线程安全 1.8开始使用 CAS 操作和循环来保证对数据的修改是线程安全的,之前使用分段锁。
     * 遍历顺序:无序
     */
    @Test
    public void concurrentHashMap_demo(){
        ConcurrentHashMap<String,String> concurrentHashMap = new ConcurrentHashMap<>(16, 0.75f);
        for (int i = 1; i <= 3; i++) {
            concurrentHashMap.put("test"+i,"test");
        }
        for (Map.Entry<String, String> entry : concurrentHashMap.entrySet()) {
            System.out.print(" Key: " + entry.getKey());
        }
    }

执行打印

Key: test2 Key: test3 Key: test1

ConcurrentSkipListMap

    /**
     * concurrentSkipListMap_demo
     * 实现方式:数组 + 链表 + 红黑树
     * 是否线程安全:线程安全
     * 遍历顺序:有序
     * 顺序机制:默认按照key的第一个数字或字母顺序正序排序,也可以创建Comparator类重写compare方法排序逻辑
     */
    @Test
    public void concurrentSkipListMap_demo() throws InterruptedException {
//        ConcurrentSkipListMap<String,String> concurrentSkipListMap = new ConcurrentSkipListMap<>();
        ConcurrentSkipListMap<Integer, String> concurrentSkipListMap = new ConcurrentSkipListMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.print("o1:"+o1 +"   o2:"+o2+"\n");
                // 定义比较逻辑
//                return o1.compareTo(o2);
                return Integer.compare(o2,o1);//倒序
            }
        });
        for (int i = 1; i <= 3; i++) {
            concurrentSkipListMap.put( i,"test");
        }
        for (Map.Entry<Integer, String> entry : concurrentSkipListMap.entrySet()) {
            System.out.print(" Key: " + entry.getKey());
        }
    }

执行打印

o1:2 o2:1
o1:3 o2:2
Key: 3 Key: 2 Key: 1

标签:map,顺序,java,HashMap,demo,线程,Key,entry,collections
From: https://www.cnblogs.com/yu-si/p/18143516

相关文章

  • java 代码编译检查工具
    <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version><configuration><encoding&......
  • CXF WebService wsdl2java
    下载apache-cxf-3.3.1并解压到bin目录下,输入生成命令wsdl2java-encodingutf-8-dD:\Software\Webservice\wshttp://XXX.XXX.XXX.XXX:XXX/WSInterface.asmx?wsdl把生成的文件,复制到项目中,进行后续编码......
  • java-mysql-语法
    条件查询1.条件运算符:>,<,=,!=,<>2.按逻辑表达式:&&,||,!and,or,not &&和and:两个条件都为true.结果为true,反之为false ||和or:只要有一个条件为true,结果就为true,反之为false !和not:如果连接的条件本身就为false,结果为......
  • 通过Java修改consul配置(保留注释、保留缩进)
         直接上代码了,找了很久也没找到保留注释的三方包,snakeyaml的缩进一直也有问题,就自己写了个正则方式的consul也没有相关接口,只接受整个的传key和value,替换相应value值,大佬有更好的方法希望能告诉我<dependency><groupId>com.orbitz.consu......
  • JAVA基础-流程控制、字符串
    一、java基础1、java主类结构packagecom.study.again001;包名publicclasshelloword{类名staticStrings1="1";静态成员变量publicstaticvoidmain(String[]args){main方法Strings2="2";局部变量System.out.println(s1......
  • java静态代理模式
    通过模拟租房来展现静态代理模式租房接口//租房publicinterfaceRent{publicvoidrent();}被代理的真实角色房东//房东publicclassHostimplementsRent{@Overridepublicvoidrent(){System.out.println("房东要出租房租");}}......
  • 如何在HTML中使用JavaScript:从基础到高级的全面指南!
    JavaScript是一种轻量级的编程语言,通常用于网页开发,以增强用户界面的交互性和动态性。然而在HTML中,有多种方法可以嵌入和使用JavaScript代码。本文就带大家深入了解如何在HTML中使用JavaScript。一、使用script标签要在HTML中使用JavaScript,我们需要使用<script>标签。这个标......
  • 每日一模块-collections
    Python的collections模块提供了很多高级的数据结构,使得我们在处理数据时能够更加方便和高效。下面我们将详细讲解collections模块中各个类的功能,并给出相应的样例。导入模块首先,我们需要导入collections模块:importcollections2.CounterCounter是一个字典子类,用于计数可哈......
  • 使用Maps SDK添加本地slpk
    SceneViewm_sceneView;publicvoidLoadSceneLayerFromSLPK(SceneViewsceneView,stringslpkPath){ if(!File.Exists(slpkPath)) thrownewException("文件不存在"); if(null==sceneView.Scene) CreateScene(sceneView); Uritreespk=newSystem.Uri......
  • 34-Java反射获取对象成员属性,getFields()与getDeclaredFields()方法的区别
    需求:在开发过程中,经常会遇到的一个问题是,需要判断某个字符串是不是对象的某个成员属性名,然后根据判断结果去操作这个成员属性参考教程:Java反射获取对象成员属性,getFields()与getDeclaredFields()方法的区别_javadeclaredfields-CSDN博客 可以通过以下方法:getFields(): 获......