Java Collections

ADT(Abstract Data Type) Vs Data Structure Interview question

Answer: ADT (Abstract Data type) represents a set of particular behaviors. for example, LIST represents a sequence of values, a QUEUE represents a FIFO(First in First Out) logic on additions/deletions operations etc. ADTs are abstracted from the implementation detail of operations. For example, Add() & Remove() operations of LIST abstract data type does not have […]

What is time complexity for offer, poll and peek methods in priority queue Java ?

Answer: Time complexity for the methods offer & poll is O(log(n)) and for the peek() it is Constant time O(1) of java priority queue. SIDE NOTE: 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 […]

If we try to insert duplicate values in a “Set”, what will happen? Do we get any complier error?

Just it doesn’t add duplicate values. Boolean add(E e) – Adds the specified element to this set if it is not already present (optional operation). As, add() method returns Boolean and on adding duplicates it will return false. Below java source code example works fine and JVM (Java Virtual Machine) doesn’t complain. If we insert duplicate values to the Set, we don’t get any compile time or run […]

Scroll to top