hashmap

HashMap<String, Integer> Search for part of an key? [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-18 12:52:43
问题 This question already has answers here : Partial search in HashMap (5 answers) Closed 3 years ago . I am currently using HashMap<String, Integer> which is filled with keys of type String which are all, let's say, 5 chars long. How can I search for an specific key of 4 chars or less, which is part and at the beginning of some other keys and get all hits as a collection of <Key, Value> ? 回答1: Iterate is your only option unless you create a custom data structure: for (Entry<String, Integer> e :

Equality between 2 HashMap

守給你的承諾、 提交于 2019-12-18 12:48:14
问题 In the equals() method of my class, I am using a private instance HashMap variable to compare for the equality. However, 2 different objects still show being equal when comparing their HashMap variables. Further research brought me to the link : Link Here . However, it just says that the reason for HashMap1.equals(HashMap2) not working is because " apparantly Java's arrays cannot be tested for equality without writing a customized code." I did not understand this reason. Can anyone please

What happens if two different objects have the same hashcode?

冷暖自知 提交于 2019-12-18 11:47:42
问题 It is my understanding that two unequal objects can have the same hashcode. How would this be handled when adding or retrieving from a HashMap java? 回答1: They will just be added to the same bucket and equals() will be used to distinguish them. Each bucket can contain a list of objects with the same hash code. In theory you can return the same integer as a hash code for any object of given class, but that would mean that you loose all performance benefits of the hash map and, in effect, will

Java sort HashMap by value [duplicate]

对着背影说爱祢 提交于 2019-12-18 11:47:29
问题 This question already has answers here : Sort a Map<Key, Value> by values (54 answers) Closed 6 years ago . I have this HashMap: HashMap<String, Integer> m which basically stores any word (String) and its frequency (integer). The following code is ordering the HashMap by value: public static Map<String, Integer> sortByValue(Map<String, Integer> map) { List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(map.entrySet()); Collections.sort(list, new Comparator<Map

Java sort HashMap by value [duplicate]

末鹿安然 提交于 2019-12-18 11:47:03
问题 This question already has answers here : Sort a Map<Key, Value> by values (54 answers) Closed 6 years ago . I have this HashMap: HashMap<String, Integer> m which basically stores any word (String) and its frequency (integer). The following code is ordering the HashMap by value: public static Map<String, Integer> sortByValue(Map<String, Integer> map) { List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(map.entrySet()); Collections.sort(list, new Comparator<Map

java hashmap key iteration

梦想与她 提交于 2019-12-18 11:46:34
问题 Is there any way to iterate through a java Hashmap and print out all the values for every key that is a part of the Hashmap? 回答1: With for-each loop , use Map.keySet() for iterating keys, Map.values() for iterating values and Map.entrySet() for iterating key/value pairs. Note that all these are direct views to the map that was used to acquire them so any modification you make to any of the three or the map itself will reflect to all the others too. 回答2: Yes, you do this by getting the

Sorted hash table (map, dictionary) data structure design

强颜欢笑 提交于 2019-12-18 11:42:38
问题 Here's a description of the data structure: It operates like a regular map with get , put , and remove methods, but has a sort method that can be called to sorts the map. However, the map remembers its sorted structure, so subsequent calls to sort can be much quicker (if the structure doesn't change too much between calls to sort ). For example: I call the put method 1,000,000 times. I call the sort method. I call the put method 100 more times. I call the sort method. The second time I call

How to swap keys and values in a Map elegantly

无人久伴 提交于 2019-12-18 11:26:23
问题 I already know how to do it the hard way and got it working - iterating over entries and swapping "manually". But i wonder if, like so many tasks, this one can be solved in a more elegant way. I have read this post, unfortunately it does not feature elegant solutions. I also have no possibility to use any fancy Guava BiMaps or anything outside the jdk (project stack is already defined). I can assume that my map is bijective, btw :) 回答1: The standard API / Java runtime doesn't offer a bi

Best HashMap initial capacity while indexing a List

无人久伴 提交于 2019-12-18 11:03:27
问题 I have a list ( List<T> list ) and I want to index its objects by their ids using a map ( HashMap<Integer, T> map ). I always use list.size() as the initial capacity in the HashMap constructor,like in the code below. Is this the best initial capacity to be used in this case? Note : I'll never add more items to the map. List<T> list = myList; Map<Integer, T> map = new HashMap<Integer, T>(list.size()); for(T item : list) { map.put(item.getId(), item); } 回答1: If you wish to avoid rehashing the

More concise HashMap initialization

荒凉一梦 提交于 2019-12-18 11:02:49
问题 I'm using a HashMap to count the occurrences of different characters in a string: let text = "GATTACA"; let mut counts: HashMap<char, i32> = HashMap::new(); counts.insert('A', 0); counts.insert('C', 0); counts.insert('G', 0); counts.insert('T', 0); for c in text.chars() { match counts.get_mut(&c) { Some(x) => *x += 1, None => (), } } Is there a more concise or declarative way to initialize a HashMap ? For example in Python I would do: counts = { 'A': 0, 'C': 0, 'G': 0, 'T': 0 } or counts = {