java the map interface

Solutions on MaxInterview for java the map interface by the best coders in the world

showing results for - "java the map interface"
Max
09 Aug 2017
1//The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date.
2
3Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key.
4Several methods throw a NoSuchElementException when no items exist in the invoking map.
5A ClassCastException is thrown when an object is incompatible with the elements in a map.
6A NullPointerException is thrown if an attempt is made to use a null object and null is not allowed in the map.
7An UnsupportedOperationException is thrown when an attempt is made to change an unmodifiable map.
8Example of List, Set, Map
9import java.util.*; // All the classes and interfaces are part of the util package
10public class CollectionsDemo {
11
12   public static void main(String[] args) {
13      // ArrayList 
14      List a1 = new ArrayList();
15      a1.add("Zara");
16      a1.add("Mahnaz");
17      a1.add("Ayan");
18      System.out.println(" ArrayList Elements");
19      System.out.print("\t" + a1);
20
21      // LinkedList
22      List l1 = new LinkedList();
23      l1.add("Zara");
24      l1.add("Mahnaz");
25      l1.add("Ayan");
26      System.out.println();
27      System.out.println(" LinkedList Elements");
28      System.out.print("\t" + l1);
29
30      // HashSet
31      Set s1 = new HashSet(); 
32      s1.add("Zara");
33      s1.add("Mahnaz");
34      s1.add("Ayan");
35      System.out.println();
36      System.out.println(" Set Elements");
37      System.out.print("\t" + s1);
38
39      // HashMap
40      Map m1 = new HashMap(); 
41      m1.put("Zara", "8");
42      m1.put("Mahnaz", "31");
43      m1.put("Ayan", "12");
44      m1.put("Daisy", "14");
45      System.out.println();
46      System.out.println(" Map Elements");
47      System.out.print("\t" + m1);
48   }
49}