Java Collections — List, Queue, Sets

  1. Need contiguous memory to store elements
  2. Only homogenous type data can be stored
  3. It is compulsory to known the number of elements that we would inserting to array
  4. Size of array is fixed
  • Reducing the effort required to write the code by providing useful data structures and algorithms
  • Java collections provide high-performance and high-quality data structures and algorithms thereby increasing the speed and quality
  • Decreases extra effort required to learn, use, and design new API’s
  • Supports reusability of standard data structures and algorithm

Java Collection Framework Hierarchy

Java collections: List

  1. ArrayList
  2. LinkedList
  3. Vectors
  1. void add(int index, E element) -> It is used to insert the specified element at the specified position in a list.

Java collections: Queue

  1. boolean add(object) -> Inserts the specified element into the queue and returns true if it is a success.
  2. boolean offer(object)-> Inserts the specified element into this queue.
  3. Object remove() -> Retrieves and removes the head of the queue.
  4. Object poll()-> Retrieves and removes the head of the queue, or returns null if the queue is empty.
  5. Object element() ->Retrieves, but does not remove the head of the queue.
  6. Object peek()-> Retrieves, but does not remove the head of this queue, or returns null if the queue is empty.

PriorityQueue Time Complexity

  • In Java programming, Java Priority Queue is implemented using Heap Data Structures and Heap has O(log(n)) time complexity to insert and delete element.
  • Offer() and add() methods are used to insert the element in the in the priority queue java program.
  • Poll() and remove() is used to delete the element from the queue.
  • Element retrieval methods i.e. peek() and element(), that are used to retrieve elements from the head of the queue is constant time i.e. O(1).
  • contains(Object)method that is used to check if a particular element is present in the queue, have leaner time complexity i.e. O(n).

Java Collections: Sets

  1. boolean addAll(Collection c) -> Add all the elements in the specified collection to this set.
  2. boolean contains(Object o) -> Returns true if the set contains the specified element.
  3. boolean isEmpty() -> Returns true if this set contains no elements.
  4. boolean remove(Object o) -> Remove the specified element from the set.
  5. void add(Object o) -> Add the specified element to the set.
  6. void clear() Removes all the elements from the set.
  7. Object clone()-> Return a shallow copy of this TreeSet instance.

Applications of Set

  • We can use Set interface to maintain data uniqueness.
  • Set is used to avoid data redundancy.

Complexity Of add Method :

  • HashSet add method have constant time complexity O(1).
  • TreeSet add method have constant time complexity O(log(n)).
  • LinkedHashSet add method have constant time complexity O(1).

Complexity Of remove Method :

  • HashSet remove method have constant time complexity O(1).
  • TreeSet remove method have constant time complexity O(log(n)).
  • LinkedHashSet remove method have constant time complexity O(1).

HashSet vs TreeSet vs LinkedHashSet:

  1. HashSet :
  • HashSet Use hashtable to store elements.
  • Elements are not stored in sorted order(unsorted).
  • add,remove,contain method have constant time complexitiy O(1).
  1. TreeSet :
  • TreeSet Use RedBlack tree to store elements.
  • Elements are stored in sorted order (sorted).
  • add, remove, and contains methods has time complexity of O(log (n)).
  1. LinkedHashSet :
  • LinkedHashSet is between HashSet and TreeSet. It is implemented as a hash table with a slinked list running through it.
  • The time complexity of basic methods is O(1).

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A pragmatic architecture — CQRS 4/5

Benefits of Agile Software Development for Enterprise

How To Learn And Master A Programming Language

London vs Chicago

Destiny 2 Project Part 2

Automation on EBS volumes using AWS Boto3 & Lambda

Using python script and pip packages in SQL Server

What is Model Browser in Entity Framework?.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kotasaisrikanth

Kotasaisrikanth

More from Medium

Chapter 01 — Overview of Java

Spring

Constructors and garbage collection — Java

OOPS CONCEPT OF JAVA