天道酬勤,学无止境

hashmap

HashMap 实现中的泛型(Generics in HashMap implementation)

问题 在Java实现中,我发现 transient Entry[] table; which is initiated in constructor as table = new Entry[capacity]; 我知道并理解不允许创建通用数组,但是我不明白的是整个事情是如何工作的。 我的意思是当我们做类似的事情时 HashMap<Integer, String> hMap = new HashMap<Integer, String>(); 上面的代码如何导致创建<Integer, String>类型的 Entry 数组 好吧,很少有人无法理解我在问什么。 重新表述我要问的是做类似的事情有什么意义 HashMap<Integer, String> hMap = new HashMap<Integer, String>(); 当它没有导致 Entry<Integer, String> 回答1 该实现创建了一个Entry<K,V>类型的对象数组 static class Entry<K,V> implements Map.Entry<K,V> 不提供泛型类型参数(来源)。 这是允许的,但需要理解编译器不再保证类型安全。 例如,在代码的其他地方你可以写 Entry<K,V> e = table[bucketIndex]; 编译器会让你这样做。 如果您确定始终将table[

2021-06-21 12:11:53    分类:技术分享    java   generics   hashmap

HashMap De-Serialization

I have a server/client app where I retrieve data from the server via Hessian/hessdroid. The data is very complex with HashMaps containing other HashMaps and images stored in byte arrays. I can display the data perfectly. To not always query the server, I´m using a data structure as a cache. This data object I save to SD card using ObjectOutputStream when closing the app. When I restart it, I read it back to memory with an ObjectInputStream. I´m having problems with the app only after reading the data from SD card. LogCat gives me the following output (100 times): DEBUG/dalvikvm(4150)

2021-06-21 12:10:27    分类:问答    android   serialization   hashmap

在嵌套映射中查找特定键的值(Find Value of Specific Key in Nested Map)

问题 在 Clojure 中,如何找到可能位于嵌套映射结构深处的键的值? 例如: (def m {:a {:b "b" :c "c" :d {:e "e" :f "f"}}}) (find-nested m :f) => "f" 回答1 Clojure 提供 tree-seq 来对任何值进行深度优先遍历。 这将简化查找嵌套键所需的逻辑: (defn find-nested [m k] (->> (tree-seq map? vals m) (filter map?) (some k))) (find-nested {:a {:b {:c 1}, :d 2}} :c) ;; => 1 此外,找到所有匹配项变成了用keep替换some的问题: (defn find-all-nested [m k] (->> (tree-seq map? vals m) (filter map?) (keep k))) (find-all-nested {:a {:b {:c 1}, :c 2}} :c) ;; => [2 1] 请注意,具有nil值的映射可能需要一些特殊处理。 更新:如果您查看上面的代码,您会发现k实际上可以是一个提供更多可能性的函数: 查找字符串键: (find-nested m #(get % "k")) 查找多个键: (find-nested m #(some % [:a :b]

2021-06-21 12:03:07    分类:技术分享    clojure   hashmap

Java 流 - 将列表排序为列表的 HashMap(Java stream - Sort a List to a HashMap of Lists)

问题 假设我有一个Dog课程。 在它里面我有一个Map<String,String> ,其中一个值是Breed 。 public class Dog { String id; ... public Map<String,String> } 我想获得List的Map : HashMap<String, List<Dog>> // breed to a List<Dog> 我更喜欢使用Stream而不是迭代它。 我该怎么做? 回答1 您可以使用groupingBy来做到这一点。 假设您的输入是List<Dog> ,则Dog类中的Map成员称为map ,并且 Breed 存储为“Breed”键: List<Dog> dogs = ... Map<String, List<Dog>> map = dogs.stream() .collect(Collectors.groupingBy(d -> d.map.get("Breed"))); 回答2 上面的好答案可以通过方法参考符号进一步改进: List<Dog> dogs = ... Map<String, List<Dog>> map = dogs.stream() .collect(Collectors.groupingBy(Dog::getBreed)); 回答3 List<Map<String , String>> as = new

2021-06-21 11:56:05    分类:技术分享    java   java-8   hashmap   java-stream

Multimap Java的Multimap - 分类时间表(Multimap of Multimap Java - Categorize timetable)

问题 我已经解析了一个 HTML 时间表并将每个主题加载到我的类对象中。 所以我有我的主题的arrayList,其中包含姓名、老师、...、小时和天的信息,现在我想重建表格,所以我需要先对它进行分类。 我认为最好的结构是这样的: Monday: 1: Math, Czech, ... 2: History ... Tuesday: 1: English, Geo 2... ... 在给定的时间内可以有多个主题,因此我尝试使用 Multimap 的 Multimap,但我无法在解析过程中声明它。 Multimap<String, Multimap<String, Subject>> timetable = HashMultimap.create(); ... for ... timetable.put(subject.den, new HashMultimap<>(subject.hod, subject)); 但它说 HashMultimap 有私人访问 com.google.common ......我不知道如何正确地写这个。 我也在考虑使用 Array,但我必须预先声明它,我想在一个循环中构建它。 有任何想法吗? 先感谢您 回答1 看起来你想要的实际上更多的是Map<String, Multimap<String, Subject>> ,在这种情况下你想要 Map<String

2021-06-21 10:24:58    分类:技术分享    java   object   arraylist   hashmap   multimap

Java key - key map

I need a kind of map which is accessible in two directions, so with a key-key structure instead of key-value. Does this exist in Java? If not, what is the best way to create it? So example: mySpecialHashMap.put("key1", "key2"); mySpecialMap.getL2R("key1") returns "key2"; mySpecialMap.getR2L("key2") returns "key1";

2021-06-15 12:34:24    分类:问答    java   map   hashmap   bidirectional

Thread-safe map with null-key capability

I need a multi-threaded Map object to use in my web server's caching, and I need to have null keys. HashMap allows me to have null keys, but ConcurrentHashMap doesn't. I tried to create a synchronized version of HashMap using Collections.synchronizedMap(new HashMap()) but it doesn't accept null keys either. Is there any alternative that I can use, without having to implement some way to wrap the null keys?

2021-06-15 12:24:45    分类:问答    java   synchronization   hashmap   concurrenthashmap

Remove a key in hashmap when the value's hashset is Empty

I have a hashmap that maps strings keys to hashsets values, and I want to remove a key from the hashmap when the hashmaps's hashset value is empty. I'm having trouble approaching this. Here's what I've tried but I'm very stuck: for(Map.Entry<String, HashSet<Integer>> entr : stringIDMap.entrySet()) { String key = entr.getKey(); if (stringIDMap.get(key).isEmpty()) { stringIDMap.remove(key); continue; } //few print statements... }

2021-06-15 12:19:36    分类:问答    java   hashmap   set   hashset

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

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.

2021-06-15 10:33:25    分类:问答    java   hashmap   guava

Scala HashMap of Lists: simpler default?

I need a HashMap of Lists. Normally I do this: val lists = mutable.HashMap[String,List[Int]]() { override def default(key: String) = { val newList = List[Int]() this(key) = newList newList } } so that I can then simply write things like lists("dog") ::= 14 without having to worry about whether the "dog" List has been initialised yet. Is there a cleaner way to do this? I find myself typing out those five default override lines again and again. Thanks!

2021-06-15 04:07:16    分类:问答    list   scala   hashmap   overriding   default