hashmap

Java why a Map of Map (ex: Map<String,Map<String,String>>) not serializeable

筅森魡賤 提交于 2020-01-15 02:32:37
问题 We are using HashMap in JDK 1.7 and I face some issue during the code review with SonarQube . Please consider below samples: public class SerializationTest implements Serializable { private Map<String,String> test1=new HashMap<>(); //Serializeable private Map<ANEnum,String> test2=new HashMap<>(); //Serializeable private Map<String,ASerializeableObject> test3=new HashMap<>(); //Serializeable private Map<String,Map<String,String>> test4=new HashMap<>(); //Not Serializeable private Map<ANEnum

Java why a Map of Map (ex: Map<String,Map<String,String>>) not serializeable

拈花ヽ惹草 提交于 2020-01-15 02:31:05
问题 We are using HashMap in JDK 1.7 and I face some issue during the code review with SonarQube . Please consider below samples: public class SerializationTest implements Serializable { private Map<String,String> test1=new HashMap<>(); //Serializeable private Map<ANEnum,String> test2=new HashMap<>(); //Serializeable private Map<String,ASerializeableObject> test3=new HashMap<>(); //Serializeable private Map<String,Map<String,String>> test4=new HashMap<>(); //Not Serializeable private Map<ANEnum

Java最新面试题

99封情书 提交于 2020-01-14 19:17:37
2019最新整理JAVA面试题附答案 作者:Jack 包含的模块: 本文分为十九个模块,分别是:Java 基础、容器、多线程、反射、对象拷贝、Java Web 、异常、网络、设计模式、Spring/Spring MVC、Spring Boot/Spring Cloud、Hibernate、MyBatis、RabbitMQ、Kafka、Zookeeper、MySQL、Redis、JVM 如下图所示: 共包含 208 道面试题,本文的宗旨是为读者朋友们整理一份详实而又权威的面试清单 ==================================================== 一. Java 基础模块 1.JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,Java 开发工具包,提供了 Java 的开发环境和运行环境。 JRE:Java Runtime Environment 的简称,Java 运行环境,为 Java 的运行提供了所需环境。 具体来说 JDK 其实包含了 JRE,同时还包含了编译 Java 源码的编译器 Javac,还包含了很多 Java 程序调试和分析的工具。简单来说:如果你需要运行 Java 程序,只需安装 JRE 就可以了,如果你需要编写 Java 程序,需要安装 JDK。 2.== 和 equals 的区别是什么

为什么ConcurrentHashMap不能存null key和null value?

 ̄綄美尐妖づ 提交于 2020-01-14 18:52:37
ConcurrentHashMap是J.U.C包下著名的线程安全类。通常作为高并发情境下HashMap的替代工具。 我们都知道HashMap是支持Null Key 和 Null Value的,当遇到Null Key时,我们会将它hash到HashMap的内部Table[]的第0个位置去。 但是ConcurrentHashMap不允许使用Null Key和Null Value,如果遇到会直接抛出NullPointException,这样设计是为什么呢? 首先,很多开发者认为,null是一个非常不好的设计,他应该只被用来表示未初始化的引用类型; 再者,在并发中,null是一个非常严重的问题,高并发下,尽可能地消除歧义是必要的,你需要知道究竟是没有找到,还是它的值为null; 对于key不为null的设置,我个人倾向于这是一种开发规范的问题,一家人就要整整齐齐,你就和value一起别为null了。 并且null的使用需要谨慎,所以尽可能地防范于未然,也是api设计者需要考虑的问题。 来源: https://www.cnblogs.com/hekiraku/p/12193326.html

Java容器系列-HashMap源码分析

人走茶凉 提交于 2020-01-14 17:57:11
HashMap 实现了 Map 接口。HashMap 使用的很广泛,但不是线程安全的,如果在多线程中使用,必须需要额外提供同步机制(多线程情况下推荐使用 ConCurrentHashMap)。 HashMap 的类图相对简单,主要就是继承了 AbstractMap,有一点需要注意,虽然没有实现 Iterable 接口,但 HashMap 本身还是实现了迭代器的功能。 本文基于 JDK1.8 成员变量及常量 HashMap 是一个 Node[] 数组,每一个下标称之为一个 桶 。 每一个键值对都是使用 Node 来存储,这是一个单链表的数据结构。每个桶上可以通过链表来存储多个键值对。 常量 HashMap 中用到的常量及其意义如下: // 初始容量(桶的个数) 2^4 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // 最大容量(桶的个数) 2^30static final int MAXIMUM_CAPACITY = 1 << 30;// 默认的装载因子(load factor),除非特殊原因,否则不建议修改static final float DEFAULT_LOAD_FACTOR = 0.75f;// 单个桶上的元素个数大于这个值从链表转成树(树化操作)static final int TREEIFY_THRESHOLD

ConcurrentHashMap原理解析

僤鯓⒐⒋嵵緔 提交于 2020-01-14 15:45:13
概述 ConcurrentHashMap是JDK提供的一个线程安全的集合类,它内部的结构原理和我们常用的HashMap基本是一致,那我们可以先来认识一下HashMap,这样基本上也能大致明白ConcurrentHashMap了。 数据结构 HashMap与ConcurrentHashMap都是用来存放一种键值对形式的数据,那它们内部的数据结构是怎么样的呢? 首先来看看HashMap的put方法 public V put(K key, V value) { // put方法中首先对key进行hash运算,再调用putVal return putVal(hash(key), key, value, false, true); } final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null

Morse Code Conversion using Java

谁都会走 提交于 2020-01-14 14:45:14
问题 I'm trying to learn about hashmaps and 2D arrays in Java. We have an assignment due to have a scanner that accepts a string and converts it to morse code. The code we're using is based of a method full of if statements, but I want to learn how I would do something like this using lists, hashmaps, or 2D arrays. My code is as follows: import java.util.*; public class MorseConversion { public static void main(String[] args) { Scanner userInput = new Scanner(System.in); System.out.println("Please

How to Get Values from List of HashMap?

岁酱吖の 提交于 2020-01-14 11:58:28
问题 I have a List of HashMap 's which has key of type Integer and value of type Long . List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>(); for (int i = 0; i < 10; i++) { HashMap<Integer, Long> mMap = new HashMap<Integer, Long>(); mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i)); ListofHash.add(mMap); } Now, how do I retrieve the key and value from the list of HashMap ? If using Collection class is the solution, please enlight me. Update 1: Actually i am

Java中HashMap的初始容量设置

旧街凉风 提交于 2020-01-14 08:56:12
Java中HashMap的初始容量设置: 根据阿里巴巴Java开发手册上建议HashMap初始化时设置已知的大小,如果不超过16个,那么设置成默认大小16: 集合初始化时, 指定集合初始值大小。 说明: HashMap使用HashMap(int initialCapacity)初始化, 正例:initialCapacity = (需要存储的元素个数 / 负载因子) + 1。注意负载因子(即loader factor)默认为0.75, 如果暂时无法确定初始值大小,请设置为16(即默认值)。 反例:HashMap需要放置1024个元素,由于没有设置容量初始大小,随着元素不断增加,容量7次被迫扩大,resize需要重建hash表,严重影响性能。 而对于为什么负载因子是0.75,答案可以在《数据结构与算法分析 Java语言描述》的散列章节中找到 参考: http://blog.csdn.net/gaopu12345/article/details/50831631 http://blog.csdn.net/ghsau/article/details/16843543 http://blog.csdn.net/ghsau/article/details/16890151 http://republicw.iteye.com/blog/1218692 http://www.cnblogs

Iterating HashMap on order it has been set

爷,独闯天下 提交于 2020-01-14 06:59:11
问题 I've set a HashMap on certain order but it is iterated on a strange order! Please consider code below: HashMap<String, String> map = new HashMap<String, String>(); map.put("ID", "1"); map.put("Name", "the name"); map.put("Sort", "the sort"); map.put("Type", "the type"); ... for (String key : map.keySet()) { System.out.println(key + ": " + map.get(key)); } and the result: Name: the name Sort: the sort Type: the type ID: 1 I need to iterate it in order i've put the entries. Any help will be