hashmap

Is the order of HashMap elements reproducible?

可紊 提交于 2019-12-22 04:16:07
问题 First of all, I want to make it clear that I would never use a HashMap to do things that require some kind of order in the data structure and that this question is motivated by my curiosity about the inner details of Java HashMap implementation. You can read in the java documentation on Object about the Object method hashCode . I understand from there that hashCode implementation for classes such as String and basic types wrappers ( Integer , Long ,...) is predictable once the value contained

How can I make a Map with two indexes?

浪尽此生 提交于 2019-12-22 03:22:47
问题 I have one Map in java like this: Map<String index1, Map<String index 2, Object obj>> map = new HashMap<>(); I want to get my Object in the map by using index1 and index2 as lookups. 回答1: The easiest way to do this would be to use Guava's Table, if you're willing to use a third party library. It works like this: Table<String, String, Object> table = HashBasedTable.create(); table.put(index1, index2, obj); Object retrievedObject = table.get(index1, index2); You can add it to your project by

longest-substring-with-at-least-k-repeating-characters

前提是你 提交于 2019-12-22 03:15:11
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/ public class Solution { public int longestSubstring(String s, int k) { // Find count of each char HashMap mp = new HashMap(); Object intObj; for (int i=0; i<s.length(); i++) { char ch = s.charAt(i); intObj = mp.remove(ch); int st = 0; if (intObj != null) { st = (int) intObj; } st++; mp.put(ch, st); } // prepre iterate secondly int ret = 0; int last = -1; HashMap newMp = new HashMap(); // iterate secondly for (int i=0; i<s.length(); i++) { char ch = s.charAt(i); int num = (int)mp.get(ch); // pass if num fits

Java memory management with HashMap

只谈情不闲聊 提交于 2019-12-21 21:18:08
问题 I am working on an application which reads in a huge amount of data from a database into a Map<String,Map<String,Map<String,String>>> , processes it, and writes the processed reports to a spreadsheet using an in-house xml writer. The whole run can take about 12 hours. I'm finding I'm getting Exception in thread "CursorController-Thread-0" java.lang.OutOfMemoryError: Java heap space at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:45) at java.lang.StringBuilder.<init>

史上最详细的HashTable源码解析,最容易懂

柔情痞子 提交于 2019-12-21 21:14:13
HashTable源码分析 更多资源和教程请关注公众号: 非科班的科班 。 如果觉得我写的还可以请给个赞,谢谢大家,你的鼓励是我创作的动力 ###1.前言 Hashtable 一个元老级的集合类,早在 JDK 1.0 就诞生了 ###1.1.摘要 在集合系列的第一章,咱们了解到,Map 的实现类有 HashMap、LinkedHashMap、TreeMap、IdentityHashMap、WeakHashMap、HashTable、Properties 等等。 ###1.2.简介 Hashtable 一个元老级的集合类,早在 JDK 1.0 就诞生了,而 HashMap 诞生于 JDK 1.2,在实现上,HashMap 吸收了很多 Hashtable 的思想,虽然二者的底层数据结构都是 数组 + 链表 结构,具有查询、插入、删除快的特点,但是二者又有很多的不同。 打开 Hashtable 的源码可以看到,Hashtable 继承自 Dictionary,而 HashMap 继承自 AbstractMap。 public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, java.io.Serializable { ..... } HashMap 继承自 AbstractMap

How can I implement a fixed size hashmap?

余生长醉 提交于 2019-12-21 21:07:28
问题 I want to implement a hashmap, but I am not allowed to let it expand. Since I do know that I need to store at most N elements, I could pre-allocate an array with N elements for each bucket of my hashtable, so that I can still store N elements in the worst case where all keys are hashed on the same bucket. But the elements that I need to store are rather big, so for large N this is very inefficient use of memory. Is it possible to implement a hashmap efficiently (in terms of memory) with a

Best way to conditionally add to a HashMap, with as few lookups as possible?

房东的猫 提交于 2019-12-21 20:49:32
问题 When using hash maps, it is common to want to add a key:value pair if the key is not already present. This reads well but isn't as optimal as it could be. if !map.contains(key) { let val = create_val(); map.insert(key, val); some_creation_logic(val); } else { let val = map[key]; some_update_logic(val); } While this works it will always do 2 lookups. The closest I could get was to use Entry.or_insert (or or_insert_with ), counting the length so the else branch can be taken. let map_len_prev =

Reducing Map by using Java 8 Stream API

旧城冷巷雨未停 提交于 2019-12-21 20:36:37
问题 I have a Map in the following form: Map<Integer, Map<String,Double>> START Let INNER be the inner map, i.e. Map<String,Double> For example, I'd like to reduce START map in a new one Map<Integer, Double> END which have the same keys, but different values. In particular, for each key, I want the new Double value be the SUM of values in the INNER map for the corresponding key. How could I achieve this by using JAVA 8's STREAM API? Thanks everyone. EDIT: A sample map is --------------------------

Converting string array to hashmap [duplicate]

守給你的承諾、 提交于 2019-12-21 17:16:44
问题 This question already has answers here : Converting string arrays into Map (5 answers) Closed 3 years ago . I have the following response T2269|175@@2a1d2d89aa96ddd6|45464047 By using the split("\\|") i have converted into string array object. The meaning for the each field is as follows: T2269 id 175@@2a1d2d89aa96ddd6 cid 45464047 refno No i have to convert it into HashMap object . Is their any solution for the above.. The above response is given for example. In real, the length of the

Hashing Keys in Java

折月煮酒 提交于 2019-12-21 15:11:13
问题 In java, when I use a String as a key for Hashmap I get a little different result than when I use the string hashcode as a key in the HashMap. Any insight? 回答1: when I use the string hashcode as a key in the HashMap. You mustn't use the hash code itself as the key. Hash codes aren't intended to be unique - it's entirely permitted for two non-equal values to have the same hash code. You should use the string itself as a key. The map will then compare hash codes first (to narrow down the