hashmap

Hashing Keys in Java

你说的曾经没有我的故事 提交于 2019-12-21 15:11:12
问题 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

Java面试题总结---01

北慕城南 提交于 2019-12-21 12:25:27
# 常见的Java问题 ## 什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? - Java虚拟机是一个可以执行Java字节码文件的虚拟机进程。Java源文件被编译成能够被Java虚拟机执行的字节码文件。 - Java是被设计为允许应用程序在任意平台都可以允许,不需要我们为每一个平台单独重写或者重新编译,Java虚拟机使其变成可能,因为它直到底层硬件平台的指令长度和其他特性。 ## JDK和JRE的区别是什么? - JRE是将要执行Java程序的Java虚拟机,它同时包含了执行applet需要的浏览器插件。 - JDK是完整的Java程序开发包,包含了JRE、编译器和其他工具。 所以,我们通常情况下,只需要下载JDK、配置好开发环境就可以了 ## "static"关键字是什么意思?Java能否覆盖(override)一个private或则static的方法 - “static”关键字表名一个成员变量或者一个成员方法可以在没有所属类的实例变量的情况下被访问,也就是我们不用使用new关键字构建一个对象,使用**类名.成员变量**或者**类名.成员方法**就可以。 - Java中的static方法不能被覆盖,因为方法覆盖是基于动态绑定的,而static方法使编译时静态绑定的。(换句话说,一个static方法,或者static属性

java基础之HashMap

梦想的初衷 提交于 2019-12-21 10:03:59
HashMap几乎是java中使用最多数据结构了,还有延伸的版本HashTable,和ConcurrentHashMap等常用的结构。 HashMap内部是如何实现的呢,让我们一起来看下: 基于哈希表的Map接口的非同步实现。 此实现允许使用null值和null键,HashMap实际上是一个“链表散列”的数据结构,即数组和链表的结合体。 当我们往HashMap中put元素的时候,先根据key的hashCode重新计算hash值,根据hash值得到这个元素在数组中的位置(即下标),如果数组该位置上已经存放有其他元素了,那么在这个位置上的元素将以链表的形式存放,新加入的放在链头,最先加入的放在链尾,如果数组该位置上没有元素,就直接将该元素放到此数组中的该位置上。 归纳起来简单地说,HashMap 在底层将 key-value 当成一个整体进行处理,这个整体就是一个 Entry 对象。HashMap 底层采用一个 Entry[] 数组来保存所有的 key-value 对,当需要存储一个 Entry 对象时,会根据hash算法来决定其在数组中的存储位置,在根据equals方法决定其在该数组位置上的链表中的存储位置;当需要取出一个Entry时,也会根据hash算法找到其在数组中的存储位置,再根据equals方法从该位置上的链表中取出该Entry。 那么HashMap什么时候进行扩容呢?

HashMap<Class<?>, List<Class<?>>> : specify that the lists' classes extend the keys'

牧云@^-^@ 提交于 2019-12-21 09:29:04
问题 One of my class need to store classes according to their superclasses. To that end, I'm using a HashMap, where keys are the superclasses, and values a list of their extended classes. So it looks like that : HashMap<Class<?>, List<Class<?>>> I'd like to know if there was a notation allowing me to be more precise, something like : HashMap<Class<T>, List<Class<? extends T>>> I've tried that and, of course, it doesn't work : T cannot be resolved. Is there a syntax that would allow me to do that ?

How to get rid of g++ hash_map deprecation warning?

不羁岁月 提交于 2019-12-21 09:19:22
问题 When I compile a c++ application I'm writing that makes use of hash_map, I get this warning on g++ 4.3.2: You are using the deprecated header . To eliminate this warning, use an ANSI-standard header file or use hte -Wno-deprecated compiler flag. 9> #include <ext/hash_map> What include replaces this? I've searched for a while on google, and can't find anything except for people having similar problems, but no solution. 回答1: My very first Google hit for "g++ hash_map deprecated" takes me to a

c++ unordered_map collision handling , resize and rehash

吃可爱长大的小学妹 提交于 2019-12-21 07:57:40
问题 I have not read the C++ standard but this is how I feel that the unordered_map of c++ suppose to work. Allocate a memory block in the heap. With every put request, hash the object and map it to a space in this memory During this process handle collision handling via chaining or open addressing.. I am quite surprised that I could not find much about how the memory is handled by unordered_map. Is there a specific initial size of memory which unordered_map allocates. What happens if lets say we

Using Java 7 HashMap in Java 8

为君一笑 提交于 2019-12-21 07:16:12
问题 I have updated a Java application to Java 8. The application heavily relies on HashMaps. When I run the benchmarks, I see unpredictable behavoir. For some inputs, the application runs faster than before, but for larger inputs, it's constantly slower. I've checked the profiler and the most time consuming operation is HashMap.get. I suspect the changes are due to the HashMap modification in Java 8, but it may not be true, as I have changed some other parts. Is there an easy way that I hook in

How to convert JSONObject to new Map for all its keys using iterator java

女生的网名这么多〃 提交于 2019-12-21 06:16:30
问题 I have a JSONObject {"2016":{"12":{"20":{"19":{"DonationTime":11111111111,"Donation":10}}}}} I want to convert it to new map with each keys int i = 0; for (Iterator<String> keysItr = object.keySet().iterator(); keysItr.`hasNext(); i++) { String key = keysItr.next(); Object value = object.get(key); if(value instanceof JSONObject) { value = toMap((JSONObject) value); map.put(key, value); } } SOP(map); //but here i want to get 4 maps } I want to get 4 maps like hourMap[19] = "{"DonationTime"

Iterating through a HashMap

时光怂恿深爱的人放手 提交于 2019-12-21 05:51:40
问题 Okay so i'm currently working on a searching method, the terms searched are ran through the database and the matching products are added to a hashMap with 2 Integer fields. then after the hashmap is made, the items are to be shown, however i'm having trouble getting the hashmap to print out the details here's my code public HashMap<Integer, Integer> bankSearch = new HashMap<Integer, Integer>(); and the use Iterator it = bankSearch.entrySet().iterator(); while (it.hasNext()) { HashMap.Entry

Issue with declaration of Map<String,Class<? extends Serializable>>

断了今生、忘了曾经 提交于 2019-12-21 05:05:04
问题 Java provides me by <? extends class> a way of filtering the java classes that you can use to build in this case the new HashMap, for example: I can do that: Map<String,? extends Serializable> map1 = new HashMap<String,String>(); It is correct, because String implements Serializable, so the compiler let me do that. But when i try to do it: Map<String,GenericClass<? extends Serializable>> map2 = new HashMap<String, GenericClass<String>>(); Being the GenericClass as it: public class