HashMap,LinkedHashMap,TreeMap,HashTable,ConcurrentHashMap,ConcurrentSkipListMap 关于k,v是否为null,以及输出排序

本文主要是介绍HashMap,LinkedHashMap,TreeMap,HashTable,ConcurrentHashMap,ConcurrentSkipListMap 关于k,v是否为null,以及输出排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

HashMap:k,v可为null,LinkedHashMap:k,v可为null,TreeMap:v可为null,HashTable:k,v都不可为null,ConcurrentHashMap:k,v都不可为null,ConcurrentSkipListMap:k,v都不可为null。

HashMap,HashTable,ConcurrentHashMap 为无序输出;TreeMap为key排序输出;LinkedHashMap put顺序先后输;ConcurrentSkipListMap为key排序输出。

Map类型KEYVALUE
HashMapnullnull
LinkedHashMapnullnull
TreeMapnot nullnull
HashTablenot nullnot null
ConcurrentHashMapnot nullnot null
ConcurrentSkipListMapnot nullnot null

下面代码测试:

public class MapKVIsNullTest {public static void main(String[] args) {// HashMapSystem.out.println("------HashMap无序输出------");HashMap<String, String> hashMap = new HashMap<String, String>();hashMap.put("3", null);hashMap.put("b", "Value1");hashMap.put("2", "Value2");hashMap.put("a", "ValueB");hashMap.put("ee", "ValueA");Iterator<Entry<String, String>> it = hashMap.entrySet().iterator();while (it.hasNext()) {Entry<String, String> e = it.next();System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue());}// TreeMapSystem.out.println("------TreeMap按Key排序输出------");TreeMap<String, String> teMap = new TreeMap<String, String>();teMap.put("3", null);teMap.put("1", "Value1");teMap.put("2", "Value2");teMap.put("b", "ValueB");teMap.put("a", "ValueA");Iterator<Entry<String, String>> tit = teMap.entrySet().iterator();while (tit.hasNext()) {Entry<String, String> e = tit.next();System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue());}// LinkedHashMapSystem.out.println("--LinkedHashMap根据输入的顺序输出--");LinkedHashMap<String, String> lhsMap = new LinkedHashMap<String, String>();lhsMap.put(null, null); //lhsMap.put("1", "Value1");lhsMap.put("2", "Value2");lhsMap.put("b", "ValueB");lhsMap.put("a", "ValueA");Iterator<Entry<String, String>> lit = lhsMap.entrySet().iterator();while (lit.hasNext()) {Entry<String, String> e = lit.next();System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue());}Collections.emptyMap();// HashTableSystem.out.println("--Hashtable无序输出--");Hashtable<String, String> hstable = new Hashtable<String, String>();hstable.put("3", "Value3");hstable.put("1", "Value1");hstable.put("2", "Value2");hstable.put("b", "ValueB");hstable.put("a", "ValueA");Iterator<Entry<String, String>> ithstable = hstable.entrySet().iterator();while (ithstable.hasNext()) {Entry<String, String> e = ithstable.next();System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue());}// ConcurrentHashMapSystem.out.println("--ConcurrentHashMap无序输出--");ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<String, String>();concurrentHashMap.put("3", "Value3");concurrentHashMap.put("b", "Value1");concurrentHashMap.put("2", "Value2");concurrentHashMap.put("a", "ValueB");concurrentHashMap.put("ee", "ValueA");for (Entry<String, String> v : concurrentHashMap.entrySet()) {System.out.println("Key: " + v.getKey() + "--Value: " + v.getValue());}// ConcurrentSkipListMapSystem.out.println("--ConcurrentHashMap无序输出--");ConcurrentSkipListMap<String, String> concurrentSkipListMap = new ConcurrentSkipListMap<String, String>();concurrentSkipListMap.put("11", "Value3");concurrentSkipListMap.put("b", "Value1");concurrentSkipListMap.put("2", "Value2");concurrentSkipListMap.put("a", "ValueB");concurrentSkipListMap.put("ee", "ValueA");for (Entry<String, String> v : concurrentSkipListMap.entrySet()) {System.out.println("Key: " + v.getKey() + "--Value: " + v.getValue());}}
}

这篇关于HashMap,LinkedHashMap,TreeMap,HashTable,ConcurrentHashMap,ConcurrentSkipListMap 关于k,v是否为null,以及输出排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/327095

相关文章

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

python多种数据类型输出为Excel文件

《python多种数据类型输出为Excel文件》本文主要介绍了将Python中的列表、元组、字典和集合等数据类型输出到Excel文件中,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一.列表List二.字典dict三.集合set四.元组tuplepython中的列表、元组、字典

Spring AI集成DeepSeek实现流式输出的操作方法

《SpringAI集成DeepSeek实现流式输出的操作方法》本文介绍了如何在SpringBoot中使用Sse(Server-SentEvents)技术实现流式输出,后端使用SpringMVC中的S... 目录一、后端代码二、前端代码三、运行项目小天有话说题外话参考资料前面一篇文章我们实现了《Spring

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.