Java Map Example

Java Map Example

How we can use Map in java collection?

View Answers

June 20, 2012 at 12:16 PM

The Map interface maps unique keys to value means it associate value to unique keys.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapExample {
    public static void main(String[] args) {

        Map mp = new HashMap();
        mp.put(new Integer(2), "Two");
        mp.put(new Integer(1), "One");
        mp.put(new Integer(3), "Three");
        mp.put(new Integer(4), "Four");

        Set st = mp.entrySet();

        Iterator it = st.iterator();

        while (it.hasNext()) {

            Map.Entry m = (Map.Entry) it.next();

            int key = (Integer) m.getKey();

            String value = (String) m.getValue();
            System.out.println("Key :" + key + "  Value :" + value);

        }
    }
}

Output:

Key :1  Value :One
Key :2  Value :Two
Key :3  Value :Three
Key :4  Value :Four

Description:- The above example demonstrates you the Map interface. Since Map is an interface, you need to instantiate a concrete implementation of the interface in order to use it. Here Map is implemented by its subclass HashMap. Using the put method, we have added elements to key and value pair. Now to display the key and values, we have called getKey() and getValue() methods that are defined by Map.Entry.

Ads









Related Tutorials/Questions & Answers:
Java Map Example
Java Map Example  How we can use Map in java collection?   The Map interface maps unique keys to value means it associate value to unique... Description:- The above example demonstrates you the Map interface. Since Map
Java Map Example
the key and values. Map.Entry defines both of these methods. Java Map Example...Map Interface adds keys to values. Every key in the Map Interface should be unique. It is part of java.util package. Each element in a map has a key
Advertisements
JavaScript google map API example.
JavaScript google map API example.  How to Use Google Maps API in JavaScript?   Google Maps- A map is a way of representation of your route...;/html> Description: - This example loads the google map with defined
Map java Object to database.
Map java Object to database.  How do you map Java Objects with Database tables
parse map in java
parse map in java  What is a Parser? Can you give the source code to parse map in Java
Example to show Hash map exception in java
Example to show Hash map exception in java       The implementation of map interface is based...-value pairs Understand with Example Hash map in java is same as the Hash table
Map
Map  If I have an object implementing the Map interface in Java and I... way of going through the map either iterator or for each loop or for loop   Here is an example of HashMap. import java.util.*; public class
java code for PartialSearch using Map????
java code for PartialSearch using Map????  java code for Partial Search using Map
Tree Map Example
Tree Map Example     ...(String[] args) { System.out.println("Tree Map Example!\n"); TreeMap <... the tree map contain: " + tMap.values()); } } Download this example
How to convert map to json in Java
How to convert map to json in Java  Hi, I my program I have an object of HashMap and I have to output this into json string. How to convert map to json in Java? Thanks   Hi, You have to use the Google Gson library
How to convert map to json in Java
How to convert map to json in Java  Hi, I my program I have an object of HashMap and I have to output this into json string. How to convert map to json in Java? Thanks   Hi, You have to use the Google Gson library
Java tree map trouble
Java tree map trouble  This is a project for a class, and I am totally stuck! I've created a tree map using an input text file. I split the lines into fields so that I could work with 2 elements from the file. It appears
Java Map
Java Map         In Java, a Map is a kind of generalized array. It provides a more general way of storing elements. It is an object that maps keys to values. Map
map
map  using map, i want display message based on id selected [both are strings]   i do no   Hi Friend, Try the following code...) { Map map=new HashMap(); map.put("Message1","Hello
java code to compare two hash map objects
java code to compare two hash map objects  java code to compare two hash map objects
Python Spark Map function example
Python Spark Map function example - Writing word count example with Map function In this example program we are going to learn about the map() function... data. Here is example program of map() function which is used for creating word
Python Spark Map function example
Python Spark Map function example - Writing word count example with Map function In this example program we are going to learn about the map() function... data. Here is example program of map() function which is used for creating word
Map
;Please visit the following links: HashTable Example ArrayList Example HashMAp Example
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java using for each? Thanks   Hi, This example is using the stream
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java using for each? Thanks   Hi, This example is using the stream
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java using for each? Thanks   Hi, This example is using the stream
How to use Map in velocity
How to use Map in velocity       This Example shows you how to use map in velocity. The method...;map.put("libno", 1001);   list.add(map);   map 
How do you map Java Objects with Database tables?
How do you map Java Objects with Database tables?  Hi, How do you map Java Objects with Database tables? thanks
java persistence example
java persistence example  java persistence example
Java HashMap example.
Java HashMap example. The HashMap is a class in java. It stores values in name values pair. You can store null value of key and values.   Here... of map. Code:  HashMapExample .java package net.roseindia.java
Java Client Application example
Java Client Application example  Java Client Application example
Example of HashSet class in java
Example of HashSet class in java. In this part of tutorial, we... unique. You can not store duplicate value. Java hashset example. How.... Example of Hashset iterator method in java. Example of Hashset size() method
What is array in java with example?
What is array in java with example?  Hi, I am beginner in Java and want to learn Java Array concepts. Which is the best tutorials for learn Java Array with example codes? What is array in java with example? Thanks
Java FTP Client Example
Java FTP Client Example  How to write Java FTP Client Example code? Thanks   Hi, Here is the example code of simple FTP client in Java which downloads image from server FTP Download file example. Thanks
Example of HashMap class in java
Example of HashMap class in java. The HashMap is a class in java collection framwork. It stores values in the form of key/value pair. It is not synchronized
Interactive map
Interactive map  hi........ i require map to be zoom in and enlarge the area and show its detail. Can anyone tel me how can this be done using java swings .... plz help

Ads