hashmap

如果面试官问你HashMap和ConcurrentHashMap以及HashTable的区别以及实现原理

本小妞迷上赌 提交于 2019-12-11 23:27:36
1:HashMap是线程不安全的 ConcurrentHashMap是线程安全的 HashTable是线程安全的 HashMap是数组加链表的方式实现的,根据传递过来的key获取这个key的hash值,然后通过一些运算,获取这个key的索引位置,将该值放在这个索引的链表集合里面,查询的时候也是这样先拿到索引位置,然后去链表集合里面去获取。 HashTable HashTable也是通过数组加链表的方式实现的,但是他实现同步的方式是锁住整个Hash表,效率很慢 ConcurrentHashMap也是通过数组加链表的方式实现的,这些链表集合里面存在一些分段锁的操作,达到数据一致性 来源: CSDN 作者: 石元璧 链接: https://blog.csdn.net/weixin_37715260/article/details/103487909

listview about adapter, for create a hashmap, why bitmap type imported cannot show image in listview?

戏子无情 提交于 2019-12-11 23:15:09
问题 list_data = list_data_add("111","222",icon); adapter = new SimpleAdapter( this, list_data, R.layout.list_item_detail, new String[]{"title","desc","icon"}, new int[]{R.id.title, R.id.desc, R.id.icon} ); listview.setAdapter(adapter); private List<Map<String, Object>> list_data_add(String title, String desc, Bitmap icon) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map; map = new HashMap<String, Object>(); map.put("title", title); map.put("desc",

Algorithm for matching HashMap key with another random HashMap key, never duplicating values or matching itself

谁都会走 提交于 2019-12-11 22:42:36
问题 It's possible this is not most logical/easyiest/effective way of doing this. I would love some input on a better logic, so I'll rather try to explain the problem. List: Jon - null Dad - null Mom - null Thor - null July - null I want to create a method that matches these with another 'key' at random, not having duplicate values or having a key and value which are the same. Another problem is if there are for example 3 keys. 1 - null 2 - null 3 - null Iteration 1: 1 - 2 2 - null 3 - null

Double Hashing java

南楼画角 提交于 2019-12-11 19:36:30
问题 public class HashTable <K, V> implements Table<K, V>{ int idx; PairHolder table[]; public HashTable(int size){ table=new PairHolder[size]; } public void put(K key, V value) { int hVal = key.hashCode(); int hashVal = hashFunc1(hVal); int stepSize = hashFunc2(hVal); while(table[index]!=null&&table[index].getFirst() != null){ index += temp; index %=table.length; } table[index].value=value; } public int hashFunc1(int key){ int abs = Math.abs(key%table.length); return abs; } public int hashFunc2

ConcurrentModificationException in HashMap

[亡魂溺海] 提交于 2019-12-11 19:29:25
问题 I am taking the insurance details from user and saving them in a hashmap. And I have button called SAVE. So only, when user clicks on this button all insurances should save in Database So I am taking a random generated id as reference until I save the details in Database After saving in database, I need to update this hashmap with key as autogenerated id public void saveInformationInDatabase(int patientId) { // getAllInsurances returns HashMapn<Integer, HashMap<Integer, InsuranceInformation>>

Reading key-value pairs as fast as possible in C++ from file

北城余情 提交于 2019-12-11 19:03:16
问题 I have a file with roughly 2 million lines like this: 2s,3s,4s,5s,6s 100000 2s,3s,4s,5s,8s 101 2s,3s,4s,5s,9s 102 The first comma separated part indicates a poker result in Omaha, while the latter score is an example "value" of the cards. It is very important for me to read this file as fast as possible in C++, but I cannot seem to get it to be faster than a simple approach in Python (4.5 seconds) using the base library. Using the Qt framework (QHash and QString), I was able to read the file

How do I iterate through an entire HashMap

孤者浪人 提交于 2019-12-11 18:33:54
问题 If the method I need to use looks like this... public void printMessages(Message mm) { } How do I iterate through the entire HashMap that looks like this... HashMap<String, ArrayList<User>> hM = new HashMap<>(); to send each User the toString message generated by the Message mm? I'm stuck thanks for the advice. 回答1: To iterate over a map, use a foreach on the entrySet() Map<K, V> map; for (Map.Entry<K, V> entry : map.entrySet()) { // do something with the key/value K key = entry.getKey(); V

HashMap和ConcurrentHashMap

邮差的信 提交于 2019-12-11 18:31:57
HashMap和ConcurrentHashMap JDK1.7-HashMap 实现的数据结构:数组和链表 map[] + Entry<K,V>; map[] 为数组:结构为数组; Entry<K,V>为链表:结构为 { String key; //key值 String value; //value 值 Entry<K,V> next; //下一个Entry int hash; //key的hashcode值 } 数组默认初始化大小为16,最大的数组长度是1<<30,加载因子是0.75 线程不安全 /** * map的put方法 */ public V put(K key, V value) { /** * 这段代码执行,inflateTable(threshold) 会将table初始化为一个长度为16的Entry数组。 */ if (table == EMPTY_TABLE) { inflateTable(threshold); } /** * key为null时,能够进行插入,所以HashMap支持为null的key */ if (key == null) return putForNullKey(value); /** * 计算key值得hash值 */ int hash = hash(key); /** * 通过key值得hash值,计算出map中数组得索引值 */

How to sort HashMap based on property of key object when values of Hashmap are same?

廉价感情. 提交于 2019-12-11 18:25:33
问题 I have a HashMap with objects of a class (obj1, obj2, obj3) as keys and java.util.Date (date1, date2, date3)as values. The HashMap is already sorted based on the values, i.e., based on the date objects. The key objects are having property called name. obj1.name = "name1", obj2.name = "name2" etc. Now, When the values of HashMap are same, i.e., when there are same dates as values, I need to check the name of the key object (obj.name) and sort the HashMap based on name propery of key object.

HashMap源码分析

与世无争的帅哥 提交于 2019-12-11 18:24:10
HashMap HashMap采用key-value的存储结构,每个唯一key对应一个唯一的value,通常情况下HashMap的查询和修改时间复杂度为O(1),因为是散列存储,HashMap不能保证元素存储的顺序,且线程不安全。 public class HashMap < K , V > extends AbstractMap < K , V > implements Map < K , V > , Cloneable , Serializable { (1)继承了AbstractMap,实现了Map接口,具备Map的所有功能 (2)实现了Cloneable,可以被克隆 (3)实现了Serializable,可以被序列化 属性 /** * 默认初始容量为16 * 容量必须指定为2的n次方, 目的是为了使hash函数能够更加有效的获取散列值 * index = hashCode & (capacity - 1) */ static final int DEFAULT_INITIAL_CAPACITY = 1 << 4 ; // aka 16 /** * 最大容量 = 2^30 */ static final int MAXIMUM_CAPACITY = 1 << 30 ; /** * 默认负载因子 * 意味着当HashMap的容量被使用75%的时候会进行扩容 */ static