hashmap

Java: Composite key in hashmaps

本小妞迷上赌 提交于 2019-12-18 10:50:52
问题 I would like to store a group of objects in a hashmap , where the key shall be a composite of two string values. is there a way to achieve this? i can simply concatenate the two strings , but im sure there is a better way to do this. 回答1: You could have a custom object containing the two strings: class StringKey { private String str1; private String str2; } Problem is, you need to determine the equality test and the hash code for two such objects. Equality could be the match on both strings

why HashMap Values are not cast in List?

半腔热情 提交于 2019-12-18 10:17:58
问题 I'm putting values into the hashmap which is of the form, Map<Long, Double> highLowValueMap=new HashMap<Long, Double>(); highLowValueMap.put(1l, 10.0); highLowValueMap.put(2l, 20.0); I want to create a list by using values() method of map. List<Double> valuesToMatch=new ArrayList<>(); valuesToMatch=(List<Double>) highLowValueMap.values(); or List<Double> valuesToMatch=(List<Double>) highLowValueMap.values(); However, it throws an exception: Exception in thread "main" java.lang

Is it possible to rename a Hashmap key?

百般思念 提交于 2019-12-18 10:15:40
问题 I'm looking for a way to rename a Hashmap key, but i don't know if it's possible in Java. 回答1: Try to remove the element and put it again with the new name. Assuming the keys in your map are String , it could be achieved that way: Object obj = map.remove("oldKey"); map.put("newKey", obj); 回答2: Assign the value of the key, which need to be renamed, to an new key. And remove the old key. hashMap.put("New_Key", hashMap.get("Old_Key")); hashMap.remove("Old_Key"); 回答3: hashMap.put("New_Key",

hashmap实现及哈希冲突

寵の児 提交于 2019-12-18 10:02:05
原文: https://www.cnblogs.com/peizhe123/p/5790252.html HashMap 采用一种所谓的“Hash 算法”来决定每个元素的存储位置。当程序执行 map.put(String,Obect)方法 时,系统将调用String的 hashCode() 方法得到其 hashCode 值——每个 Java 对象都有 hashCode() 方法,都可通过该方法获得它的 hashCode 值。得到这个对象的 hashCode 值之后,系统会根据该 hashCode 值来决定该元素的存储位置。源码如下: Java代码 public V put(K key, V value) { if (key == null) return putForNullKey(value); int hash = hash(key.hashCode()); int i = indexFor(hash, table.length); for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; //判断当前确定的索引位置是否存在相同hashcode和相同key的元素,如果存在相同的hashcode和相同的key的元素,那么新值覆盖原来的旧值,并返回旧值。 //如果存在相同的hashcode

Java HashMap Hashing function [closed]

ぐ巨炮叔叔 提交于 2019-12-18 09:46:11
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I was going through Java's HashMap hash() implementation , its like below final int hash(Object k) { // some checks h ^= k.hashCode(); // This function ensures that hashCodes that differ only by // constant

Why define object for getEntry in HashMap

一笑奈何 提交于 2019-12-18 09:39:39
问题 I am new to generics and I am not sure if the answer to my question is opinion based or has a genuine reason. In the following code what was need to case a key of an entry to an object ? Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) It appears to be easily replaced by if (e.hash == hash && (e.key == key || (key != null && key.equals(e.key)))) More reference: final Entry<K,V> getEntry(Object key) { int hash = (key == null) ? 0 : hash(key); for (Entry<K

Map<String, HashSet<String>> to JSON, & Pretty Print

这一生的挚爱 提交于 2019-12-18 09:09:39
问题 I'm trying to make my dataset correspond to this example: var family = [{ "name" : "Jason", "age" : "24", "gender" : "male" }, { "name" : "Kyle", "age" : "21", "gender" : "male" }]; I have a Map<String, HashSet<String>> of Names and unique alpha-numeric values correponding to specific entities to which those names could refer, let's call these entry items "IDs". So for instance, Fyodor Mikhailovich Dostoyevsky would perhaps be related to the ID Q626 , because that's a very specific reference,

Multiple values for a key in HashMap in Java

自古美人都是妖i 提交于 2019-12-18 07:16:09
问题 Is it possible to keep multiple values corresponding to a key in a HashMap? If yes, how? 回答1: Yes, this is called chaining. You will want to avoid chaining as much as possible, especially if the size of the chain starts increasing. Longer chain size will counter the whole purpose of using a hash structure because the goal is to come as close to O(1) as possible. Map<String, List<String>> hm = new HashMap<String, List<String>>(); List<String> values = new ArrayList<String>(); values.add("Value

scala reflection

ⅰ亾dé卋堺 提交于 2019-12-18 06:11:10
问题 I want to create a hashmap to store parameters names and their values. The parameters however are with different types. I could use HashMap[String, Any], but I wouldn't know which types they are later on. Is there anyway I can recovery the type information? Or is there any better way to store pair? 回答1: Do you want access to the static type information, or the dynamic type information? If you're after the former, you can use typed keys. Something along these lines should work: final class Key

How to iterate through a Hashmap, print the key/value and remove the value in Rust?

冷暖自知 提交于 2019-12-18 05:35:39
问题 This should be a trivial task in any language. This isn't working in Rust. use std::collections::HashMap; fn do_it(map: &mut HashMap<String, String>) { for (key, value) in map { println!("{} / {}", key, value); map.remove(key); } } fn main() {} Here's the compiler error: error[E0382]: use of moved value: `*map` --> src/main.rs:6:9 | 4 | for (key, value) in map { | --- value moved here 5 | println!("{} / {}", key, value); 6 | map.remove(key); | ^^^ value used here after move | = note: move