Showing posts with label Collection-LinkedHashMap. Show all posts
Showing posts with label Collection-LinkedHashMap. Show all posts

Tuesday, August 16, 2011

Remove all values from Java LinkedHashMap

package org.best.example;

    /*
      Remove all values from Java LinkedHashMap example
      This Java Example shows how to remove all values from LinkedHashMap object or empty
      LinkedHashMap or clear LinkedHashMap using clear method.
    */
    
    import java.util.LinkedHashMap;
    
    public class EmptyLinkedHashMapExample {
    
    public static void main(String[] args) {
    
    //create LinkedHashMap object
    LinkedHashMap lHashMap = new LinkedHashMap();
    
    //add key value pairs to LinkedHashMap
    lHashMap.put("1","One");
    lHashMap.put("2","Two");
    lHashMap.put("3","Three");
    
    /*
      To remove all values or clear LinkedHashMap use
      void clear method() of LinkedHashMap class. Clear method removes all
      key value pairs contained in LinkedHashMap.
      */
    
    lHashMap.clear();
    
    System.out.println("Total key value pairs in LinkedHashMap are : "
    + lHashMap.size());
    }
    }
    
    /*
    Output would be
    Total key value pairs in LinkedHashMap are : 0
    */

Monday, August 15, 2011

Iterate through the values of Java LinkedHashMap

package org.best.example;

    /*
      Iterate through the values of Java LinkedHashMap example
      This Java Example shows how to iterate through the values contained in the
      LinkedHashMap object.
    */
    
    import java.util.Collection;
    import java.util.LinkedHashMap;
    import java.util.Iterator;
    
    public class IterateValuesOfLinkedHashMapExample {
    
    public static void main(String[] args) {
    
    //create LinkedHashMap object
    LinkedHashMap lHashMap = new LinkedHashMap();
    
    //add key value pairs to LinkedHashMap
    lHashMap.put("1","One");
    lHashMap.put("2","Two");
    lHashMap.put("3","Three");
    
    /*
      get Collection of values contained in LinkedHashMap using
      Collection values() method of LinkedHashMap class
      */
    Collection c = lHashMap.values();
    
    //obtain an Iterator for Collection
    Iterator itr = c.iterator();
    
    //iterate through LinkedHashMap values iterator
    while(itr.hasNext())
    System.out.println(itr.next());
    }
    }
    
    /*
    Output would be
    One
    Two
    Three
    */

Sunday, August 14, 2011

Get Size of Java LinkedHashMap

package org.best.example;

        /*
      Get Size of Java LinkedHashMap Example
      This Java Example shows how to get the size or nubmer of key value pairs
      stored in LinkedHashMap using size method.
    */
    
    import java.util.LinkedHashMap;
    
    public class GetSizeOfLinkedHashMapExample{
    
    public static void main(String[] args) {
    
    //create LinkedHashMap object
    LinkedHashMap lHashMap = new LinkedHashMap();
    
    /*
      To get the size of LinkedHashMap use
      int size() method of LinkedHashMap class. It returns the number of key value
      pairs stored in LinkedHashMap object.
      */
    System.out.println("Size of LinkedHashMap : " + lHashMap.size());
    
    //add key value pairs to LinkedHashMap using put method
    lHashMap.put("1","One");
    lHashMap.put("2","Two");
    lHashMap.put("3","Three");
    System.out.println("Size of LinkedHashMap after addition : " + lHashMap.size());
    
    //remove one element from LinkedHashMap using remove method
    Object obj = lHashMap.remove("2");
    System.out.println("Size of LinkedHashMap after removal : " + lHashMap.size());
    }
    }
    
    /*
    Output would be
    Size of LinkedHashMap : 0
    Size of LinkedHashMap after addition : 3
    Size of LinkedHashMap after removal : 2
    */

Saturday, August 13, 2011

Get Set view of Keys from Java LinkedHashMap

