hashmap

21道 Redis 常见面试题,必须掌握!

怎甘沉沦 提交于 2019-12-10 21:25:08
1.什么是redis? Redis 是一个基于内存的高性能key-value数据库。 2.Reids的特点 Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载在内存当中进行操作,定期通过异步操作把数据库数据flush到硬盘上进行保存。因为是纯内存操作,Redis的性能非常出色,每秒可以处理超过 10万次读写操作,是已知性能最快的Key-Value DB。Redis的出色之处不仅仅是性能,Redis最大的魅力是支持保存多种数据结构,此外单个value的最大限制是1GB,不像 memcached只能保存1MB的数据,因此Redis可以用来实现很多有用的功能,比方说用他的List来做FIFO双向链表,实现一个轻量级的高性 能消息队列服务,用他的Set可以做高性能的tag系统等等。另外Redis也可以对存入的Key-Value设置expire时间,因此也可以被当作一 个功能加强版的memcached来用。Redis的主要缺点是数据库容量受到物理内存的限制,不能用作海量数据的高性能读写,因此Redis适合的场景主要局限在较小数据量的高性能操作和运算上。 3.使用redis有哪些好处? 1.速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1)2.支持丰富数据类型,支持string,list

Implementation independent way to see if a map contains null key

断了今生、忘了曾经 提交于 2019-12-10 21:04:59
问题 I have an API that receives a parameter which implements the Map interface. I need to check to see if this Map contains any null key. The problem is there are certain implementations of Map , such as ConcurrentHashMap which throw NPE if you call contains(null) on them. What is a good, implementation independent way to see if an object that adheres to the Map interface contains a null key or not? Note that I can't use keySet and check to see if that contains a null, because ConcurrentHashMap

Java Hash Table Issue with Object Reference

怎甘沉沦 提交于 2019-12-10 20:39:58
问题 I have a Hash Table like, HashTable ht = { (1, 1), (2, 1), (3, 1) } Now, I implement it like, Integer foo = Integer(1) and declare hash table like, HashTable ht = { (foo, foo), (2, foo), (3, foo) } Now, as per I understood from this, it will reduce heap space used by JVM. Is this correct ? Another point is that, in C, I usually use structure like, HashTable ht = { (1, mem), (2, mem), (3, mem) } { where mem is memory location (say 10) of 1 } And then use the location to access the value. Now,

JSON to hashmap (Jackson)

亡梦爱人 提交于 2019-12-10 19:16:05
问题 I want to convert JSON to a HashMap with Jackson. This is my JSON: String json = "[{\"Opleidingen\":[{\"name\":\"Bijz. trajecten zorg en welzijn\",\"afk\":\"BTZW\",\"id\":\"0\"},{\"name\":\"Bouwkunde\",\"afk\":\"Bwk\",\"id\":\"14\"},{\"name\":\"Electrotechniek / mechatronica\",\"afk\":\"EltMe\",\"id\":\"15\"},{\"name\":\"Extern\",\"afk\":\"Extern\",\"id\":\"16\"},{\"name\":\"Gezondheidszorg\",\"afk\":\"Zorg\",\"id\":\"17\"},{\"name\":\"Handel\",\"afk\":\"Hand\",\"id\":\"18\"},{\"name\":\

Recursive Hash transformation function in ruby

主宰稳场 提交于 2019-12-10 19:03:58
问题 I have the following swagger (openAPI) definition of a response schema: h = { "type"=>"object", "properties"=>{ "books"=>{ "type"=>"array", "items"=>{ "type"=>"object", "properties"=>{ "urn" =>{ "type"=>"string" }, "title"=>{ "type"=>"string" } } } } } } And would like to transform this into the following format, so as to be able to display this response as a tree: { "name"=>"200", "children"=> [ { "name"=>"books (array)", "children"=> [ {"name"=>"urn (string)" }, {"name"=>"title (string)" }

how do i preload a hashmap in an object(without put method)?

十年热恋 提交于 2019-12-10 18:49:52
问题 I have a class and it has a couple data structures in it among which is a hashmap. But I want the hashmap to have default values so I need to preload it. How do I do this since I can't use put method inside the object? class Profile { HashMap closedAges = new HashMap(); closedAges.put("19"); } I fixed it with this but I had to use a method within the object. class Profile { HashMap closedAges = loadAges(); HashMap loadAges() { HashMap closedAges = new HashMap(); String[] ages = {"19", "46",

Java集合

蓝咒 提交于 2019-12-10 18:16:48
HashMap的存储和查找原理 在HashMap中要找到某个元素,需要根据key的hash值来求得对应数组中的位置。如何计算这个位置就是hash算法。前面说过HashMap的数据结构是数组和链表的结合,所以我们当然希望这个HashMap里面的元素位置尽量的分布均匀些,尽量使得每个位置上的元素数量只有一个,那么当我们用hash算法求得这个位置的时候,马上就可以知道对应位置的元素就是我们要的,而不用再去遍历链表,这样就大大优化了查询的效率。 put 方法的源代码可以看出,当程序试图将一个key-value对放入HashMap中时,程序首先根据该 key的 hashCode() 返回值决定该 Entry 的存储位置:如果两个 Entry 的 key 的 hashCode() 返回值相同,那它们的存储位置相同。如果这两个 Entry 的 key 通过 equals 比较返回 true,新添加 Entry 的 value 将覆盖集合中原有 Entry的 value,但key不会覆盖。如果这两个 Entry 的 key 通过 equals 比较返回 false,新添加的 Entry 将与集合中原有 Entry 形成 Entry 链,而且新添加的 Entry 位于 Entry 链的头部。从HashMap中get元素时,首先计算key的hashCode,找到数组中对应位置的某一元素

Why hash_map and unordered_map on my machine are extremely slow?

点点圈 提交于 2019-12-10 16:37:51
问题 I tested them with this code (on Visual Studio 2010 sp1): #include <ctime> #include <iostream> #include <map> #include <unordered_map> #include <hash_map> int main() { clock_t time; int LOOP = (1 << 16); std::map<int, int> my_map; std::unordered_map<int, int> map_unordered_map; std::hash_map<int, int> my_hash_map; time = clock(); for (int i = 0; i != LOOP; ++i) { my_map[i] = i; } std::cout << "map: " << ((double)(clock() - time) / CLOCKS_PER_SEC) << std::endl; time = clock(); for (int i = 0;

Efficiently iterate through all MATCHING keys in a hashmap?

我的梦境 提交于 2019-12-10 16:27:33
问题 I have a HashMap with millions of entries. Need to retrieve all entries whose keys match a specific set of criteria (in this case, each key is an object with two integer properties; I need to retrieve all keys where each of these integers fall within a specified range). What is the fastest, most efficient way to iterate through all such keys? UPDATE: In this particular case, though I didn't specify it up front, the first integer in the key has a natural precedence over the second integer. 回答1

How to create a nested dictionary from a csv file with N rows in Python

岁酱吖の 提交于 2019-12-10 16:18:55
问题 I was looking for a way to read a csv file with an unknown number of columns into a nested dictionary. i.e. for input of the form file.csv: 1, 2, 3, 4 1, 6, 7, 8 9, 10, 11, 12 I want a dictionary of the form: {1:{2:{3:4}, 6:{7:8}}, 9:{10:{11:12}}} This is in order to allow O(1) search of a value in the csv file. Creating the dictionary can take a relatively long time, as in my application I only create it once, but search it millions of times. I also wanted an option to name the relevant