1.

What are the basic interfaces of Java Collections Framework and how to instantiate this interface?

Answer»

1. Set

  • Set is a collection, which avoids containing duplicate ELEMENTS.
  • This interface models the mathematical set abstraction and is used to represent sets, such as the deck of cards.
  • Set doesn’t maintain the order.
  • HashSet, LinkedHashSet or TreeSet are the implementations of the Set interface.
  • Set also adds a stronger contract on the behaviour of the equals and hashCode
  • Code SNIPPET: Set set = new HashSet();

2. List

  • The list follows the ordered collection and can contain duplicate elements.
  • One can access any element from its index.
  • The list is somewhat like array however length is changing at runtime.
  • List interface doesn’t provide the thread safety.
  • ArrayList, LinkedList, CopyOnWriteArray are the implementation of List interface.
  • Code Snippet: List list = new ArrayList();

3. Queue

  • Queue interface is a part of collection framework extend the collection interface.
  • A queue is used to insert elements at the end of the queue and removes from the beginning of the queue. It BASED on the FIFO concept.
  • The queue is inherited from the Collection framework, so it supports all the method like insertion and deletion.
  • ArrayBlockingqueue and PriorityQueue are the most widely used implementation classes.
  • public class PriorityQueue<E&GT; extends AbstractQueue<E> implements Serializable  
  • Code Snippet: Queue queue = new PriorityQueue();



Discussion

No Comment Found