Category: Java Collections

Java Collections interview questions and answers for freshers and experienced asked in technical interviews in IT companies for software job | Only Real

What is Java Priority Queue with real time application example?

Java priority queue with example and implementation will be answered. With real time application example program, the priority queue in java will be described. Answer: What is Priority Queue in Java? Priority queue is just like a traditional queue with difference that when a high priority elements come to join a queue, it gets added…

Read the full article

What is Comparable and Comparator in java?

Comparable and comparator in java with examples and explanation for when to use comparator interface and comparable interfaces in java programs. After reading the answer, you will be able to clearly understand comparable and comparator interface and why and when you should choose each one. Answer: The comparable and comparator interface in java programming is used…

Read the full article

Explain vector, time complexity and how vector grows in java

Answer: Vector & time complexity in Java: Vector is a dynamic array that grows or shrinks on run time to accommodate items during addition or deletion. Another important point is, it is synchronized. Supports both Iterator and ListIterator(provides both forward and backward traversal) which are fail-fast iterators. It allows null and duplicates values. And, also…

Read the full article

Java Collections short interview FAQs with answers – Real

List of Java Collection short interview questions and answer asked in software job / technical interviews. This list is helpful for telephonic or face to face technical interviews for freshers and experience developers/professionals. These interview questions are collected from various job interviews   Q-What is difference between Add() and Offer() methods of a queue? Answer…

Read the full article

What are advantages of comparator in Java over comparable?

Answer includes advantages of comparator over comparable interfaces and difference between both in Java collections. Note that advantages  of comparator over coparable and when would you use comparable and comparator interface is a frequently asked java interview question. You may read what is comparable and comparator interface in java collections before reading difference and advantages. Answer: Comparable Vs Comparator…

Read the full article

What is difference between Iterator and ListIterator?

Answer: Here is the differences and advantages of ListIterator over Iterator. 1-Iterator is available to all Collection classes whereas ListIterator is available to only List classes e.g ArrayList, Linkedlist and Vector. Note: Since List classes are also a collection, so, both Iterator and ListIterator are available to List classes. ListIterator can only be used with…

Read the full article

Write a program to sort employees by their experience. Employee class is not allowed to implement any interface.

Answer: For sorting employees we need to use either Comparable or comparator interface. Since, as per question, the Employee class is not allowed to implement any Interface, only option is, to create a new comparator class that implements Comparator interface. And in “compare()” method of comparator class, write the comparison code for the employee class…

Read the full article

What is difference between Arraylist and Vector in java?

Difference between Arraylist and Vector in java in Java Collections. We will be listing four important difference between java arraylist and vector data structures. Note that two differences that thread safety and performance difference should be given extra focus. Java ArrayList Vs Vector ArrayList is not thread safe as its methods are not synchronized, whereas…

Read the full article

Which to prefer add() or offer() to add items in Java Queue?

Both java queue add () and offer () methods are available in the Queue interface and are used to add elements to the queue. Preference will be given to java queue Offer() method. add() internally just call offer() method and does nothing extra. And also, add () method throws exception “java.lang.IllegalStateException: Queue full” at the…

Read the full article

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…

Read the full article