hashmap

Iterview questions

馋奶兔 提交于 2019-12-25 19:50:55
HashMap的工作原理 HashMap的工作原理是近年来常见的Java面试题。几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道Hashtable和HashMap之间的区别,那么为何这道面试题如此特殊呢?是因为这道题考察的深度很深。这题经常出现在高级或中高级面试中。投资银行更喜欢问这个问题,甚至会要求你实现HashMap来考察你的编程能力。ConcurrentHashMap和其它同步集合的引入让这道题变得更加复杂。让我们开始探索的旅程吧! “你用过HashMap吗?” “什么是HashMap?你为什么用到它?” 几乎每个人都会回答“是的”,然后回答HashMap的一些特性,譬如HashMap可以接受null键值和值,而Hashtable则不能;HashMap是非synchronized;HashMap很快;以及HashMap储存的是键值对等等。这显示出你已经用过HashMap,而且对它相当的熟悉。但是面试官来个急转直下,从此刻开始问出一些刁钻的问题,关于HashMap的更多基础的细节。面试官可能会问出下面的问题: “你知道HashMap的工作原理吗?” “你知道HashMap的get()方法的工作原理吗?” 你也许会回答“我没有详查标准的Java API,你可以看看Java源代码或者Open JDK。”“我可以用Google找到答案。”

Why doesn't my hashmap work? My object has the property that hashCode() equality implies equals(…) equality [closed]

江枫思渺然 提交于 2019-12-25 18:52:10
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . Why doesn't my Java HashMap work? My object has the property that .equals equality implies hashCode equality. You can assume that I'm modifying a field of the object after I add the object to the HashMap . 回答1: You can assume that I'm modifying a field of the object after I add the object to the

How to retrieve data from DBHelper by HashMap in Multicolumn ListView

南笙酒味 提交于 2019-12-25 18:41:36
问题 I want to View my record in Multicolumn ListView From DBHelper Class by HashMap. that will show my data in multicolumn and also all rows are clickable. i have some difficulty. my code is here DBHelper.java public ArrayList<HashMap<String, String>> getdata(){ ArrayList<HashMap<String, String>> maplist = new ArrayList<HashMap<String,String>>(); SQLiteDatabase db = getReadableDatabase(); String query = "select * from tbexpenses"; Cursor c = db.rawQuery(query, null); if(c.moveToFirst()){ do{

Java HashMap File Input Version of Code

偶尔善良 提交于 2019-12-25 18:40:16
问题 I have here a piece of code. This is a snippet from my Dijkstra implementation. Here, we have 4 vertices instantiated and each vertex has specified edges and its distance from the vertex(node). This code is straightforward and beneficial if the data will directly be inputted in the main class. Vertex v0 = new Vertex("Redvile"); Vertex v1 = new Vertex("Blueville"); Vertex v2 = new Vertex("Greenville"); Vertex v3 = new Vertex("Orangeville"); Vertex v4 = new Vertex("Purpleville"); v0.adjacencies

Java: difference between “CustomClass1” and “CustomClass1.class”?

吃可爱长大的小学妹 提交于 2019-12-25 18:21:31
问题 This question is in continuation with Java: Using Classes as a value in hashmap. What is the difference between following two approaches?: 1) String name = ( ( CustomClass1 )obj1 ).getName(); and 2) String name = ( ( mapQuery2ResponseType.get("string1") )obj1 ).getName(); where, mapQuery2ResponseType.get("string1") return value of type Class<?> First approach works perfectly but in second approach it's giving an error saying Syntax error on token "obj1", delete this token . How can I modify

Getting error while removing specific entry from listview and hashmap

旧街凉风 提交于 2019-12-25 18:20:49
问题 I am getting the following error while trying to delete specific entry from listview and hashmap: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.get(ArrayList.java:411) at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:349) at android.widget.AdapterView.getItemAtPosition(AdapterView.java:790) at com.example.cmmalli.helloworld.MainActivity.onContextItemSelected(MainActivity.java:120) at android.app.Activity.onMenuItemSelected(Activity.java:3224) at android

