Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday 5 June 2017

Java Map

 

Java Map Interface

Java Map

Map is an interface in java which is available in java.util.* package. Collection map interface contains keys and values and each keys and values are called objects.

Map contains only unique key and can be contains duplicate values.

Each key and value pairs are known as entry.

There are four classes that implements Map interface in java collection.
  • HashMap class
  • LinkedHashMap class
  • HashTable class
  • TreeMap class

Methods of Java Map Interface

There are some useful methods of collection map interface. These are

1) void clear()

This method removes all of the mapping from this map.

2) Object put(Object key, Object value)

This method is used to insert an key and value i.e entry in this map.

3) void putAll(Map map)

This method is used to insert the specified map in this map.

4) boolean containsKey(Object key)

This method returns true if this map contains a mapping for the specified key.

5) boolean containsValue(Object value)

This method returns true if this map maps one or more keys to the specified value.

6) Object remove(Object key)

This is used to remove or delete the entry for the specified key.

7) Object get(Object key)

This method returns the value for the specified key.

8) Set keySet()

This method returns a Set view of the keys contained in this map.

9) Set entrySet()

This method returns the Set view containing all the key and values.

10) int hashCode()

This method returns the hash code value for this map. 

11) boolean isEmpty()

This method returns true if there is no key and value mapping.



Map.Entry Interface

Map.Entry interface is a child interface of Map interface and it provides method so that we easily get the key and get the value.

Methods of Map.Entry Interface

Map.Entry provides two method.

1) Object getKey()

It is used to get key.

2) Object getValue()

It is used to get the value.



Java Map Example

This is an example of Map interface.

import java.util.*;
class MapExample
{
public static void main(String args[])
{
//creating object of HashMap class
Map<Integer, String> m = new HashMap<Integer, String>();
m.put(1, "pink");//using put method to insert key and value
m.put(2, "red");
m.put(3, "yellow");
m.put(4, "orange");
m.put(5, "brown");
m.put(6, "pink");
//using Map.Entry interface
for(Map.Entry me : m.entrySet())
{
System.out.println(me.getKey()+" "+me.getValue());
}
}


output : 1 pink
              2 red
              3 yellow
              4 orange
              5 brown
              6 pink

In the above example, there is 1,2... is a key and pink, red... is value. In this program there is no duplicate key but there is duplicate value pink. In this above example we used HashMap class, we will learn in later this HashMap class.


Java Map Example 2

This is another example of java map where we will take duplicate elements and let's see what happens.

import java.util.*;
class MapExample2
{
public static void main(String args[])
{
//creating object of HashMap class
Map<Integer, String> m = new HashMap<Integer, String>();
m.put(1, "pink");//using put method to insert key and value
m.put(2, "red");
m.put(3, "yellow");
m.put(4, "orange");
m.put(5, "brown");
m.put(2, "black");//duplicate key
//using Map.Entry interface
for(Map.Entry me : m.entrySet())
{
System.out.println(me.getKey()+" "+me.getValue());
}
}

}

output : 1 pink
              2 black
              3 yellow
              4 orange
              5 brown




Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate