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

相关文章

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

如何通过try-catch判断数据库唯一键字段是否重复

《如何通过try-catch判断数据库唯一键字段是否重复》在MyBatis+MySQL中,通过try-catch捕获唯一约束异常可避免重复数据查询,优点是减少数据库交互、提升并发安全,缺点是异常处理开... 目录1、原理2、怎么理解“异常走的是数据库错误路径,开销比普通逻辑分支稍高”?1. 普通逻辑分支 v

从基础到高级详解Python数值格式化输出的完全指南

《从基础到高级详解Python数值格式化输出的完全指南》在数据分析、金融计算和科学报告领域,数值格式化是提升可读性和专业性的关键技术,本文将深入解析Python中数值格式化输出的相关方法,感兴趣的小伙... 目录引言:数值格式化的核心价值一、基础格式化方法1.1 三种核心格式化方式对比1.2 基础格式化示例

Java中HashMap的用法详细介绍

《Java中HashMap的用法详细介绍》JavaHashMap是一种高效的数据结构,用于存储键值对,它是基于哈希表实现的,提供快速的插入、删除和查找操作,:本文主要介绍Java中HashMap... 目录一.HashMap1.基本概念2.底层数据结构:3.HashCode和equals方法为什么重写Has

Linux实现查看某一端口是否开放

《Linux实现查看某一端口是否开放》文章介绍了三种检查端口6379是否开放的方法:通过lsof查看进程占用,用netstat区分TCP/UDP监听状态,以及用telnet测试远程连接可达性... 目录1、使用lsof 命令来查看端口是否开放2、使用netstat 命令来查看端口是否开放3、使用telnet

java -jar example.jar 产生的日志输出到指定文件的方法

《java-jarexample.jar产生的日志输出到指定文件的方法》这篇文章给大家介绍java-jarexample.jar产生的日志输出到指定文件的方法,本文给大家介绍的非常详细,对大家的... 目录怎么让 Java -jar example.jar 产生的日志输出到指定文件一、方法1:使用重定向1、

C++归并排序代码实现示例代码

《C++归并排序代码实现示例代码》归并排序将待排序数组分成两个子数组,分别对这两个子数组进行排序,然后将排序好的子数组合并,得到排序后的数组,:本文主要介绍C++归并排序代码实现的相关资料,需要的... 目录1 算法核心思想2 代码实现3 算法时间复杂度1 算法核心思想归并排序是一种高效的排序方式,需要用

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu