Questions tagged [hashmap]

A data structure that uses a hash function to map identifying values, known as keys, to their associated values

A hash map (or hash table) is a data structure which contains "key-value" pairs and allows retrieving values by key.

The most attractive feature is fast lookup of elements, particularly for large numbers of elements. Hash maps work by using a hash function to transform keys into a hash number that is then used as an index into an array of "buckets" containing one or more elements. This allows constant time access to the relevant bucket, followed by a linear search for the desired element within the bucket. When the number of elements in each bucket is kept low (possibly by dynamically resizing the array of buckets as elements are inserted) this offers constant time lookup on average even when the number of elements in the hash map increases. This can be a significant advantage compared to lookup in a tree-based structures which needs to perform more steps as the number of elements increases.

A drawback of hash tables is that elements are not stored in an obvious or meaningful order, as a good hash function will not map neighbouring keys to neighbouring buckets.

If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table.

Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

Map m = Collections.synchronizedMap(new HashMap(...));

The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

This class is a member of the Java Collections Framework.

Official docs: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

Stackoverflow Link: Differences between HashMap and Hashtable?

15203 questions
5
votes
2 answers

Java StackOverflowError after putting ArrayList to HashMap

Hello, can somebody explain to me why this block of code doesn't work? ArrayList list = new ArrayList(); list.add(list); HashMap map = new HashMap(); map.put(list, 1); After I put list to map, it…
peto1234
  • 369
  • 5
  • 11
5
votes
1 answer

Scala HashMap: iterate in insertion order?

I have been looking around, but most of them point to a java TreeMap. The only issue with that is I do not want to convert any Scala into java and back. If there really is no way, then I am ok with that, but I would like to hear it from some…
Andy
  • 10,553
  • 21
  • 75
  • 125
4
votes
2 answers

Why do I get so many collisions in my custom closed-hashset?

I have a custom closed-hashset/open-addressing (i.e. no linked lists) class. It's very specific to my needs - it's not generic (only for positive long numbers), needs the amount of records to be inserted to be predefined, and doesn't support remove…
user976850
  • 1,086
  • 3
  • 13
  • 25
4
votes
2 answers

How to Create Hashmap in android

i want to create hashmap.how its possible i want this type hashmap as below {Question=how are you, friend=mack} {Question=how are you, friend=jack} {Question=hello, friend=mack} {Question=hello, friend=jack} how its get this type of map from below…
user1153176
  • 1,006
  • 5
  • 15
  • 21
4
votes
2 answers

How can I define a macro/typedef/etc for tr1::unordered_map that doesn't bind the template arguments?

This might be a little silly question, but I just have to ask it. I am trying to use the unordered_map class in C++, but instead of referencing it everytime as tr1::unordered_map, I would like to just use the keyword hashMap.I know that typedef…
Vinayak Agarwal
  • 1,350
  • 3
  • 15
  • 28
4
votes
2 answers

Backbone.js : Changing view.attributes does not reflect on view.el

I have a backbone view like so window.InputView = Backbone.View.extend({ tagName:'input', className:'', attributes:{}, initialize:function(){ this.attributes=this.model.attributes; this.el =…
Gautam
  • 7,868
  • 12
  • 64
  • 105
4
votes
5 answers

Data structure to hold just keys (not caring about value)

I need to store a list of Strings and need to check if a String exists in the list. I normally would just use some Map with a key and boolean... i.e. HashMap map = new HashMap
K2xL
  • 9,730
  • 18
  • 64
  • 101
4
votes
4 answers

Having a composite key for hash map in c++

I have a data structure which has, , , and Since Book title or Author can be duplicated, I'd like to build a composite key. (let's say I cannot make extra unique key, such as ID) Since data is pretty huge, I'm using GCC…
devEvan
  • 361
  • 4
  • 16
4
votes
3 answers

linked hashmap finding key by value?

In a linked hashmap Is there any methods to get the value of K by only providing V? I've searched all over the internet and so far I've only found loops to get the key.
cataschok
  • 339
  • 1
  • 6
  • 10
4
votes
4 answers

Is it OK to use classes as dictionary keys in python?

I am about to write a function that finds a correct handler for a class. To achieve it I want to use a mapping from classes to handlers. I've already checked that it is possible to use classes as dictionary keys, however I am not completely sure if…
zefciu
  • 1,967
  • 2
  • 17
  • 39
4
votes
2 answers

NullPointerException when using GWT's AutoBean deserialization with HashMap

I have some problem with the Google's AutoBean serialization and deserialization. I have an AutoBean that contains primitive types and Maps as well. I can serialize and deserialize the primitive types without any problem, but when i try to read the…
SaWo
  • 1,515
  • 2
  • 14
  • 32
4
votes
3 answers

Fast loading of elements into array/list with fixed index without duplicates

My requirement is to enter strings into an array which are not in the array. I also need to maintain fixed indexes, as this array will be used with other data structure with a one-to-one relation with each index. At present i am using the ArrayList…
phoxis
  • 60,131
  • 14
  • 81
  • 117
4
votes
1 answer

Right usage of org.simpleframework.xml.ElementMap?

I am trying to use org.simpleframework.xml.ElementMap to map the following XML to my Java classes: blahblah one
Thomas
  • 1,053
  • 2
  • 11
  • 20
4
votes
1 answer

Java String: Is hashcode actually the hashvalue?

A hastable uses some hash function on an object to store. This hash function essentially calculates the position of the object in the table. If we use a HashTable or HashMap and the size can not fit more elements then these collections are…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
4
votes
2 answers

Objects in HashMap - pass to ListView adapter

Is it possible to pass both strings and drawables as objects in the same HashMap, and then use this hashmap to populate a ListView via SimpleAdapter ? I want this because I first get JSON data which also contains the URL to a thumbnail. Then I…
Kjell-Bear
  • 759
  • 1
  • 5
  • 12