Learn 4 Techniques to PRINT ArrayList Elements in Java with Code Example. To print elements, first we’ll create a String ArrayList and store weekdays name as strings into it and display them using following ways: Here is a string ArrayList. We have created an object of the list and added name of week days using […]
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 […]
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 […]
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 […]
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 – […]
What are advantages of comparator in Java over comparable?
Answer includes advantages of comparator over comparable interfaces in java and the differences between them. As a side note, what’re advantages of comparator over comparable, and when would you use comparable and comparator interface is a frequently asked interview question for java developer profile. You may read what is comparable and comparator interface in java collections before reading the […]
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 […]
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 […]
What is difference between Arraylist and Vector in java?
Difference between Arraylist and Vector in java in Java Collections. I’ll be listing 4 important differences between java arraylist and vector data structures. Java ArrayList Vs Vector ArrayList is not thread safe as its methods are not synchronized, whereas Vector’s methods are synchronized. So, in multithreading environment, we can use Vector without making any extra […]
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 queue’s Offer() method. The add() method internally just call the offer() method and does nothing extra. And also, the add () method throws exception “java.lang.IllegalStateException: Queue […]