Sunday, September 25, 2016

JAVA HashMaps


HashMaps implement the Map Interface which stores data using a key and a value pair.  You can think of an array as a map that uses the index locations as the key for each value stored.

When you want to map values to different types of keys, like mapping names to an address, you can use a Map. This way, you can store data that is associated with other data.

The keys used must be unique identifiers for a specific value in a HashMap group. Typically, keys will be strings and the data mapped to the keys will be numerical integers, doubles, etc. The string keys are converted to integer numbers using the "Hash Function." Keys are assigned to integers randomly and stored in a table. To call a value in the HashMap, the key must be provided.


Benefits of HashMap

1) Ability to store data that is associated with other data, ie names and phone numbers
2) Fast Look-Up - must know the key for the value of interest
3) Fast Addition - adding to table without concern of order
4) Fast Subtraction - subtraction of an element not concerned with order


Methods Available in the Map Interface

- add a (key, value) pair - put (K key, V value)
- looks up the value of a specified key - get (key)
- remove elements  - remove(key)
- clear all collection - clear()
- searches for a key, if found returns TRUE -  containsKey(key)
- searches for a value, if found mapped to one or more keys returns TRUE -  containsValue(key)
- checks if the map contains the same elements as another map - equals(map)
- get size of collection - size()
- checks to see if the collection is empty - isEmpty()
- returns a string version of the map - toString()
- return a Set Collection of all keys - keyset ()
- return a Collection of all values - values ()





Initializing a HashMap


When initializing a HashMap it is helpful to use the more basic Map callout for the first collection type - this will help if you want to change the type easily later on, if needed.


Map<String,Integer> newMap = new HashMap<String,Integer>();


Using Methods on a HashMap



The following example stores the number of times a playing card comes up in a game. For example, each time a King of Hearts is turned over, a counter increases by 1 for that type of card. A list of all cards is mapped to the corresponding count for the number of times that card was flipped.  Finally, a list of the (key, value) pairs is printed.  This example will assume a list of card flips has already been loaded into an array called arrayCards.


// Needed import for the HashMap generation


import java.util.*;

Map<String,Integer> newMap = new HashMap<String,Integer>();

for (String card : arrayCards) {

//Determine if the card is already contained in the map as a key
//If it does not currently exist add it with a count of 1
//If it does currently exist add to the current count by 1

if (!newMap.containsKey(card)) {
newMap.put(card,1);
} else {
newMap.put(card,newMap.get(card)+1);
}

}

// Now Iterate over a Set of the HashMap keys to print the key and value pair



Set<String> keys = newMap.keySet();

Iterator <String> itr = keys.iterator();

while(itr.hasNext()) {

String key = itr.next();
int value = newMap.get(key);

Out.println(key + " " + value);

}


2 comments:

  1. I loved the article! I looked for this in several places and only found it here. Congratulations on quality content!

    ReplyDelete
  2. That was really what I was looking for! Many thanks and congratulations for the blog!

    ReplyDelete