Hash Maps?

510 views.

What is HashMap? and where and why we used Hashmaps?

Add Comment
1 Answer(s)
291 views.

HashMap is a member of the Java Collection Framework and is a very common way to collect and retrieve data. HashMap represents a data structure that offers key-value pairs storing, based on hashing.
In this example we are going to show how we can create a simple HashMap and a HashMap where multiple values correspond to a key, as well as some basic functions to add and retrieve HashMap’s objects.

1. Syntax of HashMap class

The general expression of HashMap Class is Class HashMap<K,V>, where:
K: specifies the type of keys maintained by this map
V: defines the type of mapped values

HashMap includes some different expressions for its constructor:
HashMap(): empty constructor with default values for initialCapacity and loadFactor.
HashMap(int initialCapacity): constructs an empty HashMap, in which initialCapacity is specified but loadFactor has its default value.
HashMap(int initialCapacity, float loadFactor): constructs an empty HashMap, specified by the initialCapacity and loadFactor.
HashMap(Map m): a constructor of a new HashMap that includes the specified mapping.
The arguments are:
initialCapacity: is the initial number of buckets of the hash table, where its default value is 16.
loadFactor: represents the percentage of how full the hash table can be, before the increasing of its capacity. Its default value is 0.75

 

Example :::

 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapTest {

public static void main(String[] args) {

// a simple hashMap declaration with default size and load factor

HashMap<String, Integer> hashMap = new HashMap<String, Integer>();

// hashMap with multiple values with default size and load factor

HashMap<String, ArrayList<String>> multiMap = new HashMap<String, ArrayList<String>>();

// Put elements to the hashMap

hashMap.put(“USA”, new Integer(1));

hashMap.put(“UK”, new Integer(2));

hashMap.put(“China”,new Integer(3));

// take a value of a specific key

System.out.println(“Simple HashMap: Key ‘UK’ has value = ” + hashMap.get(“UK”));

// see if a specific value or key is into the hashMap

System.out.println(“Simple HashMap containes value ‘1’ – ” + hashMap.containsValue(1));

System.out.println(“Simple HashMap containes key ‘Greece’ – “

+ hashMap.containsKey(“Greece”));

// create an arrayList to store values

ArrayList<String> listOne = new ArrayList<String>();

listOne.add(“Blue”);

listOne.add(“Black”);

listOne.add(“Brown”);

// create list two and store values

ArrayList<String> listTwo = new ArrayList<String>();

listTwo.add(“Pink”);

listTwo.add(“Purple”);

// put values into map

multiMap.put(“B color”, listOne);

multiMap.put(“P color”, listTwo);

// Get a set of the entries

Set<Entry<String, ArrayList<String>>> setMap = multiMap.entrySet();

// Get an iterator

Iterator<Entry<String, ArrayList<String>>> iteratorMap = setMap.iterator();

System.out.println(“\nHashMap with Multiple Values”);

// display all the elements

while(iteratorMap.hasNext()) {

Map.Entry<String, ArrayList<String>> entry =

(Map.Entry<String, ArrayList<String>>) iteratorMap.next();

String key = entry.getKey();

List<String> values = entry.getValue();

System.out.println(“Key = ‘” + key + “‘ has values: ” + values);

}

}

}

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.