hashmap

HashMap实现原理及源码分析

こ雲淡風輕ζ 提交于 2019-12-20 10:14:27
HashMap 概述 HashMap 是基于哈希表的 Map 接口的非同步实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。此类不保证映射的顺序,特别是它不保证该顺序恒久不变。 此实现假定哈希函数将元素适当地分布在各桶之间,可为基本操作(get 和 put)提供稳定的性能。迭代 collection 视图所需的时间与 HashMap 实例的“容量”(桶的数量)及其大小(键-值映射关系数)成比例。所以,如果迭代性能很重要,则不要将初始容量设置得太高或将加载因子设置得太低。也许大家开始对这段话有一点不太懂,不过不用担心,当你读完这篇文章后,就能深切理解这其中的含义了。 需要注意的是:Hashmap 不是同步的,如果多个线程同时访问一个 HashMap,而其中至少一个线程从结构上(指添加或者删除一个或多个映射关系的任何操作)修改了,则必须保持外部同步,以防止对映射进行意外的非同步访问。 HashMap 的数据结构 在 Java 编程语言中,最基本的结构就是两种,一个是数组,另外一个是指针(引用),HashMap 就是通过这两个数据结构进行实现。 HashMap实际上是一个“链表散列”的数据结构,即数组和链表的结合体。 从上图中可以看出,HashMap 底层就是一个数组结构,数组中的每一项又是一个链表。当新建一个 HashMap 的时候,就会初始化一个数组。

Using HashMap to map a String and int

血红的双手。 提交于 2019-12-20 09:41:34
问题 I have a ListView showing names of countries. I have stored the names in strings.xml as a string-array called country_names . In populating the ListView, I use an ArrayAdapter which reads from strings.xml: String[] countryNames = getResources().getStringArray(R.array.country_names); ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(this, R.layout.checked_list, countryNames); myList.setAdapter(countryAdapter); Now I also have a CountryCode for each country. When a particular

Python equivalent for HashMap [duplicate]

纵饮孤独 提交于 2019-12-20 09:35:48
问题 This question already has answers here : Hash Map in Python (9 answers) Closed 6 years ago . I'm new to python. I have a directory which has many subfolders and files. So in these files I have to replace some specified set of strings to new strings. In java I have done this using HashMap . I have stored the old strings as keys and new strings as their corresponding values. I searched for the key in the hashMap and if I got a hit, I replaced with the corresponding value. Is there something

Retrieve all values from HashMap keys in an ArrayList Java

霸气de小男生 提交于 2019-12-20 09:29:47
问题 Good day, this is kind of confusing me now(brain freeze!) and seem to be missing something. Have an ArrayList which i populate with a HashMap. now i put in my HashMap and arraylist. Map.put(DATE, value1); Map.put(VALUE, value2); arraylist.put(Map); Since am parsing a JSON, the arraylist increases in significant size. now my question is how do you get the values from both map keys in the arraylist? i have tried this if(!list.isEmpty()){ // list is an ArrayList for(int k = 0; k < list.size(); k

How to make object instance a hash key in Ruby?

♀尐吖头ヾ 提交于 2019-12-20 09:28:41
问题 I have a class Foo with a few member variables. When all values in two instances of the class are equal I want the objects to be 'equal'. I'd then like these objects to be keys in my hash. When I currently try this, the hash treats each instance as unequal. h = {} f1 = Foo.new(a,b) f2 = Foo.new(a,b) f1 and f2 should be equal at this point. h[f1] = 7 h[f2] = 8 puts h[f1] should print 8 回答1: See http://ruby-doc.org/core/classes/Hash.html Hash uses key.eql? to test keys for equality. If you need

Why is HashMap faster than HashSet?

删除回忆录丶 提交于 2019-12-20 09:19:15
问题 I have been reading/researching the reason why HashMap is faster than HashSet . I am not quite understanding the following statements: HashMap is faster than HashSet because the values are associated to a unique key. In HashSet , member object is used for calculating hashcode value which can be same for two objects so equals() method is used to check for equality. If it returns false , that means the two objects are different. In HashMap , the hashcode value is calculated using the key object

Ruby value of a hash key?

情到浓时终转凉″ 提交于 2019-12-20 08:57:13
问题 I've got a list of values that are in a Ruby hash. Is there a way to check the value of the key and if it equals "X", then do "Y"? I can test to see if the hash has a key using hash.has_key? , but now I need to know if hash.key == "X" then... ? 回答1: Hashes are indexed using the square brackets ([]). Just as arrays. But instead of indexing with the numerical index, hashes are indexed using either the string literal you used for the key, or the symbol. So if your hash is similar to hash = {

Volatile HashMap vs ConcurrentHashMap

浪子不回头ぞ 提交于 2019-12-20 08:43:46
问题 I have a cache class which contains a volatile HashMap<T> to store cache items. I'm curious what would be the consequences of changing volatile HashMap to ConcurrentHashMap ? Would i gain performance increase? This cache is readonly cache. What would be the best option to use? just HashMap? Cache is being populated on a interval. 回答1: First, it appears you don't understand what the volatile keyword does. It makes sure that if the reference value held by the variable declared volatile changes,

HashMap Searching For A Specific Value in Multiple Keys

浪子不回头ぞ 提交于 2019-12-20 07:58:15
问题 I'm checking to see if a key in my HashMap exists, if it does, I also want to check to see if any other keys have a value with the same name as that of the original key I checked for or not. For example I have this. System.out.println("What course do you want to search?"); String searchcourse = input.nextLine(); boolean coursefound = false; if(hashmap.containsKey(searchcourse) == true){ coursefound = true; } This checks to see if the key exists in my hashmap, but now I need to check every

Array to Hash Ruby

ぃ、小莉子 提交于 2019-12-20 07:56:57
问题 Okay so here's the deal, I've been googling for ages to find a solution to this and while there are many out there, they don't seem to do the job I'm looking for. Basically I have an array structured like this ["item 1", "item 2", "item 3", "item 4"] I want to convert this to a Hash so it looks like this { "item 1" => "item 2", "item 3" => "item 4" } i.e. the items that are on the 'even' indexes are the keys and the items on the 'odd' indexes are the values. Any ideas how to do this cleanly?