hashmap

Java 8 Hash Map not working properly

独自空忆成欢 提交于 2019-12-10 16:05:20
问题 We're facing weird issues with how HashMap behaves since java 8. When HashMap keys implement Comparable interface but compareTo implementation is inconsistent with equals then HashMaps: grow much larger then they are supposed to grow they contain several instances of equal elements values attached to those elements might differ get(key) result depends on which key is used (even if the keys are equal according to equals method). I've created a small test to reproduce the problem (see below).

chat application in android so that sender and receiver message should be on different side

孤者浪人 提交于 2019-12-10 15:58:45
问题 protected void onPostExecute( ArrayList<HashMap<String,String>> myArrayList)// for arraylist(ArrayList<String> result) { for (HashMap<String, String> data : myArrayList) { String sender_no = data.get(TAG_SENDER_NO); String msg1=data.get(TAG_SEN_MSG); String receiver_no=data.get(TAG_RECEIVER_NO); if(sender_no.equals(senderno)) { ListAdapter adapter = new SimpleAdapter(SinglechatActivity.this, myArrayList,R.layout.list_row_layout_even, new String[] { TAG_SEN_MSG },new int[] { R.id.message_me })

Calculating HashMap overhead in Java

隐身守侯 提交于 2019-12-10 15:49:33
问题 Let's say I'm storing 1000 objects in a hashmap. This hashmap is extended to allow me to map three dimensional coordinates to the objects stored in it; the objects inside have a fixed size. The hash key is a long integer. How would I go about figuring out (mathematically) the probable overhead for this structure? Is it significant enough that, for instance, if the data inside is around 256mb that the overhead will matter? Is there a reliable way (Aside from a profiler, which I've found are

Java集合容器系列08-HashSet

人盡茶涼 提交于 2019-12-10 15:43:44
一、HashSet的介绍 HashSet是一个依赖于HashMap的Set接口实现,容器的元素存储和操作都是基于内部的一个HashMap实例实现,因为这个原因,它不保证Set中元素的迭代顺序特别是不保证该顺序的恒久不变,允许插入null元素。该类可以为基本的集合操作提供稳定的性能保证,这些基本操作包括add、remove、contains和size,假定哈希函数正确地将元素分布在底层HashMap的槽中,那么对此HashSet进行迭代所需的时间与元素的个数和底层HashMap的槽的个数成正比的,所以迭代性能很重要的话,就不要将初始容量设置得太高(或者负载因子设置得太低)。注意HashSet不是线程安全的容器,如果有多个线程访问该容器,且至少有一个线程对容器做了结构性修改,那么它就必须在外部保证同步,这通常是通过对操作该容器的代码块加锁实现的,如果没有则可以使用Collections.synchronizedSet在包装它作为一个线程安全的容器使用。HashSet的iterator返回的迭代器对象Iterator是fail-fast(快速失败)的,如何理解,即在该迭代器创建之后任何时间对该容器做了结构性修改(除了基于iterator.remve方法删除容器元素之外)都将导致迭代器遍历时抛出ConcurrentModificationException异常,这种快速失败行为无法绝对保证

阿里面经汇总

天涯浪子 提交于 2019-12-10 15:29:32
网友 1 1.自我介绍 2.项目中的问题 3.Java类加载器有哪些 4.Java反射机制及应用 5.Synchronized底层实现,4种锁 6.Full GC,Minor GC 7.redis主从复制 8.thrift RPC原理 9.cookie session区别 10.如果登陆了如何其他服务器知道 11.SSO原理,应用场景 12.有什么问题问我吗? 网友 2 阿里一面(新零售)一小时左右 1.自我介绍 2.学到数据结构,常用的有哪些, 3.数组和链表的差别,进行频繁插入和删除用哪个 4.如何进行查找,二分查找,还有其他的吗(hash,b树,当时没想起来,之后又补充的) 5.树有哪些树,说下B树的实现原理(如何分裂节点 7.栈和堆的区别 8.给了千万级数据,怎么进行查找(答:hash,多级hash,类似java的HashMap 9.说到HashMap,说下在java中的实现原理,线程安全吗,线程安全的有什么(hashtable,concurrenthashmap) 10.list都有哪些,ArrayList和LinkedList的区别 11.java的这些内存都是怎么分配的,jvm模型 12.类加载,双亲委托加载 13.java的 == 和equals方法的区别。 14.用的java版本1.8,和1.7有什么区别(不知道。。说了concurrenthashmap的区别

HashMap底层实现(源码分析)

谁说我不能喝 提交于 2019-12-10 15:26:07
一、数据结构 Map将实际数据存储在Entry类的数组中。 代码片段: Java代码 transient Entry[] table; //HashMap的成员变量,存放数据 static class Entry<K,V> implements Map.Entry<K,V> { //内部类Entry final K key; V value; Entry<K,V> next; //指向下一个数据 final int hash; /** * Creates new entry. */ Entry( int h, K k, V v, Entry<K,V> n) { value = v; next = n; key = k; hash = h; } 执行下面代码后,可能的存储内部结构是: Map map = new HashMap(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); 执行put方法时根据key的hash值来计算放到table数组的下标,如果hash到相同的下标,则新put进去的元素放到Entry链的头部,如上图所示。put方法的源码后面详细解释。 二、属性和构造方法 Java代码 static final int DEFAULT_INITIAL_CAPACITY

storing charcter and binary number in a hash map

假装没事ソ 提交于 2019-12-10 15:13:48
问题 I am trying to store a mapping of letters to a Binary number. Here is my Mapping ("h",001) ("i", 010) ("k",011) ("l",100) ("r", 101) ("s",110) ("t",111) For this purpose, I have created a hash map and stored the key value pairs. I now want to display the corresponding binary value, for a given sentence. Here is my code for the same. package crups; import java.util.*; public class onetimepad { public static void main(String args[]) { HashMap <String , Integer>hm = new HashMap <String , Integer

HashTable和HashMap

微笑、不失礼 提交于 2019-12-10 15:12:57
Hashtable的应用非常广泛,HashMap是新框架中用来代替Hashtable的类,也就是说建议使用HashMap,不要使用Hashtable。可能你觉得Hashtable很好用,为什么不用呢?这里简单分析他们的区别。 1.Hashtable的方法是同步的,HashMap未经同步,所以在多线程场合要手动同步HashMap这个区别就像Vector和ArrayList一样。 查看Hashtable的源代码就可以发现,除构造函数外,Hashtable的所有 public 方法声明中都有 synchronized 关键字,而HashMap的源代码中则连 synchronized 的影子都没有,当然,注释除外。 2.Hashtable不允许 null 值(key 和 value 都不可以),HashMap允许 null 值(key和value都可以)。 先看个Hashtable正常输出的示例: Hashtable table = new Hashtable(); table.put("a-key", "a-value"); table.put("b-key", "b-value"); table.put("c-key", "c-value"); 输出如下: table.put(null, "a-value"); 运行之后异常如下: Exception in thread "main"

Why does ExecutorService deadlock when performing HashMap operations?

守給你的承諾、 提交于 2019-12-10 14:58:22
问题 When running the following class the ExecutionService will often deadlock. import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ExecutorTest { public static void main(final String[] args) throws InterruptedException { final ExecutorService executor = Executors.newFixedThreadPool(10); final HashMap<Object

Exception in thread “main” java.lang.StackOverflowError

醉酒当歌 提交于 2019-12-10 14:56:46
问题 I have a piece of code and I could not figure out why it is giving me Exception in thread "main" java.lang.StackOverflowError. This is the question: Given a positive integer n, prints out the sum of the lengths of the Syracuse sequence starting in the range of 1 to n inclusive. So, for example, the call: lengths(3) will return the the combined length of the sequences: 1 2 1 3 10 5 16 8 4 2 1 which is the value: 11. lengths must throw an IllegalArgumentException if its input value is less than