Java Map Example

Ads

 
 

Share on Google+Share on Google+

Naulej kumar
Java Map Example
1 Answer(s)      5 years and 2 months ago
Posted in : Java Beginners

How we can use Map in java collection?

Ads
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 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
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
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
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...: Satya = 101 Rohit = 102 Aniket = 103 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
java code for PartialSearch using Map????
java code for PartialSearch using Map????  java code for Partial Search using Map
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
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 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
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
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
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
Map
;Please visit the following links: HashTable Example ArrayList Example HashMAp Example
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
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
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 
java run time textfield and use with map - Java Beginners
java run time textfield and use with map  i want to program which read data from file and store in map and display in different textfields which are generated at run time like a text file store information class a1
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
Map interface
Map interface  What is the Map interface
java persistence example
java persistence example  java persistence example
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
Java Client Application example
Java Client Application example  Java Client Application 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
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