Java Program to Develop a Map Interfaces

Aim:

Develop a java Programming was Collections of Map Interfaces.

Procedure:

  • Step 1: Open a New Java Application on Netbeans Editor.
  • Step 2: Open Netbeans Ide, Choose File new Project.
  • Step 3: New Open a New Project and Choose a Java Application.
  • Step 4: Create a Java File for Performing map inteface such as tree map, hashset.
  • Step 5: Run the Index Page and Verify the Result Page.
  • Step 6: Save the Project (Ctrl+s).
  • Step 7: Run the Project (F5).

Program:

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
public class mapp
{
 public static void main(String[] args)
 {
 TreeMap<String,String>hm=new TreeMap<String,String>(new MyCompr());
 hm.put("java","language");
 hm.put("computer","machine");
 hm.put("india","country");
 hm.put("mango","fruit");
 hm.put("game","cricket");
 System.out.println("TreeMap Entries:");
 System.out.println(hm);
 Map<String,String>subMap1=hm.subMap("game","java");
 System.out.println("Sub-Map entries:");
 System.out.println(subMap1);
 Map<String,String>subMap2=hm.subMap("game",true,"java",true);
 System.out.println("sub-map entries:");
 System.out.println(subMap2); 
Map<String,String>subMap3=hm.subMap("game",false,"java",true);
 System.out.println("sub-Map entries:");
 System.out.println(subMap3);
 }
}
class MyCompr implements Comparator<String>
{
 public int compare(String str1,String str2)
 {
 return str1.compareTo(str2);
 }
} 

Output:

Leave a Comment