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, interfaces and algorithms to handle collection (or a group) of related objects as a single unit. They are very useful for managing complex data structures and provide a wide range of data structures and methods for manipulating data, and are highly optimized for performance and memory usage. There are many built-in methods for performing common operations on data structures, such as sorting, searching, filtering, and iteration, which allows saving time and effort when implementing complex algorithms. They are type-safe, with some of them thread-safe, providing concurrent access to shared data.


  • Interfaces

  • List
  • The java.util.List interface is a subtype of the java.util.Collection interface, and part of java.util package. It represents an ordered list of objects. The elements of list can be accessed in a specific order, and by index as well, which starts from 0. The List interface is implemented by many Java classes, such as ArrayList, LinkedList, and Vector, providing their own implementations of List interface methods. It can be even your own class which can implement List interface to provide custom implementations of its various methods.


  • Set
  • The java.util.Set interface is a subtype of the java.util.Collection interface. It represents set of unordered and non-repeatable unique objects. This interface is implemented by several Java classes including HashSet, TreeSet, and LinkedhashSet.


  • 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 i.e. First-In-First-Out (FIFO) principle. Queues are commonly used in multi-threaded applications and in algorithms that require a first-in-first-out order, such as breadth-first search (graph traversal algorithm) and job scheduling.


  • 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. A Map cannot contain duplicate keys, and each key can map to at most one value. The Map interface is implemented by several classes in Java, including HashMap, TreeMap, LinkedHashMap, and ConcurrentHashMap.


  • 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.