Header Ads

  • Breaking Now

    Collection APIs Related Interview Questions

    You may like to read following posts before going through the list of questions mentioned on Collections APIs in this post:
    1. Questions on java.util.* and java.io.* package
    2. Core Java Questions With Short Answers
    Q. How will you synchronize a collection? A. The code sample given below elaborates on synchronization of various collection objects:
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.SortedMap;
    import java.util.SortedSet;
    import java.util.TreeMap;
    import java.util.TreeSet;
    
    
    public class CollectionSynchronization {
    public static void main(String[] args) {
    Collection collection = Collections.synchronizedCollection(new ArrayList());
    List list = Collections.synchronizedList(new ArrayList());
    Set set = Collections.synchronizedSet(new HashSet());
    Map map = Collections.synchronizedMap(new HashMap());
    SortedMap sortedMap = Collections.synchronizedSortedMap(new TreeMap());
    SortedSet sortedSet = Collections.synchronizedSortedSet(new TreeSet());
    }
    }
    
    Q. Why is it advisable to override hashCode() method as well, when equals() method is overridden? A. When equals() method is overridden, then each object must have same hashcode for pair of objects which have equals() method overridden.If the .hashCode() method of your custom class doesn't work the same way your .equals() method works, the results of your code will be erroneous. Q. If a collection object has duplicate values then how will you convert into a collection having unique values? A. Here goes the listing:
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeSet;
    
    
    public class CollectionExample {
    public static void main(String[] args) {
     ArrayList list = new ArrayList();
     list.add("Apple");
     list.add("Apple");
     list.add("Apple");
     list.add("Mango");
     list.add("Guvava");
     list.add("Pineapple");
     Set set = new TreeSet(list);
     Iterator iterator = set.iterator();
       while (iterator.hasNext()) {
      String str= (String)iterator.next();
      System.out.println(""+str);
      }
    
     
    
    }
    }
    
    Output:
    
    Apple
    Guvava
    Mango
    Pineapple
    
    Q. How will you address the scenario given below to decide on collection object to work with: - Multiple keys with multiple values - Multiple keys with single value - Single key with multiple values - Single key with single value A. In the given scenario, HashMap will be the best choice. As long as the keys are unique, one can enter multiple values in an ArrayList and associate this with a key inside HashMap and retrieve those multiple values from unique key. Q.How to get a TreeSet from a TreeMap? A.The possibility of creating a TreeSet from the keyset of TreeMap.Check the example given as below:
    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeMap;
    import java.util.TreeSet;
    
    
    public class CollectionExample {
    public static void main(String[] args) {
        TreeMap treeMap = new TreeMap();
        treeMap.put("1", "Apple");
        treeMap.put("2", "Mango");
        treeMap.put("3", "Guvava");
        treeMap.put("4", "Banana");
        treeMap.put("5", "Grapes");
    
        Set set = new TreeSet(treeMap.keySet());
        Iterator iterator = set.iterator();
    
        while (iterator.hasNext()) {
            String str = (String) iterator.next();
            System.out.println("" + str);
        }
    }
    }
    
    Output:
    1
    2
    3
    4
    5
    
    Q.How a TreeSet different from HashSet? A.One line answer is : TreeSet contains-> Unique and Ordered/Sorted values(as per natural order or based on Comparator implemented logic).It is backed by TreeMap. While HashSet contains-> Unique values but not Ordered/Sorted The example given below will explain the same:
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeMap;
    import java.util.TreeSet;
    
    
    public class CollectionExample {
      public static void main(String[] args) {
          Set hashSet = new HashSet();
          Set treeSet = new TreeSet();
    
          addElements(hashSet);
          addElements(treeSet);
    
          System.out.println("HashSet:"+hashSet);
          System.out.println("TreeSet"+treeSet);
      }
    
      private static void addElements(Set aTreeSet) {
          aTreeSet.add(Integer.valueOf(6));
          aTreeSet.add(Integer.valueOf(2));
          aTreeSet.add(Integer.valueOf(2));
          aTreeSet.add(Integer.valueOf(4));
          aTreeSet.add(Integer.valueOf(7));
          aTreeSet.add(Integer.valueOf(7));
          aTreeSet.add(Integer.valueOf(1));
      }
    }
    
    The Output is:
    
    HashSet:[2, 4, 6, 1, 7]
    TreeSet[1, 2, 4, 6, 7]
    
    Q.A practical problem, I want to choose a collection object which allows me to put only unique values inside it and in the same order as it is coming out of a database, here data from the database can contain duplicate values.What collection object will you choose? A.Choose:
    LinkedHashSet uniqueData = new LinkedHashSet(dataFromDB);
    
    Q. What is the initial capacity of Vector? A. 10. Try out the code given below:
    import java.util.Set;
    import java.util.Vector;
    
    
    public class CollectionExample {
       public static void main(String[] args) {
           Vector vector = new Vector();
           int capacity = vector.capacity();
           System.out.println("Capacity:" + capacity);
       }
    }
    
    The Output is:
    
    Capacity:10
    
    An article on JavaWorld on Collections' API

    Post Top Ad

    Post Bottom Ad