Java Map

Java Map

Java Map

Java Map , consist of a combination of key and value . Every individual  pair of Key and Value is called Entry.

In this Article , I will step through a simple Sample program to explain what is Java Map and how to implement entry and key.

Class mapIt

  1. First i have created new class i called it “mapIt”
  2. Then i import all the library listed below
    • import java.util.HashMap;
    • import java.util.LinkedHashMap;
    • import java.util.Map;
    • import java.util.TreeMap;
  3. In this program i will create all 3 different Class ” HashMap , LinkedHashMap , TreeMap”
  4. First i create a new instance name “hash_Map” from Class HasMap
  5. And then i add the Element with key and value being assigned
  6. Finally i used the getKey() & getValue() to iterate through the “hash_Map” and print out every single element contain inside
  7. Repeat Step 3, 4,5,6 for ” LinkedHashMap , TreeMap”
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;


public class mapIt {

	public static void main(String[] args) {
		
//**********Using HashMap******************************************		
		// HashMap does not maintain the orderSequence
		// Integer is the Key
		// string is the Value
		Map<Integer,String>hash_Map = new HashMap<Integer,String>();
		
		
		// Add element with define key < integer> and Value < string > in the newly created instance Map "hash_Map"
		hash_Map.put(1, "dog");
		hash_Map.put(3, "cat");
		hash_Map.put(7, "mice");
		hash_Map.put(8, "elephant");
		
		//Iterate the Map " hash_Map"
		
		System.out.println("Iteration for hash_Map");
		for (Map.Entry<Integer, String> entry: hash_Map.entrySet()) {
			int key = entry.getKey();
			String value = entry.getValue();
			System.out.println("Key: " + key + ", value: " + value);

		
		}
//**********Using LinkedHashMap**************************************
		
	//*********LinkedHashMap will maintain the element Sequence*******
		
		
		
		Map<Integer, String>link_Hash_Map = new LinkedHashMap<Integer, String>();
		
	//*********Add element with key into the newly created 	"link_Hash_Map"*****
		
		link_Hash_Map.put(1, "dog");
		link_Hash_Map.put(3, "cat");
		link_Hash_Map.put(7, "mice");
		link_Hash_Map.put(8, "elephant");
		//Iterate the Map " LinkedHashMap"
		
		System.out.println("Iteration for LinkedHashMap");
		for (Map.Entry<Integer, String> entry: link_Hash_Map.entrySet()) {
					int key = entry.getKey();
					String value = entry.getValue();
					System.out.println("Key: " + key + ", value: " + value);

				
				}
//**********Using TreeMap**************************************		
		
		//*********TreeMap will sort the element Sequence*******	
		
		
		Map <Integer ,String>tree_Map = new TreeMap<Integer, String>();
		
		
		// add new element with key into the newly 
		
		tree_Map.put(1, "dog");
		tree_Map.put(3, "cat");
		tree_Map.put(7, "mice");
		tree_Map.put(8, "elephant");
		//Iterate the Map " TreeMap"
		
		System.out.println("Iteration for TreeMap");
	   for (Map.Entry<Integer, String> entry: tree_Map.entrySet()) {
							int key = entry.getKey();
							String value = entry.getValue();
							System.out.println("Key: " + key + ", value: " + value);

						
						}
		
		
		

	}

}


 

Useful Java Map Method

No Method Description
1 Set entrySet() Returning Set View containing all keys and value
2 Set keySet() Return Set view Value containing all keys
3 boolean containsKey(Object key)) Search the Specified Key on this map
4 Object get(Object key) Return Value of the specific key
5 Object remove(Object key) Delete the entry of the specified key.
6 void putAll(Map map) Insert the Specified Map into this map
7 Object put(Object key, Object value) Insert an entry into this map.

Check out also Set in Java here

Check Map in Java at Oracle here

Leave a Reply

Your email address will not be published. Required fields are marked *

10 − one =