Collection Framework Interview Questions

For the post of Senior Java Developer Collection framework questions are most common questions which are asked in interviews.

In this post I am going to cover all important and most repeated collection framework questions. Lets get started.

 

Q. What is the Collection framework in Java?

Collection Framework consist of classes and interface, which are used to store and manipulate the data in the form of objects.

It provides various classes such as ArrayList, LinkedList, Vector, Stack, and HashSet, etc. and interfaces such as List, Queue, Set, etc. for this purpose.

Collections are growable in nature. i.e. Based on our requirement we can increase or Decrease the size.

Collections can hold both homogeneous & Heterogeneous elements.

Every Collection class is implemented based on some standard data structure.

 

Q. Arrays Vs Collections?

  • Arrays are always of fixed size, i.e., a user can not increase or decrease the length of the array      according to their requirement, but Collections are growable in nature, i.e. based on              requirement size can be changed dynamically.
  • Arrays can only store homogeneous or similar type objects, but in Collection, heterogeneous    objects can be stored.
  • There is no underlying Data structure in Arrays hence it cannot provide in built or ready-made    methods for user requirements such as sorting, searching etc. but Collection includes      readymade  or built in  methods to use.
  • Arrays can hold both objects and primitives whereas Collections can hold only objects.
  • With respect to memory Arrays are not recommended but Collections are recommended to        use.
  • With respect to performance Arrays are recommended and collections are not recommended to  use.

Q. What are different interfaces used in Collection framework?

 Following are the key interfaces in collection frameworks.

      1.Collection

      2.List

      3.Set

      4.Queue

      5.Map


1. Collection :

Collection interface defines the most common methods which are applicable for any Collection object.

In general collection interface is considered as root interface of Collection Framework.

Every collection must implement this interface.

Java Package for Collection interface is java.util.Collection.

public interface Collection<E>extends Iterable

Where <E> represents that this interface is of Generic type.


2. List :

List interface extends the Collection interface.

It is an ordered collection of objects. 

It contains duplicate elements.

It also allows random access of elements.

Java Package is java.util.List

public interface List<E> extends Collection<E> 


3. Set :

Set is child interface of Collection interface

In Set insertion order is not preserved.

Duplicate elements are not allowed in Set.

Java Package is java.util.Set

public interface Set<E> extends Collection<E> 

 

4. Queue :

Queue is also a child interface of Collection interface.

In Queue elements are stored in the form of First In First Out(FIFO)

Java Package is java.util.Queue

public interface Queue<E> extends Collection<E> 

 

5. Map :

Map is not a child interface of Collection interface, In other words Map interface does not implement the Collection interface.

A Map represents elements in the form of  a key and value pair.

It can only contain a unique key but can have duplicate elements.

SortedMap and NavigableMap are the interfaces which implements Map interface.

Java Package is java.util.Map

 

Q. Array Vs ArrayList



Q. ArrayList Vs LinkedList




Q. ArrayList Vs Vector




Q. Collection Vs Collections.



Q List Vs Set



Q Set Vs Map


Q HashSet Vs HashMap



Q HashMap Vs Hashtable



Q. HashSet Vs TreeSet



Q. HashMap Vs TreeMap



Q Comparable Vs Comparator



Q Explain Equals and Hashcode contract in Java.

 

First lets understand both methods individually.

equals():

The equals() method is used to check whether two objects are the same or not.

equals() method is present in Object class and its default implementation is that JVM checks memory address of 2 objects to check equality. If both objects are pointing to same location then they are equal.

It needs to be overridden if we want to check the objects based on the property. 

For example, Student is a class that has 2 data members: rollNo and name. However, we want to check the equality of Student based on its field rollNo. Then, we need to override the equals() method.

 

hashCode():

The hashCode() method is also present in Object Class.

The hashCode() method returns a hash code value (an integer number).

By default the hashCode() method returns an integer that represents the internal memory address of the object.

The hashCode() method returns the same integer number if two keys are identical.

We can also override hashCode() method based on the criteria we want for equality of two objects.


Equals and Hashcode Contract:

If we are overriding equals() method, Then we must override hashCode() method as well.

In short, if two objects are equal, their  hashCode() method must return same value.

In above example of equals() method, we have overridden equals() method to check equality of 2 objects based on their rollNo in other words 2 objects of student class are said to be equal if their roll numbers are same.

Now as per Equals and HashCode Contract in JAVA if 2 objects are equal then they should return same hashCode. as per the default implementation of hashCode() method hashcodes are calculated from internal memory address of that object, So in this case we must override hashCode() method as well in order to work in accordance with Equals and HashCode contract.

Please execute following code in order to understand Equals HashCode contract.

package com.nomaan.test;

public class Student {

               private int rollNo;

               private String name;

               public Student(int i, String name) {

                              this.name = name;

                              this.rollNo = i;

               }

               public boolean equals(Student s) {

        // if roll numbers are equal return true else return false

                              if (this.rollNo == s.rollNo)

                                            return true;

                              else

                                             return false;

               }

 

               public int hashCode() { 

                                                            return rollNo; 

                                                       }

               public static void main(String[] args) {

                              Student s1 = new Student(1, "Nomaan");

                              Student s2 = new Student(1, "Shripad");

                              System.out.println(s1.equals(s2));

                              System.out.println(s1.hashCode());

                              System.out.println(s2.hashCode());

               }

}

 

In Above program We have overridden both equals and hashCode() methods. Both s1 and s2 objects are equal based on our overridden criteria and it satisfy equals and hashCode contract as for both objects hasCode is also same which we achieved by overriding hashCode method.

 

Output of above program is as follows:

 

true

1

1

 

In above program if we comment hashCode method() then JVM will return different hashCodes for both objects. in this case it will not be in accordance with the contract.

 

Output of above program without overriding hashCode() method is as follows

 

true

292938459

917142466

 

I hope we are clear with Equals and HashCode Contract in JAVA.




Comments

Popular posts from this blog

Object Oriented Programming Concepts