hashmap

Why can I use Strings as keys in a HashMap?

折月煮酒 提交于 2019-12-12 10:58:06
问题 If two String s that are the same are not actually identical, then why can I use strings as keys in a HashMap without using the same String object? String s1 = "Test"; String s2 = "Test"; System.out.println(s1 == s2); // should be false System.out.println(s1.equals(s2)); // should be true HashMap<String, String> map = new HashMap(); map.put(s1, "foo"); System.out.println(map.get(s2)); // should be "foo"--but why? Does HashMap have some special behavior for String objects? If not, why can two

How to get a sorted result in iBatis?

三世轮回 提交于 2019-12-12 09:52:32
问题 I have a table mgr_employee with 2 columns managerName, teamEmployee. Although I do a sort in sql I get unsorted resultMap in java. How can I get a sorted map? Why does iBatis mix up the resultMap? <resultMap id="s_filter_defaults_ResultMap" class="java.util.HashMap"> <result property="key" column="managerName"/> <result property="value" column="count"/> </resultMap> <select id="mCount" parameterClass="java.util.HashMap" resultMap="mcount_ResultMap"> <![CDATA[ select managerName, count

VacantEntry does not implement any method in scope named set

对着背影说爱祢 提交于 2019-12-12 09:45:47
问题 This snippet of code: use std::collections::hash_map::Entry::{Occupied, Vacant}; use std::collections::HashMap; fn main() { let mut vars = HashMap::<i32, f64>::new(); let key = 10; let val = match vars.entry(key) { Vacant(entry) => entry.set(0.0), Occupied(entry) => entry.into_mut(), }; *val += 3.4; println!("{}", val); } Gives this error: error[E0599]: no method named `set` found for type `std::collections::hash_map::VacantEntry<'_, i32, f64>` in the current scope --> src/main.rs:8:32 | 8 |

How do I access the nth item in a dictionary or hash?

拈花ヽ惹草 提交于 2019-12-12 09:45:37
问题 I have a dictionary of keys and values, for example: { fred: 1, dave: 2, lily: 3 } How do I get the 2nd element in the dictionary - {dave:2} in this case? Background: I've seen this question asked so many times on SO in one form or another, so I thought I'd write a Q&A page as a community wiki that folks can be referred to and which might hopefully become the canonical answer for this question. This Q&A applies to dictionaries as they are implemented in many different languages. Different

What is the time complexity of java.util.HashMap class' keySet() method?

和自甴很熟 提交于 2019-12-12 09:44:21
问题 I am trying to implement a plane sweep algorithm and for this I need to know the time complexity of java.util.HashMap class' keySet() method. I suspect that it is O(n log n). Am I correct? Point of clarification: I am talking about the time complexity of the keySet() method; iterating through the returned Set will take obviously O(n) time. 回答1: Actually, getting the keyset is O(1) and cheap. This is because HashMap.keyset() returns the actual KeySet object associated with the HashMap. The

Java Weak Hash Map - Need to remove entry based on weakness of value, not key

主宰稳场 提交于 2019-12-12 09:37:42
问题 So the Java WeakHashMap lets one create a map whose entries are removed if its keys become weak. But how can I create a Map whose entries are removed when the values in the map become weak? The reason I want to use a map is as a Global Hash Table which keeps track of objects according to their ID's. ID ---> Object Address Key ---> Value (Where ID is a text string) I want key-value pairs to be removed when the object addresses become weak, not the Strings that point to them. Anyone any

read text file and store in hashmap. Then sort in order

好久不见. 提交于 2019-12-12 09:27:57
问题 File is like this: name1 134.2 name2 456.7 name3 265.3 ... ... I read the text file and store in HashMap after that I want to sort in order(by the highest value) but the problem is that because I sort the values in String, I cant compare it. So..is there a way to put the values of textfile into hashmap in double or integer form? import java.io.*; import java.util.*; class Test { public static void main(String[] args) throws FileNotFoundException { Scanner scanner = new Scanner(new FileReader(

Does a map using equals method for key checking exists?

允我心安 提交于 2019-12-12 09:13:21
问题 I want to store data in a map, with key unicity, but I would like the map to use the equals method of my key class. It seems that HashMap doesn't use the equals method (I may be wrong, if so my tests are wrong). My problem here is that the map use hashCode to check for duplicate, and I would like a map implementation that use equals. I am storing timestamp in the key, and would like to make it so that 2 keys are equals if there timestamp difference does not exceed a defined amount (let say

HashMap Java example to avoid collision

大兔子大兔子 提交于 2019-12-12 08:50:35
问题 I am using HashMap in java to store key and Object <Key,Object> . And I read about hashmap collision and I am trying to avoid it by using linked list. I did some search online, but I couldn't find an example how to do this. Can somebody point me to an online resources that implement the hashmap with linked list? 回答1: The Java HashMap already handles collisions for you in this way. All you need to do is ensure you are overriding and implementing the key's hashCode() and equals() method. Each

HashMap源码学习

…衆ロ難τιáo~ 提交于 2019-12-12 08:49:47
HashMap 简介 HashMap 主要用来存放键值对,它基于哈希表的Map接口实现,是常用的Java集合之一。 JDK1.8 之前 HashMap 由 数组+链表 组成的,数组是 HashMap 的主体,链表则是主要为了解决哈希冲突而存在的(“拉链法”解决冲突).JDK1.8 以后在解决哈希冲突时有了较大的变化,当链表长度大于阈值(默认为 8)时,将链表转化为红黑树(将链表转换成红黑树前会判断,如果当前数组的长度小于 64,那么会选择先进行数组扩容,而不是转换为红黑树),以减少搜索时间,具体可以参考 treeifyBin 方法。 底层数据结构分析 JDK1.8之前 JDK1.8 之前 HashMap 底层是 数组和链表 结合在一起使用也就是 链表散列 。 HashMap 通过 key 的 hashCode 经过扰动函数处理过后得到 hash 值,然后通过 (n - 1) & hash 判断当前元素存放的位置(这里的 n 指的是数组的长度),如果当前位置存在元素的话,就判断该元素与要存入的元素的 hash 值以及 key 是否相同,如果相同的话,直接覆盖,不相同就通过拉链法解决冲突。 所谓扰动函数指的就是 HashMap 的 hash 方法。使用 hash 方法也就是扰动函数是为了防止一些实现比较差的 hashCode() 方法 换句话说使用扰动函数之后可以减少碰撞 JDK 1