OOPs Concepts

Language Elements

Exceptions

Java String Class

Java Array Class

Collections

Java Example Codes

Core Java Interview Questions

Collections in Java


Java Collections API provide set of classes and interfaces to handle collection (or a group) of objects.


  • Interfaces

  • List
  • The java.util.List interface is a subtype of the java.util.Collection interface. It represents an ordered list of objects. The elements of list can be accessed in a specific order, and by index as well.


  • Set
  • The java.util.Set interface is a subtype of the java.util.Collection interface. It represents set of non-repeatable objects.


  • Queue
  • The java.util.Queue interface is a subtype of the java.util.Collection interface. It represents an ordered list of objects just like a List, but its intended use is slightly different. A queue is designed to have elements inserted at the end of the queue, and elements removed from the beginning of the queue


  • Map
  • The java.util.Map interface represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface.


  • Implementations

  • Linked List
  • -  LinkedList is implemented using doubly linked list, maintaining references to the previous and next element at each node in the list.
    -  It can be used both as List and Queue.
    -  LinkedList class can contain duplicate elements.
    -  It is non synchronized.The LinkedList in java can have any type of elements including null and duplicates.
    -  Elements can be inserted and can be removed from both the ends and can be retrieved from any arbitrary position.


  • Array List
  • - The class ArrayList provides resizable-array, i.e. items can be added and removed from the list.
    - An ArrayList is a dynamic data structure, so it can be used when there is no upper bound on the number of elements.


  • Treet Set
  • -  TreeSet sorts the elements in the ascending (unlike while HashSet doesn't maintain any order).
    - TreeSet allows null element (HashSet doesn't).
    - Like most of the other collection classes this class is also not synchronized (however it can be).


  • Hash Map
  • - Hash Map can be used to store key-value pairs.
    -  There cannot be duplicate keys in a Map and each key maps to at most one value.
    -  It can be used to store multiple values for the same key.
    -  HashMap is unsynchronized and Hashtable is synchronized. HashMap is the faster one between the two.


  • Tree Map
  • -  To maintain map keys in an ordered fashion (asending orderof keys), one can use TreeMap.
    -  TreeMap is unsynchronized collection, so it is not suitable for thread-safe operations until unless synchronized explicitly.





    Copyright © by Zafar Yasin. All rights reserved.