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.









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 iterator with example
() method to get the data in Set object form. Java Map Iterator with Example... Java Map Iterator is an interface. It keeps the data in the key and value form. It is implemented by HashMap, Tree Map. Map has no iterator method. So
Advertisements
Java Map Example
Java Map Example: package Collection; import java.util.HashMap; import...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
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
Spring Map Example
Spring Map Example In this example you will see how bean is prepared for injecting Map collection type key and its values. MapBean.java... Download this example code
Java Map Iterate
Map is an Interface in the Collection Framework. Map is implemented by its two sub classes HashMap Treemap Map has no iterator method. So use the entrySet() method.It returns the data as Set Example of Java Map
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
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
Example to show Hash map exception in java
Example to show Hash map exception in java       The implementation of map interface is based... method to key-value pairsADS_TO_REPLACE_2 Understand with Example Hash map in java
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     ... static void main(String[] args) { System.out.println("Tree Map Example!\n"... TreeMapExample Tree Map Example! Keys of tree map: [1, 2, 3
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
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 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 map building - Development process
Java map building  Hello All I want to build a map for a road... to proceed further i need to build a map using these coodinates which i am unable to think of how to do it. I can only use java swing or 2d for this purpose. Any
Map Java Objects with Database tables
Map Java Objects with Database tables  How to map Java Objects with Database tables?   First write Java domain objects ie. beans with setter and getter methods. Then map java class to table and database columns to Java
java code to compare two hash map objects
java code to compare two hash map objects  java code to compare two hash map objects
Map
;Please visit the following links: HashTable Example ArrayList Example HashMAp Example
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... with the transformed data. Here is example program of map() function which is used
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);   map = new HashMap();   map.put("rno
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
Using Arraylist or Hash Map - Java Interview Questions
Using Arraylist or Hash Map  what could b the pros and cons changing between Hash Map to Array List
Map interface
Map interface  What is the Map interface
google map with jsf - Java Server Faces Questions
google map with jsf  hi, i am using jsf with googlemap.i load the script file with googlemap .its working fine.how can i load the database values(latlong) to googlemap from jsf bean values.i want to show mutiple markers
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 (adsbygoogle
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
Map code
Map code  hi................ I have map of navi mumbai, where i want to see the location vashi, nerul, turbhe.... means want to zoom into this area... will help me to do so.....?????? i am using java swings
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
Generate area for map from backing bean - Java Server Faces Questions
Generate area for map from backing bean  I need to populate the map in my page, and the coordinates should come from the backing bean.. How could I do
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
Maven Repository/Dependency: com.github.romix | java-concurrent-hash-trie-map
Maven Repository/Dependency of Group ID com.github.romix and Artifact ID java-concurrent-hash-trie-map. Latest version of com.github.romix:java-concurrent-hash-trie-map dependencies. # Version Release Date
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
Maven dependency for com.graphhopper - map-matching version 0.4.0-RC1 is released. Learn to use map-matching version 0.4.0-RC1 in Maven based Java projects
; com.graphhopper - map-matching version 0.4.0-RC1 in Java projects. Follow the step...; com.graphhopper - map-matching version 0.4.0-RC1 java library in your project...Maven dependency for  com.graphhopper  - Version 0.4.0-RC1 of map
Maven dependency for org.webjars.npm - source-map version 0.7.2 is released. Learn to use source-map version 0.7.2 in Maven based Java projects
this version ( org.webjars.npm - source-map version 0.7.2 ) in their Java...Maven dependency for  org.webjars.npm  - Version 0.7.2 of source-map released The developers of   org.webjars.npm - source-map project
Maven dependency for org.webjars.npm - source-map version 0.5.4 is released. Learn to use source-map version 0.5.4 in Maven based Java projects
this version ( org.webjars.npm - source-map version 0.5.4 ) in their Java...Maven dependency for  org.webjars.npm  - Version 0.5.4 of source-map released The developers of   org.webjars.npm - source-map project
Maven dependency for org.webjars.npm - source-map version 0.7.4 is released. Learn to use source-map version 0.7.4 in Maven based Java projects
this version ( org.webjars.npm - source-map version 0.7.4 ) in their Java...Maven dependency for  org.webjars.npm  - Version 0.7.4 of source-map released The developers of   org.webjars.npm - source-map project
Hash Map
Hash Map  how to add the hash map objects in the list and iterate the objects
hash map
int getI2(){ return i2; } } class Example{ public static void main...=2; HashMap<Integer,Test> map=new HashMap<Integer,Test>
FTP Java example
FTP Java example  Where is the FTP Java example on your website? I am... examples of FTP at: FTP Programming in Java tutorials with example code. Thaks... functionality in my Java based application. My application is swing based and I have
freemarker example - Java Beginners
an example for freemarker. i want to get the values from java and display those values in the page designed using freemarker(how to get the values from java). and please provide an example with code and directory structure. send me ASAP

Ads