hashmap

jdk1.8源码分析之HashMap

冷暖自知 提交于 2019-12-24 11:53:00
原文链接:https://segmentfault.com/a/1190000012926722?utm_source=tag-newest 1.概述 本篇文章我们来聊聊大家日常开发中常用的一个集合类 - HashMap 。HashMap 最早出现在 JDK 1.2中,底层基于散列算法实现。HashMap 允许 null 键和 null 值,在计算哈键的哈希值时,null 键哈希值为 0。HashMap 并不保证键值对的顺序,这意味着在进行某些操作后,键值对的顺序可能会发生变化。另外,需要注意的是,HashMap 是非线程安全类,在多线程环境下可能会存在问题。 在本篇文章中,我将会对 HashMap 中常用方法、重要属性及相关方法进行分析。需要说明的是,HashMap 源码中可分析的点很多,本文很难一一覆盖,请见谅。 2.原理 上一节说到 HashMap 底层是基于散列算法实现,散列算法分为散列再探测和拉链式。HashMap 则使用了拉链式的散列算法,并在 JDK 1.8 中引入了红黑树优化过长的链表。数据结构示意图如下: 对于拉链式的散列算法,其数据结构是由数组和链表(或树形结构)组成。在进行增删查等操作时,首先要定位到元素的所在桶的位置,之后再从链表中定位该元素。比如我们要查询上图结构中是否包含元素 35 ,步骤如下: 定位元素 35 所处桶的位置, index = 35 %

Java sort a Hashmap on Value

不羁的心 提交于 2019-12-24 11:36:31
问题 Learning Java as I go (Python background). Simple word count program in Java 7 code (can not use J8!). I have a hash map of word:count pairs. Now I need to sort on count (decreasing order), and break ties with using word in alphabetical order. Have read s/o and I tried a treemap but it does not seem to handle ties very well so I don't think that is right. I have seen a lot of solutions posted that define a new class sortbyvalue and define a comparator. These will not work for me as I need to

JMeter - Store/update a hashmap object in a variable/property

对着背影说爱祢 提交于 2019-12-24 11:14:35
问题 I need to create a hash-map inside of a JSR233 sampler that will contain certain headers and properties elements for an external java utility that I will call using a Java Request Sampler . I am going to need to create many hashmaps as the key-value pairs will vary based on the systems I am testing. For example, I am going to have to change the JMSReplyTo , the JMSCorrelationID , $TextBody: fields for every hash-map. All of this is done inside one thread group, but I may also want to

Parsing JSON To HashMap With Dynamic Keys

二次信任 提交于 2019-12-24 10:48:37
问题 I am attempting to parse below json to java object. I cannnot able to parse to combined keys and values. "txn": { "TXN_TYPE": { "=": "SB" }, "TXN_DATE(0)": { "=": "0170501" }, "TXN_DATE(1)": { "=": "0170502" }, "TXN_DATE(2)": { "=": "0170503" }, "TXN_AMT(0)": { "=": "10.00" } "TXN_AMT(1)": { "=": "20.00" } "TXN_AMT(2)": { "=": "30.00" } } i am using org.json.JSONObject to parse and iterate over it and store it in HashMap. JSONObject txn= jObject.getJSONObject("txn"); if (txn!= null) { Map

clarifying facts behind Java's implementation of HashSet/HashMap

孤街醉人 提交于 2019-12-24 10:44:25
问题 1. I understand the different hash map mechanisms and the ways in which key collisions are handled (either open addressing -linear/quadratic probing, chaining, extendable hashing, etc. Which one does HashSet/HashMap make use of? 2. I realise that a good HashMap relies on a good hash function. How does Java's HashSet/HashMap hash the objects? I know that there is a hash function but so far for strings I have not needed to implement this. What if I now want to Hash a Java Object that I create -

Java中ArrayList与HashMap的遍历

喜你入骨 提交于 2019-12-24 08:35:49
1.ArrayList遍历 package com.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListDemo { public static void main(String args[]){    List<String> list = new ArrayList<String>();    list.add("luojiahui");    list.add("luojiafeng");    //方法1    Iterator it1 = list.iterator();    while(it1.hasNext()){    System.out.println(it1.next());    }    //方法2    for(Iterator it2 = list.iterator();it2.hasNext();){    System.out.println(it2.next());    }    //方法3    for(String tmp:list){    System.out.println(tmp);    }    //方法4    for(int i = 0;i < list.size(

Sorting Guava tables in descending order based on values

纵然是瞬间 提交于 2019-12-24 08:18:27
问题 I am planning to use guava tables for storing my values in the table format. I would like to know some function which performs the descending order sorting based on the value in the table ... Could you people throw some views about this. Thank you. 回答1: Just like with maps, you should copy the cellSet() , sort the cells by the values, and then put that into an order-preserving ImmutableTable . Ordering<Table.Cell<String, String, Integer>> comparator = new Ordering<Table.Cell<String, String,

Shared ownership of an str between a HashMap and a Vec

≯℡__Kan透↙ 提交于 2019-12-24 07:38:30
问题 I come from a Java/C#/JavaScript background and I am trying to implement a Dictionary that would assign each passed string an id that never changes. The dictionary should be able to return a string by the specified id. This allows to store some data that has a lot of repetitive strings far more efficiently in the file system because only the ids of strings would be stored instead of entire strings. I thought that a struct with a HashMap and a Vec would do but it turned out to be more

Locking value objects of a ConcurrentHashMap

社会主义新天地 提交于 2019-12-24 07:36:02
问题 In this post: Can we use Synchronized for each entry instead of ConcurrentHashMap? I asked if we can use Synchronized block to lock only entries of a HashMap, which I learnt we cannot. Now, my question is, if we have a ConcurrentHashMap (not hashMap) with values of type ArrayList, or TreeMap, then can I use that approach (using synchronized). Here what I mean: ConcurrentHashMap<String, ArrayList<String>> map = new ConcurrentHashMap<>(); synchronized (map.get("key")) { //do something with the

how to handle race condition with Runnable using separate thread for lambda inside hashmap

无人久伴 提交于 2019-12-24 07:13:15
问题 I was getting a code review from my software mentor (who is a C# programmer). He recommended I use lambda in my application to avoid using a large chain of if/else if statements. I created a hashmap with two types String and Runnable . He said it looks good, but now we have a problem where there is a race condition... meaning Runnable is creating a separate thread, and if the Runnable.run() thread does not win the race it could cause problems down the line (especially being in the constructor