hashmap

Initial capacity for a HashSet<Integer>

末鹿安然 提交于 2019-12-12 08:12:59
问题 What Initial Capacity should I use for a HashSet into which I know that I am going to insert 1000 integers to prevent the need for any internal rebuilds ? At first I though that I should use 1000 but reading the description of the constructor that taks the initialCapacity parameter it says Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75). . So If I set capacity to 1000 the hashMap will resize when reaching 750 elements

HashMap and visibility

Deadly 提交于 2019-12-12 07:44:43
问题 HashMap's javadoc states: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. I built a sample code that, based on the specification, is supposed to fail almost immediately and throw a ConcurrentModificationException; It does fail immediately as expected with Java 7 but it (seems to) always work with Java 6 (i.e. it does not throw the promised

Split string into key-value pairs

冷暖自知 提交于 2019-12-12 07:13:39
问题 I have a string like this: pet:cat::car:honda::location:Japan::food:sushi Now : indicates key-value pairs while :: separates the pairs. I want to add the key-value pairs to a map. I can achieve this using: Map<String, String> map = new HashMap<String, String>(); String test = "pet:cat::car:honda::location:Japan::food:sushi"; String[] test1 = test.split("::"); for (String s : test1) { String[] t = s.split(":"); map.put(t[0], t[1]); } for (String s : map.keySet()) { System.out.println(s + " is

Why ArrayList grows at a rate of 1.5, but for Hashmap it's 2?

半世苍凉 提交于 2019-12-12 07:10:55
问题 As per Sun Java Implementation, during expansion, ArrayList grows to 3/2 it's initial capacity whereas for HashMap the expansion rate is double. What is reason behind this? As per the implementation, for HashMap, the capacity should always be in the power of two. That may be a reason for HashMap's behavior. But in that case the question is, for HashMap why the capacity should always be in power of two? 回答1: The expensive part at increasing the capacity of an ArrayList is copying the content

Displaying object values which are stored in a HashMap

爷,独闯天下 提交于 2019-12-12 06:51:45
问题 I have a HashMap in which I have stored a string(key) and a Student object(value). I wish to iterate through the hash and display each key/value pair. However, I want to display the key followed by the String name value stored in the Student object. I have tried several ways and each one fails me in some way. I believe I do not entirely understand the "static" modifier, nor do I fully understand how to use HashMaps. This is my full program: import java.io.*; import java.util.Scanner; import

Android HashMap not persisting when returning to activity

烂漫一生 提交于 2019-12-12 06:50:11
问题 I am trying to keep my HashMap values when I navigate to another activity and return. This is the code I have for now. The HashMap works and is able to grab and save the data from the EditText in the view. However as soon as I leave from the activity and return, the HashMap is reinitialized to empty -> {} I have looked at documentation and it seems this is the correct way of ensuring that a variable data is persisted. However it does not work. please let me know what could be the issue:

Sort Hashmap by value and then alphabetically [duplicate]

有些话、适合烂在心里 提交于 2019-12-12 06:50:06
问题 This question already has answers here : Sorting Java objects using multiple keys (7 answers) Sort a Map<Key, Value> by values (54 answers) Closed 5 months ago . I am creating a word frequency program which works good with counting the words. I am using a hashmap to store the word frequencies. I use the word as the key and the count as the value. However, I want to sort the hashMap at the end first by the word frequency and then by key alphabetically like this: it - 2 of - 2 the - 2 times- 2

How to iterate through Nested Map and Multiset? - Java/Guava

醉酒当歌 提交于 2019-12-12 06:36:33
问题 How should I iterate through a Nested Map with such declaration? Map<String, Multiset<String>> Please suggest if there are other hashmap/list that are more effective way of doing this hash population task? import com.google.common.collect.Multiset; import com.google.common.collect.TreeMultiset; String[] foobarness = {"foo" , "bar", "ness", "foo", "bar", "foo", "ness", "bar", "foo", "ness", "foo", "bar", "foo", "ness", "bar", "ness", "foo", "bar", "foo", "ness"}; String[] types = {"type::1",

How do I read specific values from a txt file and put them into a HashMap?

≡放荡痞女 提交于 2019-12-12 06:15:09
问题 I have a text file that looks like this: a, 9, 1 b, 10, 5 c, 3, 4 etc... I am trying to create two hashmaps where the keys in both are the letters, the values for the first map are the first number, and values for the second map are the second number. For example, Map1 = [a, 9, b, 10, c, 3] and Map2 = [a, 1, b, 5, c, 4] I can't figure out how to access the specific character and matching integer. Here is what I have so far: public static void constructLetterMaps() throws FileNotFoundException

Help with Hashmaps in Java

谁都会走 提交于 2019-12-12 05:57:08
问题 I'm not sure how I use get() to get my information. Looking at my book, they pass the key to get(). I thought that get() returns the object associated with that key looking at the documentation. But I must be doing something wrong here.... Any thoughts? import java.util.*; public class OrganizeThis { /** Add a person to the organizer @param p A person object */ public void add(Person p) { staff.put(p, p.getEmail()); System.out.println("Person " + p + "added"); } /** * Find the person stored