package org.best.example;

      /*
      Get Set view of Keys from Java LinkedHashMap example
      This Java Example shows how to get a Set of keys contained in LinkedHashMap
      using keySet method of Java LinkedHashMap class.
    */
    
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.Set;
    
    public class GetSetViewOfKeysFromLinkedHashMapExample {
    
    public static void main(String[] args) {
    
    //create LinkedHashMap object
    LinkedHashMap lHashMap = new LinkedHashMap();
    
    //add key value pairs to LinkedHashMap
    lHashMap.put("1","One");
    lHashMap.put("2","Two");
    lHashMap.put("3","Three");
    
    /*
      get Set of keys contained in LinkedHashMap using
      Set keySet() method of LinkedHashMap class
      */
    
    Set st = lHashMap.keySet();
    
    System.out.println("Set created from LinkedHashMap Keys contains :");
    //iterate through the Set of keys
    Iterator itr = st.iterator();
    while(itr.hasNext())
    System.out.println(itr.next());
    
    /*
      Please note that resultant Set object is backed by the LinkedHashMap.
      Any key that is removed from Set will also be removed from
      original LinkedHashMap object. The same is not the case with the element
      addition.
      */
    
    //remove 2 from Set
    st.remove("2");
    
    //check if original LinkedHashMap still contains 2
    boolean blnExists = lHashMap.containsKey("2");
    System.out.println("Does LinkedHashMap contain 2 ? " + blnExists);
    }
    }
    
    /*
    Output would be
    Set created from LinkedHashMap Keys contains :
    1
    2
    3
    Does LinkedHashMap contain 2 ? false
    */

Friday, August 12, 2011

Check if a particular value exists in Java LinkedHashMap

package org.best.example;

       /*
      Check if a particular value exists in Java LinkedHashMap example
      This Java Example shows how to check if LinkedHashMap object contains a particular
      value using containsValue method of LinkedHashMap class.
    */
    
    import java.util.LinkedHashMap;
    
    public class CheckValueOfLinkedHashMapExample {
    
    public static void main(String[] args) {
    
    //create LinkedHashMap object
    LinkedHashMap lHashMap = new LinkedHashMap();
    
    //add key value pairs to LinkedHashMap
    lHashMap.put("1","One");
    lHashMap.put("2","Two");
    lHashMap.put("3","Three");
    
    /*
      To check whether a particular value exists in LinkedHashMap use
      boolean containsValue(Object key) method of LinkedHashMap class.
      It returns true if the value is mapped to one or more keys in the
      LinkedHashMap otherwise false.
      */
    
    boolean blnExists = lHashMap.containsValue("Two");
    System.out.println("Two exists in LinkedHashMap ? : " + blnExists);
    }
    }
    
    /*
    Output would be
    Two exists in LinkedHashMap ? : true
    */

Thursday, August 11, 2011

Check if a particular key exists in Java LinkedHashMap

package org.best.example;
   
        /*
      Check if a particular key exists in Java LinkedHashMap example
      This Java Example shows how to check if LinkedHashMap object contains a particular
      key using containsKey method of LinkedHashMap class.
    */
    
    import java.util.LinkedHashMap;
    
    public class CheckKeyOfLinkedHashMapExample {
    
    public static void main(String[] args) {
    
    //create LinkedHashMap object
    LinkedHashMap lHashMap = new LinkedHashMap();
    
    //add key value pairs to LinkedHashMap
    lHashMap.put("1","One");
    lHashMap.put("2","Two");
    lHashMap.put("3","Three");
    
    /*
      To check whether a particular key exists in LinkedHashMap use
      boolean containsKey(Object key) method of LinkedHashMap class.
      It returns true if the LinkedHashMap contains mapping for specified key
      otherwise false.
      */
    
    boolean blnExists = lHashMap.containsKey("3");
    System.out.println("3 exists in LinkedHashMap ? : " + blnExists);
    }
    }
    
    /*
    Output would be
    3 exists in LinkedHashMap ? : true
    */