hashmap

Merge two Maps of Maps in Java

情到浓时终转凉″ 提交于 2019-12-12 05:48:22
问题 I have a HashMap which in turn has a HashMap within, Map<Long, Map<String, Double>> map = new HashMap<>(); I will have multiple inner maps for the same Long value, how do I make sure that the value field isn't replaced and rather merged (appended)? This question differs from How to putAll on Java hashMap contents of one to another, but not replace existing keys and values? as it is about a Map of Strings, I'm stuck with a map of maps For instance Map<Long, Map<String, Double>> map = new

Mutation of the keys in HashMap causes wrong results

一世执手 提交于 2019-12-12 05:39:01
问题 in my project I use HashMap in order to store some data, and I've recently discovered that when I mutate the keys of the HashMap, some unexpected wrong results may happen. For Example: HashMap<ArrayList,Integer> a = new HashMap<>(); ArrayList list1 = new ArrayList<>(); a.put(list1, 1); System.out.println(a.containsKey(new ArrayList<>())); // true list1.add(5); ArrayList list2 = new ArrayList<>(); list2.add(5); System.out.println(a.containsKey(list2)); // false Note that both a.keySet()

TIntObjectHashMap - get Key for given value

大城市里の小女人 提交于 2019-12-12 04:49:10
问题 How to get the key from Trove TIntObjectHashMap for a value that exists and been found in the map ?? if(map.containsValue(source)) { for (Entry<Integer, String> entry : map.entrySet()) { // entrySet() is not recognized by Trove? and i can not find any corresponding method ?? if (entry.getValue().equals(source)) { entry.getKey(); } } } 回答1: I would do something like this: TIntObjectMap<String> map = new TIntObjectHashMap<>(); map.put( 1, "a" ); map.put( 2, "b" ); AtomicInteger found = new

Java 7 vs Java 8 HashMap resizing

允我心安 提交于 2019-12-12 04:46:20
问题 In Java 7, HashMap resize() created a temporary newTable, transferred the contents into it and then assigned the temporary newTable to the original table. void resize(int newCapacity) { Entry[] oldTable = table; int oldCapacity = oldTable.length; if (oldCapacity == MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return; } Entry[] newTable = new Entry[newCapacity]; boolean oldAltHashing = useAltHashing; useAltHashing |= sun.misc.VM.isBooted() && (newCapacity >= Holder.ALTERNATIVE_HASHING

How to reference a hash of array of hashes in order to compare values

一世执手 提交于 2019-12-12 04:32:51
问题 I have the following data structure: my %hash = ( 'hsa_circ_0024017|chr11:93463035-93463135+|NM_033395|KIAA1731 FORWARD' => [ { 'energy' => '-4.3', 'spacer' => 'AGGCACC', 'end' => '97', 'start' => '81' } ], 'hsa_circ_0067224|chr3:128345575-128345675-|NM_002950|RPN1 FORWARD' => [ { 'energy' => '-4.4', 'spacer' => 'CAGT', 'end' => '17', 'start' => '6' }, { 'energy' => '-4.1', 'spacer' => 'GTT', 'end' => '51', 'start' => '26' }, { 'energy' => '-4.1', 'spacer' => 'TTG', 'end' => '53', 'start' =>

reversing keys/values - create new instance of HashMap

风流意气都作罢 提交于 2019-12-12 04:24:23
问题 I’ve got an existing instance of HashMap simply called sale (it is Map<String, Set<String>>) I use it to log customers and items history. Is there a way to create a new instance of HashMap, that effectively reverses this usage? i.e will show each item purchased as a unique key and the corresponding value as a String set of the customers that have purchased that product. I suspect there is a simple process using keySet() in some way to iterate over the sales map but I just can’t see how to do

How to efficiently group SQL query output using Java?

情到浓时终转凉″ 提交于 2019-12-12 04:23:31
问题 I am doing SQL query using core Java on three different tables with LEFT join and getting following output: pId pName sId sName gId gName 1 p1 11 s1 111 g1 1 p1 11 s1 112 g2 2 p2 12 s2 null null 3 p3 13 s3 113 g3 Now I want to group this as following: [{ "pId": 1, "pname": "p1", "sub": [{ "sId": 11, "sName": "s1", "grades": [{ "gId": 111, "gName": "g1" }, { "gId": 112, "gName": "g2" }] }] }, { "pId": 2, "pname": "p2", "sub": [{ "sId": 12, "sName": "s2", "grades": [] }] }, { "pId": 3, "pname":

Java sorting and HashMap

不想你离开。 提交于 2019-12-12 04:06:37
问题 I have a HashMap<String, Integer> How can I sort this data structure and keep the key-value mappings? I want to sort by VALUES not keys. Collection<Integer> counts = tableFrequency.values(); But then I lose the key mappings. Or is there a better associative data-structure that I could have used instead of the HashMap? 回答1: To sort a Map by its values, you could grab its entrySet and sort that with a custom Comparator. List<Entry<K,V>> sorted = new ArrayList<>(map.entrySet()); Collections.sort

searching for specific keys in multiple independent hashMap objects

人走茶凉 提交于 2019-12-12 04:06:16
问题 I have a slightly theoretical question that I want to clear up before I start programing my solution any further... Background. I need to perform comparisons between 2 (or possible more) MS Access files. Each file should contain data that is found in one of the others. Due to the 'limitation' I have encountered with JDBC and connecting to Access result sets (they are only forward scrollable!) I have created 'java objects' (classes) that models the structure of a result set. in essence I have

If I use HashMap<String, ArrayList<String>> in Java

旧城冷巷雨未停 提交于 2019-12-12 04:05:15
问题 I use HashMap<String, ArrayList<String>> in Java. When input value is comes, For example, input value is [1, "stack"] , [2, "over"] , [1, "flow"] ..... I want to enter value [1, ["stack", "flow"]] , [2, "over"] in HashMap. But key value is duplicate. So, HashMap was overwrite. So, What can I do? 回答1: Try a Guava Multimap: The traditional way to represent a graph in Java is Map<V, Set<V>> , which is awkward in a number of ways. Guava's Multimap framework makes it easy to handle a mapping from