Tuesday, February 19, 2019

Java Maps

Java Map Interface
Library
  • java.util.Map
  • java.util.TreeMap

  • Background
  • Maps are part of the Collections Framework but technically Maps are not collections
           because they do not implement the Collection interface.
  • Maps store key / value pairs and all keys must be unique.
  • All maps implement the Map interface and share a common functionality that allows
           adding a key/value pair or access the value given they key.
  • Map provides two collection-views to deal with the keys and values separately.
  • A set of the entries is obtained by calling entrySet(). It returns a Set collection.
           The Set that contains the entries, which are held in an object of type Map.Entry.
  • The key can be obtained by calling getKey().
  • The value can be obtained by calling getValue().
  • The Set of the keys can be obtained by calling keySet().
  • The Collection of the values can be obtained by calling values().

  • Methods
    # Map is a generic interface
    # interface Map<K, V>
    public void clear()
    public boolean containsKey(Object k)
    public boolean containsValue(Object v)
    public Set<Map.Entry<K, V>> entrySet()
    public V get(Object k)
    public boolean isEmpty()
    public Set<K> KeySet()
    public V put(K k, V v)
    public void putAll (Map<? extends K, ? extends V> m)
    public v remove(Object k)
    public int size()
    public Collection<V> values()
    
    Java Map example code
    import java.io.*;
    import java.util.*;
    
    class MyMaps
    {
        public static void main(String args[]) {
            // Create a tree map
            TreeMap<String, Integer> myNums = new TreeMap<String, Integer>();
            myNums.put ("this", 99);
            myNums.put ("one", 1);
            myNums.put ("two", 2);
    
            System.out.println ("the map contains " + myNums.size() + " items.");
    
            // Create a set of the entry
            Set<Map.Entry<String, Integer>> set = myNums.entrySet();
            for (Map.Entry<String, Integer> my : set) {
                System.out.println (my.getKey());
                System.out.println (my.getValue());
            }
    
            // A new map to merge with myNums
            TreeMap<String, Integer> myNums2 = new TreeMap<String, Integer>();
            myNums2.put ("three", 3);
            myNums2.put ("four", 4);
            myNums.putAll (myNums2);
    
            // To show all entries after merging
            set = myNums.entrySet();
            System.out.println (myNums.size());
    
            for (Map.Entry<String, Integer> my : set) {
                System.out.println (my.getKey());
                System.out.println (my.getValue());
            }
    
            // To search a key
            if (myNums.containsKey("six")
                 System.out.println (myNums.get("six"));
            // Search for a value
            if (my.Nums.containsValue (1))
                System.out.println ("yes");
    
            // To remove an entry
            if (myNums.remove("six") != null)
                System.out.println ("six is removed!");
            else
                System.out.println (my.getValue());
    
            // Show entries after removal
            Set<String> keys = myNums.keySet();
            for (String str : keys)
                System.out.println (str);
            // Display the value set after removal
            Collection<Integer> values = myNums.values();
            for (Integer i : values)
                System.out.println (i);
    
            // Clear the map
            myNums.clear()
            if (! myNums.isEmpty())
                System.out.println ("The map is not empty! Check again!");        
        }
    }
    

    No comments:

    Post a Comment