Questions tagged [containskey]
80 questions
39
votes
4 answers
Java HashMap containsKey returns false for existing object
I have a HashMap for storing objects:
private Map fields = Collections.synchronizedMap(new HashMap());
but, when trying to check existence of a key, containsKey method returns false.
equals and hashCode methods are implemented, but…

agad
- 2,192
- 1
- 20
- 32
35
votes
6 answers
Java hashmaps without the value?
Let's say I want to put words in a data structure and I want to have constant time lookups to see if the word is in this data structure. All I want to do is to see if the word exists. Would I use a HashMap (containsKey()) for this? HashMaps use…

jbu
- 15,831
- 29
- 82
- 105
27
votes
6 answers
Java HashMap.containsKey() doesn't call equals()
I have a hashmap:
Map hm = new HashMap();
LotWaferBean lw = new LotWaferBean();
... //populate lw
if (!hm.containsKey((LotWaferBean) lw)) {
hm.put(lw, triggerFiles[l]);
}
The code for…

Will Sumekar
- 1,249
- 5
- 16
- 25
20
votes
4 answers
C# List as Dictionary key
I have a dictionary which is keyed by a List:
private Dictionary
- , string> Lookup;
I'm trying to use ContainsKey, but it doesn't seem to be working, and I have no idea why.
Here is the debug information from my Visual Studio…

Harry
- 863
- 3
- 10
- 26
11
votes
4 answers
Containskey VS Try Catch
I have a list of Vector2's Generated I have to check against a dictionary to see if they exist, this function gets executed every tick.
which would run fastest/ be better to do it this way?
public static bool exists(Vector2 Position,…

Dusty
- 413
- 6
- 17
9
votes
3 answers
Custom Class used as key in Dictionary but key not found
I have a class, show below, which is used as a key in a Dictionary
I'm having issues when trying to find any key within this dictionary, it never finds it at all. As you can see, I have overridden Equals and GetHashCode.
To look…

Ian Devlin
- 18,534
- 6
- 55
- 73
9
votes
4 answers
Check the existence of a HashMap key
In Java, having a HashMap fully filled in with data of such form:
HashMap map = new HashMap(1000000, 1);
what is faster when checking the existence of a random key, say 100:
if (map.get(100) == null))
or
if…

Sophie Sperner
- 4,428
- 8
- 35
- 55
8
votes
4 answers
HashMap with byte array key and String value - containsKey() function doesn't work
I'm using a HashMap: byte[] key and String value. But I realize that even I put the same object (same byte array and same string value) by using
myList.put(TheSameByteArray, TheSameStringValue)
into HashMap, the table still inserts a new object…

Anh-Tuan Mai
- 1,129
- 19
- 36
7
votes
4 answers
What does hashmap check when it calls containsKey()?
ArrayList lis = new ArrayList();
lis.add(2);
lis.add(3);
ArrayList lis2 = new ArrayList();
lis2.add(2);
lis2.add(3);
HashMap, Integer> map = new…

user1885433
- 313
- 5
- 13
6
votes
3 answers
Java HashMap containsKey
I have the following code
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class Person {
private String name;
private long birthTime;
@Override
public int hashCode() {
return Objects.hash(name,…

Tech Enthusiast
- 63
- 2
5
votes
3 answers
Java TreeMap contains a key but a containsKey call returns false (even the key is exactly the same unchanged object)
Why is it possible to loop the keySet of a TreeMap and getting a .containsKey == false?
for (Object thisObject : map.keySet()) {
if (!map.containsKey(thisObject)) {
System.out.println("This line should be never reached.");
…

F___ThisToxicCommunityHere
- 421
- 4
- 12
4
votes
2 answers
Check if pair of columns is in a row of a data frame
I'd like to know if there is any efficient way of checking if a given pair (or tuple of more than two) of columns is in a data frame.
For example, suppose I had the following data…

anymous_asker
- 113
- 1
- 11
3
votes
2 answers
Powershell - match with Containskey & set value of hashtable don't work
I am working on a script by Richard L. Mueller to disable inactive account in our AD.
Trap {"Error: $_"; Break;}
$D = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$Domain = [ADSI]"LDAP://$D"
$Searcher = New-Object…

nlks
- 69
- 1
- 2
- 11
3
votes
2 answers
Appropriate datastructure for key.contains(x) Map/Dictionary
I am somewhat struggling with the terminology and complexity of my explanations here, feel free to edit it.
I have 1.000 - 20.000 objects. Each one can contain several name words (first, second, middle, last, title...) and normalized numbers(home,…

ASA
- 1,911
- 3
- 20
- 37
3
votes
1 answer
C# Dictionary ContainsKey/TryGetValue not working
I've looked around for a while and seen plenty of references to modifying GetHashCode() and things when playing with ContainsKey() and TryGetValue() - but all those issues and examples have all been with some obscure user-specific key.
I have a…

Krenom
- 1,894
- 1
- 13
- 20