GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - Map Interface
Selected Reading
Java - Map InterfaceMap InterfaceThe Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date.
Map Interface Methods
Classes that Implement MapThe following are the classes that implement a Map to use the functionalities of a Map -
Interfaces that Extend MapThe following are the interfaces that extend the Map interface -
Examples of Map InterfaceExample 1Map has its implementation in various classes like HashMap. Following is an example to explain map functionality − import java.util.HashMap;
import java.util.Map;
public class CollectionsDemo {
public static void main(String[] args) {
Map<String, String> m1 = new HashMap<>();
m1.put("Zara", "8");
m1.put("Mahnaz", "31");
m1.put("Ayan", "12");
m1.put("Daisy", "14");
System.out.println();
System.out.println(" Map Elements");
System.out.print("\t" + m1);
}
}
OutputMap Elements
{Daisy = 14, Ayan = 12, Zara = 8, Mahnaz = 31}
Example 2Map has its implementation in various classes like TreeMap which sorts the entries based on keys. Following is an example to explain map functionality using TreeMap − import java.util.Map;
import java.util.TreeMap;
public class CollectionsDemo {
public static void main(String[] args) {
Map<String, String> m1 = new TreeMap<>();
m1.put("Zara", "8");
m1.put("Mahnaz", "31");
m1.put("Ayan", "12");
m1.put("Daisy", "14");
System.out.println();
System.out.println(" Map Elements");
System.out.print("\t" + m1);
}
}
Output Map Elements
{Ayan=12, Daisy=14, Mahnaz=31, Zara=8}
Example 3Map has its implementation in various classes like HashMap. Following is an example to explain map functions using HashMap to add and remove elements to the map− import java.util.HashMap;
import java.util.Map;
public class CollectionsDemo {
public static void main(String[] args) {
Map<String, String> m1 = new HashMap<>();
m1.put("Zara", "8");
m1.put("Mahnaz", "31");
m1.put("Ayan", "12");
m1.put("Daisy", "14");
System.out.println();
System.out.println(" Map Elements");
System.out.print("\t" + m1);
m1.remove("Daisy");
System.out.println(" Map Elements");
System.out.print("\t" + m1);
}
}
Output Map Elements
{Daisy=14, Ayan=12, Zara=8, Mahnaz=31} Map Elements
{Ayan=12, Zara=8, Mahnaz=31}
java_collections.htm
Advertisements
| ||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |