hashmap

Add subclasses to ConcurrentHashMap of super type in a static initializer?

此生再无相见时 提交于 2019-12-13 18:29:34
问题 public class People { class Family extends People { } } public class Together { private static Collection<Family> familyList = new ArrayList<Family>(); private static ConcurrentMap<String, Collection<People>> registry = new ConcurrentHashMap<String, Collection<People>>(); static { registry.put(Family.class.toString(), familyList); } } Error message: The method put(String, Collection<people>) in the type Map<String,Collection<people>> is not applicable for the arguments (String, Collection

Hashmap overwriting values

馋奶兔 提交于 2019-12-13 18:09:12
问题 Hey im trying to make a hashMap store for planes. But when i add, it will only print out one flight. Can anyone help me with this. Here is my code: import java.util.Scanner; public class MainApp { private Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { new MainApp().start(); } public void start() { Airline aerLingus = new Airline("AerLingus"); PlaneStore planeStore = new PlaneStore("Aer Lingus"); Flight p1 = new Flight("Aer Lingus","A01", 150.5, 10.5, 500,

Remove Duplicates from List of HashMap Entries

房东的猫 提交于 2019-12-13 16:50:04
问题 I have a List<HashMap<String,Object>> which represents a database where each list record is a database row. I have 10 columns in my database. There are several rows where the values of 2 particular columns are equals. I need to remove the duplicates from the list after the list is updated with all the rows from database. What is the efficient way? FYI - I am not able to do distinct while querying the database, because the GroupName is added at a later stage to the Map after the database is

HashMap having null as key

大城市里の小女人 提交于 2019-12-13 15:04:38
问题 How HashMap internally differentiate between null and 0 as key. As per this post the hash code for null key is 0 , Is it correct? If yes then both should go in same bucket at index 0 and there should be one value in the HashMap but this is not how HashMap works. Can somebody explain me? what is the hash code for null key in HashMap ? Sample code: HashMap<Integer,String> map=new HashMap<Integer,String>(); map.put(null, "abc"); map.put(0, "xyz"); // will it override the value for null as key?

compare python dictionary values of type list to see if they match in that order

半城伤御伤魂 提交于 2019-12-13 14:25:01
问题 prefs = { 's1': ["a", "b", "c", "d", "e"], 's2': ["c", "d", "e", "a", "b"], 's3': ["a", "b", "c", "d", "e"], 's4': ["c", "d", "e", "b", "e"] } I have a dictionary, and I want to compare the values (Type: List) for each key to see if they exist in that order. So essentially I'm trying to iterate over each key-value pair and compare the value which is of type list to the next value to see if the elements in that list match in that specific order. If we find a match, I want to return a list of

sparse_hash_map is very slow for specific data

谁都会走 提交于 2019-12-13 14:19:37
问题 tl;dr: why does key lookup in sparse_hash_map become about 50x slower for specific data? I am testing the speed of key lookups for sparse_hash_map from Google's sparsehash library using a very simple Cython wrapper I've written. The hashtable contains uint32_t keys and uint16_t values. For random keys, values and queries I am getting more than 1M lookups/sec. However, for the specific data I need the performance barely exceeds 20k lookups/sec. The wrapper is here. The table which runs slowly

HashMap<String, boolean> copy all the keys into HashMap<String, Integer>and initialize values to zero

寵の児 提交于 2019-12-13 12:27:40
问题 What is the best way ? Just looping through and putting the key and zero, or is there another more elegant or existing library method. I am also using Google's guava java library if that has any useful functionality ? Wanted to check if there was anything similar to the copy method for lists, or Map's putAll method, but just for keys. 回答1: Don't think there's much need for anything fancy here: Map<String, Boolean> map = ...; Map<String, Integer> newMap = Maps.newHashMapWithExpectedSize(map

significance of “power of 2” in java.util.HashMap implementation [duplicate]

廉价感情. 提交于 2019-12-13 11:42:36
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Java HashMap Default Initial Capacity I was reading the implementation of HashMap in java.util.HashMap. The initial capacity,maximum capacity etc., are powers of two. Parts of declaration copied from java.util.HashMap /** * The default initial capacity - MUST be a power of two. */ static final int DEFAULT_INITIAL_CAPACITY = 16; /** * The maximum capacity, used if a higher value is implicitly specified * by

Java8 toMap exception message confusing [duplicate]

孤者浪人 提交于 2019-12-13 11:19:28
问题 This question already has answers here : Why does Collectors.toMap report value instead of key on Duplicate Key error? (4 answers) Java 8 toMap IllegalStateException Duplicate Key (2 answers) Ignore duplicates when producing map using streams (6 answers) Alternative for throwingMerger in Java 8 (1 answer) Collectors.toMap with same keys(print same key) (2 answers) Closed last year . Got this exception: java.lang.IllegalStateException: Duplicate key 50 I looked over every map that is using

How to set and get using model in Hashmap?

纵然是瞬间 提交于 2019-12-13 10:10:39
问题 Model private HashMap<String, Object> data; public Mode() { this.data = new HashMap<String, Object>(); } public Object get(Mode value) { return this.data.get(value); } public void set(String key, Object value) { this.data.put(key, value); } } How do I set and get the value using this model? 回答1: Try this public Object get(String key) { return this.data.get(key); } public void set(String key, Object value) { this.data.put(key, value); } 来源: https://stackoverflow.com/questions/43753983/how-to