hashmap

How to remove a key from Hash and get the remaining hash in Ruby/Rails?

梦想的初衷 提交于 2019-12-17 14:59:40
问题 To add a new pair to Hash I do: {:a => 1, :b => 2}.merge!({:c => 3}) #=> {:a => 1, :b => 2, :c => 3} Is there a similar way to delete a key from Hash ? This works: {:a => 1, :b => 2}.reject! { |k| k == :a } #=> {:b => 2} but I would expect to have something like: {:a => 1, :b => 2}.delete!(:a) #=> {:b => 2} It is important that the returning value will be the remaining hash, so I could do things like: foo(my_hash.reject! { |k| k == my_key }) in one line. 回答1: Rails has an except/except!

HashMap的tableSizeFor

纵饮孤独 提交于 2019-12-17 12:25:44
最近看HashMap的源码,发现一个tableSizeFor方法,看了半天始终不知道是在做什么,遂上网查资料。理解之后,只能拍手叫绝。记录一下,时常查看。 代码如下: static final int tableSizeFor(int cap) { int n = cap - 1; n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; } 首先,需要知道这个方法的功能:是返回 大于等于输入参数且最近的2的整数次幂的数。 然后,从二进制的角度分析一下执行过程。 假设n的二进制为01xxx...xxx。 对n右移1位:001xx...xxx,再位或:011xx...xxx 对n右移2为:00011...xxx,再位或:01111...xxx …… 从上面可以推理出,代码的执行过程是把最高位的1裂变到后面的所有位,且是以2倍的速度进行增长。因为java的int是32位的,所以只需要执行到n右移16位并进行位或即可。 当后面所有位都为1的时候,加上1(2的0次幂),即可得到大于n且最近的2的整数次幂 注意: 第一句代码 int n = cap - 1;

图解集合4:HashMap

陌路散爱 提交于 2019-12-17 11:39:31
初识HashMap 之前的List,讲了ArrayList、LinkedList,最后讲到了CopyOnWriteArrayList,就前两者而言,反映的是两种思想: (1)ArrayList以数组形式实现,顺序插入、查找快,插入、删除较慢 (2)LinkedList以链表形式实现,顺序插入、查找较慢,插入、删除方便 那么是否有一种数据结构能够结合上面两种的优点呢?有,答案就是HashMap。 HashMap是一种非常常见、方便和有用的集合,是一种键值对(K-V)形式的存储结构,下面将还是用图示的方式解读HashMap的实现原理, 四个关注点在HashMap上的答案 关 注 点 结 论 HashMap是否允许空 Key和Value都允许为空 HashMap是否允许重复数据 Key重复会覆盖、Value允许重复 HashMap是否有序 无序,特别说明这个无序指的是 遍历HashMap的时候,得到的元素的顺序基本不可能是put的顺序 HashMap是否线程安全 非线程安全 添加数据 首先看一下HashMap的一个存储单元Entry: static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; Entry<K,V> next; int hash; ... } 之前一篇写LinkedList的文章

What is the Java equivalent of Objective-C's NSDictionary?

你。 提交于 2019-12-17 10:59:47
问题 What is the closest implementation of Objective-C's NSDictionary in Java? To me, it looks like HashMap<String, Object> , but I'm very new to Objective-C. Thanks 回答1: NSDictionary is a class cluster (see the "Class Cluster" section in The Cocoa Fundamentals Guide), meaning that the actual implementation is hidden from you, the API user. In fact, the Foundation framework will choose the appropriate implementation at run time based on amount of data etc. In addition, NSDictionary can take any id

unordered_map thread safety

▼魔方 西西 提交于 2019-12-17 10:45:28
问题 I am changing a single thread program into multi thread using boost:thread library. The program uses unordered_map as a hasp_map for lookups. My question is.. At one time many threads will be writing, and at another many will be reading but not both reading and writing at the same time i.e. either all the threads will be reading or all will be writing. Will that be thread safe and the container designed for this? And if it will be, will it really be concurrent and improve performance? Do I

How to make HashMap work with Arrays as key?

旧街凉风 提交于 2019-12-17 10:45:20
问题 I am using boolean arrays as keys for a HashMap. But the problem is HashMap fails to get the keys when a different array is passed as key, although the elements are same. (As they are different objects). How can I make it work with arrays as keys ? Here is the code : public class main { public static HashMap<boolean[], Integer> h; public static void main(String[] args){ boolean[] a = {false, false}; h = new HashMap<boolean[], Integer>(); h.put(a, 1); if(h.containsKey(a)) System.out.println(

How to calculate HashMap memory usage in Java?

痞子三分冷 提交于 2019-12-17 10:37:11
问题 I was asked in an interview to calculate the memory usage for HashMap and how much estimated memory it will consume if you have 2 million items in it. For example: Map <String,List<String>> mp=new HashMap <String,List<String>>(); The mapping is like this. key value ----- --------------------------- abc ['hello','how'] abz ['hello','how','are','you'] How would I estimate the memory usage of this HashMap Object in Java? 回答1: The short answer To find out how large an object is, I would use a

Difference between HashMap and ArrayList in Java?

蹲街弑〆低调 提交于 2019-12-17 10:23:23
问题 In Java, ArrayList and HashMap are used as collections. But I couldn't understand in which situations we should use ArrayList and which times to use HashMap . What is the major difference between both of them? 回答1: You are asking specifically about ArrayList and HashMap, but I think to fully understand what is going on you have to understand the Collections framework. So an ArrayList implements the List interface and a HashMap implements the Map interface. So the real question is when do you

HashMap initialization parameters (load / initialcapacity)

与世无争的帅哥 提交于 2019-12-17 10:17:11
问题 What values should I pass to create an efficient HashMap / HashMap based structures for N items? In an ArrayList , the efficient number is N (N already assumes future grow). What should be the parameters for a HashMap ? ((int)(N * 0.75d), 0.75d)? More? Less? What is the effect of changing the load factor? 回答1: Regarding the load factor, I'll simply quote from the HashMap javadoc: As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher

Super high performance C/C++ hash map (table, dictionary) [closed]

穿精又带淫゛_ 提交于 2019-12-17 10:10:39
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I need to map primitive keys (int, maybe long) to struct values in a high-performance hash map data structure. My program will have a few hundred of these maps, and each map will generally have at most a few thousand entries. However, the maps will be "refreshing" or "churning" constantly; imagine processing