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

相关文章

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用

Python如何判断字符串中是否包含特殊字符并替换

《Python如何判断字符串中是否包含特殊字符并替换》这篇文章主要为大家详细介绍了如何使用Python实现判断字符串中是否包含特殊字符并使用空字符串替换掉,文中的示例代码讲解详细,感兴趣的小伙伴可以了... 目录python判断字符串中是否包含特殊字符方法一:使用正则表达式方法二:手动检查特定字符Pytho

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

JAVA数组中五种常见排序方法整理汇总

《JAVA数组中五种常见排序方法整理汇总》本文给大家分享五种常用的Java数组排序方法整理,每种方法结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录前言:法一:Arrays.sort()法二:冒泡排序法三:选择排序法四:反转排序法五:直接插入排序前言:几种常用的Java数组排序

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Java遍历HashMap的6种常见方式

《Java遍历HashMap的6种常见方式》这篇文章主要给大家介绍了关于Java遍历HashMap的6种常见方式,方法包括使用keySet()、entrySet()、forEach()、迭代器以及分别... 目录1,使用 keySet() 遍历键,再通过键获取值2,使用 entrySet() 遍历键值对3,

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

JAVA保证HashMap线程安全的几种方式

《JAVA保证HashMap线程安全的几种方式》HashMap是线程不安全的,这意味着如果多个线程并发地访问和修改同一个HashMap实例,可能会导致数据不一致和其他线程安全问题,本文主要介绍了JAV... 目录1. 使用 Collections.synchronizedMap2. 使用 Concurren

Python如何精准判断某个进程是否在运行

《Python如何精准判断某个进程是否在运行》这篇文章主要为大家详细介绍了Python如何精准判断某个进程是否在运行,本文为大家整理了3种方法并进行了对比,有需要的小伙伴可以跟随小编一起学习一下... 目录一、为什么需要判断进程是否存在二、方法1:用psutil库(推荐)三、方法2:用os.system调用

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n