Java集合List、Set、Map

╄→尐↘猪︶ㄣ 提交于 2019-12-25 17:57:39
集合是 java 基础中非常重要的一部分,同样也是 Java 面试中很重要的一个知识点。所以,给王小整理了这篇关于集合的文章。 1、接口继承关系以及实现 集合类存放于 Java.util 包中,主要有 3 种:set、list 和 map。 Collection:Collection 是集合 List、Set、Queue 的最基本的接口 Iterator:迭代器,可以通过迭代器遍历集合中的数据 Map:是映射表的基础接口 层次关系图: 2、List Java 的 List 是非常常用的数据类型。List 是有序的 Collection。Java List 一共三个实现类:分别是 ArrayList、Vector 和 LinkedList。 2.1、ArrayList ArrayList 是最常用的 List 实现类,内部是通过数组实现的,它允许对元素进行快速随机访问。数组的缺点是每个元素之间不能有间隔,当数组大小不满足时需要增加存储能力,就要将已经有数组的数据复制到新的存储空间中。当从 ArrayList 的中间位置插入或者删除元素时,需要对数组进行复制、移动、代价比较高。因此,它适合随机查找和遍历,不适合插入和删除。 2.2、Vector Vector 与 ArrayList 一样,也是通过数组实现的,不同的是它支持线程的同步,即某一时刻只有一个线程能够写 Vector

HashMap stored on disk is very slow to read back from disk

谁都会走 提交于 2019-12-25 16:08:19
问题 I have a HashMap that stores external uids and then it stores a different id ( internal for our app ) that has been set for the given uid. e.g: 123.345.432=00001 123.354.433=00002 The map is checked by uid to make sure the same internal id will be used. If something is resent to the application. DICOMUID2StudyIdentiferMap defined as follows: private static Map DICOMUID2StudyIdentiferMap = Collections.synchronizedMap(new HashMap()); The load however will overwrite it, if we successfully load,

HashMap的底层原理

南楼画角 提交于 2019-12-25 16:04:38
HashMap的底层原理 HashMap底层是数组+链表或者是数组+红黑树,当我们向HashMap中put元素的时候,先根据key的hashcode重新计算hash值,根据hash值得到这个元素在数组中的位置(即下标),如果数组在该位置上已经存放有其他元素了,那么在这个位置上的元素将以链表的形式存放,新加入的放在链头,最先加入的放在尾,如果链表的长度超过8,则将链表转换成红黑树。如果数组该位置上没有元素,就直接将元素放到此数组中的该位置上。当系统决定存储HashMap中的key-value对时,完全没有考虑Entry中的value,仅仅只是根据key来计算并决定每个Entry的存储位置。我们完全可以把Map集合中的value当成key的附属,当系统决定了key的存储位置之后,value随之保存在那里即可。当我们get元素的时候,同样根据key的hashcode计算的hash值得到元素的存储位置,然后调用key的equals方法与该位置的元素进行比较,如果为true则直接返回,否则如果此位置是一个链表类型,则从链表中查找元素返回,否则如果此位置是一个树节点,则从二叉树中查找元素返回,如果都不满足,则返回null。 来源: CSDN 作者: Betty_betty_betty 链接: https://blog.csdn.net/Betty_betty_betty/article

get Hashmap from Method

北战南征 提交于 2019-12-25 14:26:44
问题 I'm new to Java and haven't yet learned how to create 2 separate classes and combine them and so, I end up jumbling everything in the main and you can imagine how the code end up looking not understandable, which I plan to learn later on in my course. Yet I need a solution to work with 'Methods' so my code can look cleaner and if I need to add to it or repair it wouldn't be much of a hassle. So basically my question is whether I can use Hashmap.get from the main to get information from a