» » » Top 20 collection framework interview questions and answers in java

Java collections interview questions:


1.Why Map interface doesn’t extend Collection interface?

  • Set is unordered collection and does not allows duplicate elements.
  • List is ordered collection allows duplicate elements.
  • Where as Map is key-value pair.
  • It is viewed as set of keys and collection of values.
  • Map is a collection of key value pairs so by design they separated from collection interface.


2.What is difference between HashMap and Hashtable?

  • Synchronization or Thread Safe 
  • Null keys and null values 
  • Iterating the values 
  •  Default Capacity 
hashmap vs hashtable

    Click here for the
    Differences between HashMap and Hash-table

    3.Differences between comparable and comparator?

    • Comparable Interface is actually from java.lang package.
    • It will have a method compareTo(Object obj)to sort objects
    • Comparator Interface is actually from java.util package.
    • It will have a method compare(Object obj1, Object obj2)to sort objects
    Read more :  comparable vs comparator

    4.How can we sort a list of Objects?

    •  To sort the array of objects we will use  Arrays.sort() method.
    • If we need to sort collection of object we will use Collections.sort().

    5.What is difference between fail-fast and fail-safe?

    • Fail fast is nothing but immediately report any failure. whenever a problem occurs fail fast system fails. 
    • in java Fail fast iterator while iterating through collection of objects sometimes concurrent modification exception will come there are two reasons for this.
    • If one thread is iterating a collection and another thread trying to modify the collection.
    • And after remove() method call if we try to modify collection object



    6. What is difference between Iterator ,ListIterator and Enumeration?

    • Enumeration interface implemented in java 1.2 version.So Enumeration is legacy interface.
    • Enumeration uses elements() method.
    • Iterator is implemented on all Java collection classes.
    • Iterator uses iterator() method.
    • Iterator can traverse in forward direction only.
    • ListIterator is implemented only for List type classes
    • ListIterator uses listIterator() method.
    Read more :  

    What is difference between Iterator ,ListIterator and Enumeration?

    7.What is difference between Set and List in Java?

    • A set is a collection that allows unique elements.
    • Set does not allow duplicate elements
    • Set allows only one null value.
    • Set having classes like :
    • HashSet
    • LinkedHashSet
    • TreeSet
    • List having index. and ordered  collection
    • List allows n number of null values.
    • List will display Insertion order with index.
    • List having classes like :
    • Vector
    • ArrayList
    • LinkedList

    8.Differences between arraylist and vector?

    • Vector was introduced in  first version of java . that's the reason only vector is legacy class.
    • ArrayList was introduced in java version1.2, as part of java collections framework.
    • Vector is  synchronized. 
    • ArrayList is not synchronized.

    9.What are the classes implementing List interface?

    • ArrayList     
    • LinkedList     
    • Vector

    10. Which all classes implement Set interface ?

    • HashSet
    • LinkedHashSet
    • TreeSet

    11.How to make a collection thread safe?

    • Vector, Hashtable, Properties and Stack are synchronized classes, so they are thread-safe and can be used in multi-threaded environment.
    • By using Collections.synchronizedList(list)) we can make list classes thread safe.
    • By using 
      java.util.Collections.synchronizedSet()  we can make set classes thread safe.

    12.Can a null element added to a TreeSet or HashSet?

    • One null element can be added to hashset.
    • TreeSet does not allow null values

    13. Explain Collection’s interface hierarchy?


    Collections interview Questions and answers java

    14.Which design pattern Iterator follows?

    • Iterator design pattern

    15.Which data structure HashSet implements

    • Hashset implements hashmap internally.

    16.Why doesn't Collection extend Cloneable and Serializable?

    • List and Set and queue extends Collection interface.
    • SortedMap extends Map interface.

    17.What is the importance of hashCode() and equals() methods? How they are used in Java?

    • equals() and hashcode() methods defined in "object" class. 
    • If equals() method return true on comparing two objects then hashcode() of those two objects must be same.

    18.What is difference between array & arraylist?

    • Array is collection of similar type of objects and fixed in size.
    • Arraylist is collection of homogeneous and heterogeneous elements.

    19.What is the Properties class?

    • Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key and the value is String.

    20.How to convert a string array to arraylist?

    • ArrayList al=new ArrayList( Arrays.asList( new String[]{"java", "collection"} ) ); 
    •  arrayList.toArray(); from list to array


    22.Java Collection framework programming interview questions 



    1. Print prime numbers? 
    2. Java Program Find Second highest number in an integer array 
    3. Java Interview Program to find smallest and second smallest number in an array 
    4. Java Coding Interview programming Questions : Java Test on HashMap  
    5. Constructor chaining in java with example programs 
    6. Swap two numbers without using third variable in java 
    7. Find sum of digits in java 
    8. How to create immutable class in java 
    9. AtomicInteger in java 
    10. Check Even or Odd without using modulus and division  
    11. String Reverse Without using String API 
    12. Find Biggest substring in between specified character
    13. Check string is palindrome or not?
    14. Reverse a number in java? 
    15. Collections interview questions and programs
     For more interview programs: Top 40 java interview programs


    Instance Of Java

    We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
    «
    Next
    Newer Post
    »
    Previous
    Older Post

    3 comments for Top 20 collection framework interview questions and answers in java

    1. Nice sharing

      Very useful detailed information on Java inerview Q and A. especially good and to the point wise elaboration of each answers included in this blog. I am searching such Java questions and answers on google, I found your blog. Nice one. Now Bookmarking for my future use.

      Thanks .

      ReplyDelete
    2. best site for interview preparation.

      ReplyDelete

    Select Menu