• September 1, 2014
  • Core Java
  • Comments Off on Map interface in java

Map interface in java

Map interface:

A map represents an object with key value pair.  A map cannot contain duplicate keys and one key can map to at most one value.

Commonly used methods of Map interface:

1. clear(): Removes all key-value pairs from this map.

Syntax: public void clear()

2. containsKey(Object k): Returns true if this map contains specified key otherwise returns false.

Syntax: public boolean containsKey(Object k)

3. (Object v): Returns true if this map contains specified value otherwise returns false.

Syntax: public boolean containsValue(Object v)

4. entrySet(): Returns a Set that contains the entries of this map. The set contains objects of type Map.Entry. It provides a set-view of this map.

Syntax: public Set entrySet().

5. equals(Object obj): Returns true if specified object is equals to this map otherwise returns false.

Syntax: public boolean equals(Object obj)

6. get(Object k): Returns the value associated with the specified key.

Syntax: public Object get(Object k)

7. hashCode(): Returns the hash code for this map.

Syntax: public int hashCode()

8. isEmpty(): Returns true if this map is empty otherwise returns false.

Syntax: public boolean isEmpty()

9. keySet(): Returns a Set that contains the keys in this map. This method provides a set-view of the keys in this map.

Syntax: public Set keySet()

10. put(Object k, Object v): Puts an entry in this map, overwriting any previous value associated with the specified key. Returns previous value linked to the key. Return null if key does not already exist.

Syntax: public Object put(Object k, Object v)

11. putAll(Map m): Puts all the entries from m into this map.

Syntax: public void putAll(Map m)

12. remove(Object k): Removes the entry whose key equals to specified key from this map. Returns the value to which this map previously associated with specified key, or null if the map contained no mapping for the key.

Syntax: public Object remove(Object k)

13. size(): Returns the number of entries in this map.

Syntax: public int size()

14. values(): Returns a collection containing the values in this map. This method provides a collection-view of the values in this map.

Syntax: public Collection values()

 A simple example of HashMap class to explain few methods of Map interface.

 HashMapTest.java

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
/**
 * This class is used to show the HashMap functionality.
 * @author javawithease
 */
public class HashMapTest {
	public static void main(String args[]){
		//Create HashMap object.
		Map hashMap = new HashMap();
 
		//Add objects to the HashSet.
		hashMap.put(4, "Roxy");
		hashMap.put(2, "Sunil");
		hashMap.put(5, "Sandy");
		hashMap.put(1, "Munish");
		hashMap.put(3, "Pardeep");
 
		//Print the HashMap object.
		System.out.println("HashMap elements:");
		System.out.println(hashMap);
 
		//Get iterator
		Set set=hashMap.entrySet();  
		Iterator iterator=set.iterator();  
 
		//Print the HashMap elements using iterator.
		System.out.println("HashMap elements using iterator:");
		while(iterator.hasNext()){
		   Map.Entry mapEntry=(Map.Entry)iterator.next();  
		   System.out.println("Key: " + mapEntry.getKey() + ", " +
		   		"Value: " + mapEntry.getValue());  
		}  
	}
}

Output:

HashMap elements:
{1=Munish, 2=Sunil, 3=Pardeep, 4=Roxy, 5=Sandy}
HashMap elements using iterator:
Key: 1, Value: Munish
Key: 2, Value: Sunil
Key: 3, Value: Pardeep
Key: 4, Value: Roxy
Key: 5, Value: Sandy

Download this example.
 
Next Topic: Daemon thread in java in java with example.
Previous Topic: List interface in java with example.

Related Topics:

Collection framework in java.
Collection interfaces in java.
Collection interface in java with example.
Set interface in java with example.
SortedSet interface in java with example.
List interface in java with example.
Map interface in java with example.
Daemon thread in java in java with example.
SortedMap interface in java with example.
Queue interface in java with example.
Deque interface in java with example.
Enumeration interface in java with example.
Collection classes in java with example.
HashSet in java with example.
LinkedHashSet in java with example.
TreeSet in java with example.
ArrayList in java with example.
LinkedList in java with example.
HashMap in java with example.
LinkedHashMap in java with example.
TreeMap in java with example.
PriorityQueue in java with example.
ArrayDeque in java with example.
Abstract classes in collection framework in java with example.
Sorting in java with example.
Comparable interface in java with example.
Comparator interface in java with example.
Properties class in java with example.
Hashtable in java with example.
ListIterator interface in java with example.

 


Comments are closed.