hashmap

Java HashMap的put操作(Java1.8)

孤街浪徒 提交于 2019-12-17 09:04:16
https://www.cnblogs.com/JzedyBlogs/p/10208295.html 写得非常好: 这个是Java1.8 -------------------------------- 1 public V put(K key, V value) { 2 return putVal(hash(key), key, value, false, true); 3 } 4 5 static final int hash(Object key) {//hash函数,用于索引定位 6 int h; 7 return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); 8 } 9 10 final V putVal(int hash, K key, V value, boolean onlyIfAbsent, 11 boolean evict) { 12 Node<K,V>[] tab; Node<K,V> p; int n, i; 13 if ((tab = table) == null || (n = tab.length) == 0) 14 n = (tab = resize()).length;//存储数据Node没有初始化,此时初始化 15 if ((p = tab[i = (n - 1) & hash])

[Java复习] 集合框架 Collection

╄→гoц情女王★ 提交于 2019-12-17 08:36:15
Q1 Collection java的集合以及集合之间的继承关系? 数组和链表的区别? 固定长度,连续内存,不能扩展,随机访问快,插入删除慢。链表相反 List, Set, Map的区别? List,Set继承Collection接口 List可以放重复数据,Set不能,Map是k-v对 List和Map的实现方式以及存储方式? ArrayList: 底层动态数组。随机访问快,增删慢,线程不安全。 扩容导致数组复制,批量删除会导致找两个集合交集,效率低。 LinkedList: 底层链表(双向列表)。增删快,查找慢,线程不安全。 遍历: 1.普通for循环,元素越多后面越慢 2.迭代器:每次访问,用游标记录当前位置 根据下标获取node,会根据index处于前半段还是后半段进行折半,提升效率。 HashMap: 散列表, 数组+链表+红黑树(JDK1.8) 默认16, 扩容2的幂 Q2 List ArrayList实现原理? 动态数组,默认10,扩容grow(minCapacity),增加到1.5倍 ArrayList和LinkedList的区别,以及应用场景? 1.动态数组和双向队列链表。 2.ArrayList( 实现了RandomAccess接口 )用for循环遍历优于迭代器,LinkedList则相反。 3.ArrayList在数组任意位置插入,或导致该位置后面元素重新排列

Loading a map using Properties class

吃可爱长大的小学妹 提交于 2019-12-17 07:54:29
问题 I have a map with 75000 entries and each entry value will be of size 10kb on average. I load this map into memory using Properties class . But due to the size of the map , I get OutOfMemoryException when the RAM on the host is small. One option that i have is to read the entries in batches (like 10,000) into memory instead of loading the complete map. Read the next 10k after processing the initial 10k. Is there any way to accomplish this using Properties class. Also, is there any better

Limiting the max size of a HashMap in Java

六眼飞鱼酱① 提交于 2019-12-17 07:14:31
问题 I want to limit the maximum size of a HashMap to take metrics on a variety of hashing algorithms that I'm implementing. I looked at the loadfactor in one of HashMap 's overloaded constructors. HashMap(int initialCapacity, float loadFactor) I tried setting the loadFactor to 0.0f in the constructor (meaning that I don't want the HashMap to grow in size EVER) but javac calls this invalid: Exception in thread "main" java.lang.IllegalArgumentException: Illegal load factor: 0.0 at java.util.HashMap

How can I mutate other elements of a HashMap when using the entry pattern?

删除回忆录丶 提交于 2019-12-17 06:57:05
问题 I'd like to use a HashMap to cache an expensive computation that's dependent on other entries in the map. The entry pattern only provides a mutable reference to the matched value, but not to the rest of the HashMap . I'd really appreciate feedback on a better way to solve this (incorrect) toy example: use std::collections::HashMap; use std::collections::hash_map::Entry::{Occupied, Vacant}; fn compute(cache: &mut HashMap<u32, u32>, input: u32) -> u32 { match cache.entry(input) { Vacant(entry)

ArrayList as key in HashMap

只愿长相守 提交于 2019-12-17 06:48:11
问题 Would it be possible to add an ArrayList as the key of HashMap . I would like to keep the frequency count of bigrams. The bigram is the key and the value is its frequency. For each of the bigrams like "he is", I create an ArrayList for it and insert it into the HashMap . But I am not getting the correct output. public HashMap<ArrayList<String>, Integer> getBigramMap(String word1, String word2) { HashMap<ArrayList<String>, Integer> hm = new HashMap<ArrayList<String>, Integer>(); ArrayList

Using an instance of an object as a key in hashmap, and then access it with exactly new object? [duplicate]

别等时光非礼了梦想. 提交于 2019-12-17 06:34:31
问题 This question already has answers here : What issues should be considered when overriding equals and hashCode in Java? (11 answers) Closed 4 years ago . I have a hasmap with a key object, HashMap<Key, Object> test; and make new Key("the same") as key.. so its like..: test.put(new Key("the same"), someObject); (without storing that key in a variable) so.. after a while... i want to access the hashmap, because i do not have the object, i've tried to make new Key("the same") and make it as a key

Why does HashMap require that the initial capacity be a power of two?

无人久伴 提交于 2019-12-17 06:28:11
问题 I was going through Java's HashMap source code when I saw the following //The default initial capacity - MUST be a power of two. static final int DEFAULT_INITIAL_CAPACITY = 16; My question is why does this requirement exists in the first place? I also see that the constructor which allows creating a HashMap with a custom capacity converts it into a power of two: int capacity = 1; while (capacity < initialCapacity) capacity <<= 1; Why does the capacity always has to be a power of two? Also,

C# Java HashMap equivalent

只谈情不闲聊 提交于 2019-12-17 06:22:09
问题 Coming from a Java world into a C# one is there a HashMap equivalent? If not what would you recommend? 回答1: Dictionary is probably the closest. System.Collections.Generic.Dictionary implements the System.Collections.Generic.IDictionary interface (which is similar to Java's Map interface). Some notable differences that you should be aware of: Adding/Getting items Java's HashMap has the put and get methods for setting/getting items myMap.put(key, value) MyObject value = myMap.get(key) C#'s

C# Java HashMap equivalent

泄露秘密 提交于 2019-12-17 06:22:02
问题 Coming from a Java world into a C# one is there a HashMap equivalent? If not what would you recommend? 回答1: Dictionary is probably the closest. System.Collections.Generic.Dictionary implements the System.Collections.Generic.IDictionary interface (which is similar to Java's Map interface). Some notable differences that you should be aware of: Adding/Getting items Java's HashMap has the put and get methods for setting/getting items myMap.put(key, value) MyObject value = myMap.get(key) C#'s