hashmap

null pointer exception when retrieving from HashMap even though containsKey() returns true

旧巷老猫 提交于 2019-12-13 02:09:29
问题 I've been developing an Android guitar app, and currently I'm having some problems with HashMap . To sum it up, I get a nullpointer exception when I try to retrieve a value from a HashMap with get(key) despite containsKey(key) returning true for that key. Specifically, I have an instance _currentPreset of a class, which has a HashMap called _maxCoordRange , which is initialized in the class like this: private Map<String, Float> _maxCoordRange = new HashMap<String, Float>(); This parameter is

What does `putForNullKey` method do inside the `put` method in hashmap?

荒凉一梦 提交于 2019-12-13 02:08:21
问题 Today I was fixing one defect and found a very interesting thing. I was trying to put a key value pair in hashmap. (I was assuming that key is there but later it was found to be null). So while retrieving the value using a key , I was not getting null every time. Later I found that key is null , I corrected it. But then I see the code for put method of Hashmap . Why does it does not gives an exception when the key is null ? It calls putForNullKey private method. What does it do? But i am

Writing a Java program to encrypt and decrypt a ADFGVX cipher

有些话、适合烂在心里 提交于 2019-12-13 02:06:08
问题 I need to be able to encrypt and decrypt a message using a Polybius Square. I know how this works on paper but don't know where to start when turning it into a program. I was planning to use a hash map but I was told thats a bad way to go about it and that there are better approaches to do this...but I don't know what approaches that would be. I've been given code to help me by my lecture from a workshop on the project but I don't fully understand it. I'll paste it below and if anyone could

Is there anything wrong with replacing class attributes with a HashMap?

丶灬走出姿态 提交于 2019-12-13 01:51:38
问题 Just a theoretical question that could lead to some considerations in terms of design. What if you were to replace POJOs with this reusable class ? It might avoid some boilerplate code but what issues could it bring about ? // Does not include failsafes, guards, defensive copying, whatever... class MySingleGetterAndSetterClass{ private HashMap<String,Object> myProperties; public SingleGetterAndSetter( String name ){ myProperties = new HashMap<String,Object>(); myProperties.put( "name", name )

深入浅出HashMap的设计与优化

空扰寡人 提交于 2019-12-13 00:50:09
一:常用的数据结构 众所周知, ArrayList 是基于数组的数据结构实现的,LinkedList 是基于链表的数据结构实现的,而 HashMap 是基于哈希表的数据结构实现的。我们不妨一起来温习下常用的数据结构。 数组: 采用一段连续的存储单元来存储数据。对于指定下标的查找,时间复杂度为 O(1),但在数组中间以及头部插入数据时,需要复制移动后面的元素。 链表: 一种在物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素)组成,结点可以在运行时动态生成。每个结点都包含“存储数据单元的数据域”和“存储下一个结点地址的指针域”这两个部分。由于链表不用必须按顺序存储,所以链表在插入的时候可以达到 O(1) 的复杂度,但查找一个结点或者访问特定编号的结点需要 O(n) 的时间。 哈希表: 根据关键码值(Key value)直接进行访问的数据结构。通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做哈希函数,存放记录的数组就叫做哈希表。 树: 由 n(n≥1)个有限结点组成的一个具有层次关系的集合,就像是一棵倒挂的树。 二:HashMap 的实现结构 了解完数据结构后,我们再来看下 HashMap 的实现结构。作为最常用的 Map 类,它是基于哈希表实现的,继承了 AbstractMap

Test if a string contains the key of a hash in Perl

怎甘沉沦 提交于 2019-12-13 00:35:27
问题 I have a string and want to tell if it contains the key of a hash and if it does I would like to print the value of the hash like so: #!/usr/bin/perl -w my %h = ( 'key1' => 'v1', 'key2' => 'v2', 'key3' => 'v3' ); my $str = "this is a string containing key1\n"; if ($str contains a key of %h){ print the value of that key; #i.e v1 } Whats the best way to do this? (Preferably concise enough to contain in an if statement) 回答1: If you have to search through multiple strings but have just the one

How to facilitate Netbeans Designer to load JPanel-s that use an enum reverse-lookup using hashmap?

核能气质少年 提交于 2019-12-12 23:13:52
问题 This has become a Dorothy Dix as I found the root of the matter while I was composing this question. Nevertheless, I decided to post it because it had everyone in our office stumped and I could find nothing helpful in the google or stackoverflow searches. This is a good example of once you ask the right question, you may see the light. While the title might sounds complicated, it is a really simple problem that seemed to have no answer. At the centre of this is an enum : Status public enum

ConcurrentHashMap的实现原理(JDK1.7和JDK1.8)

人走茶凉 提交于 2019-12-12 22:51:24
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 哈希表 1.介绍 哈希表就是一种以 键-值(key-indexed) 存储数据的结构,我们只要输入待查找的值即key,即可查找到其对应的值。 哈希的思路很简单,如果所有的键都是整数,那么就可以使用一个简单的无序数组来实现:将键作为索引,值即为其对应的值,这样就可以快速访问任意键的值。这是对于简单的键的情况,我们将其扩展到可以处理更加复杂的类型的键。 2.链式哈希表 链式哈希表从根本上说是由一组链表构成。每个链表都可以看做是一个“桶”,我们将所有的元素通过散列的方式放到具体的不同的桶中。插入元素时,首先将其键传入一个哈希函数(该过程称为哈希键),函数通过散列的方式告知元素属于哪个“桶”,然后在相应的链表头插入元素。查找或删除元素时,用同们的方式先找到元素的“桶”,然后遍历相应的链表,直到发现我们想要的元素。因为每个“桶”都是一个链表,所以链式哈希表并不限制包含元素的个数。然而,如果表变得太大,它的性能将会降低。 3.应用场景 我们熟知的缓存技术(比如redis、memcached)的核心其实就是在内存中维护一张巨大的哈希表,还有大家熟知的HashMap、CurrentHashMap等的应用。 ConcurrentHashMap与HashMap等的区别 1.HashMap 我们知道HashMap是线程不安全的

Perl hashes don't work as expected

丶灬走出姿态 提交于 2019-12-12 19:23:18
问题 #!/usr/bin/perl sub f { { a => 1, b => 2 } } sub g { { %{f()}, c => 3, d => 4, } } use Data::Dumper; print Dumper g(); The above code outputs $VAR1 = 'a'; $VAR2 = 1; $VAR3 = 'b'; $VAR4 = 2; $VAR5 = 'c'; $VAR6 = 3; $VAR7 = 'd'; $VAR8 = 4; despite in my understanding it should output $VAR1 = { 'a' => 1, 'c' => 3, 'b' => 2, 'd' => 4 }; What is my misunderstanding? 回答1: The problem is that a pair of braces is ambiguous in Perl, and may be either a block or an anonymous hash Because of the

Double indexing in associative array

我们两清 提交于 2019-12-12 18:07:19
问题 I'm building a server using Node.js. I store the objects representing my users in an associative array and right now the index of those objects are the sockets id of that connection. Using the Socket.io library i obtain it by simply doing socket.id, so for each handler that takes care of my requests I can always know which user made the request. Client-side every user has the ids of the connected users (that are different from the socket ids). The problem araises when i have an handler that