Questions on java collection

When we call put method by passing key-value pair, HashMap uses Key hashCode with hashing to find out the index to store the key-value pair. The Entry is stored in the LinkedList, so if there are already existing entry, it uses equals method to check if the passed key already exists, if yes it overwrites the value else it creates a new entry and store this key-value Entry.

Questions on java collection

If you like the page or have any questions, feel free to comment at end of the page. Collections in Java offer some of the most comprehensive suites of built-in Data structures. Often this is the most common topic amongst interviewers. Good foundation and understanding of java collections help developer in building efficient programs.

Q1 What is the difference between an ArrayList and a vector? Ans Synchronization - ArrayList is not thread-safe whereas Vector is thread-safe. In Vector class each method like addget int i is surrounded with a synchronized block, thus making Vector class thread-safe.

This means that the collection is of fixed size. When the collection is coming to near it's max capacity it dynamically resizes itself. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

Performance - Since Vector is thread-safe, the performance is slower than ArrayList. Q2 How can an ArrayList be synchronized without using Vector? Ans Arraylist can be synchronized using: Now I want the list to be sorted on the basis of the employeeID of Employee class.

Java Collections Interview Questions & Answers | Java2Blog

What are the steps? One way to achieve is by using Comparable interface. Implement Comparable interface to the Employee class and override the compareTo Object obj method in which compare the employeeID.

Now consider that Employee class is a jar file. Since Comparable interface cannot be implemented, create Comparator and override the compare Object obj, Object obj1 method.

Ans Both collections implements Map. Both collections store value as key-value pairs.

Questions on java collection

The key differences between the two are: Hashmap is not synchronized in nature but hashtable is thread-safe. This means that in a multithreaded application, only one thread can get access to a hashtable object and performs an operation on it.

Hashmap doesn't guarantee such behavior and is not used in multithreaded environment. Hashmap is traversed using an iterator, hashtable can be traversed by enumerator or iterator.

Iterator in hashmap and hashtable is fail fast but enumerator in hashtable is fail safe. HashMap permits null values and only one null key, while Hashtable doesn't allow key or value as null.

Since hashtable is synchronized, it is relatively slower in performance than hashmap Q5 What are the classes implementing the List interface? Ans There are three implementations of the List interface: It is a resizable array implementation. The size of the ArrayList can be increased dynamically also operations like add, remove and get can be formed once the object is created.

It also ensures that the data is retrieved in the manner it was stored. The ArrayList is not thread-safe. It is the thread-safe implementation of ArrayList. The methods are wrapped around a synchronized block. It is faster than ArrayList for add and remove operation as time complexity is O 1.

But for searching or getting an element the average time complexity is O n where n is the size of the list. Q6 Which all classes implement the Set interface? Ans A Set is a collection that contains no duplicate elements. More formally, a set contains no pair of elements e1 and e2 such that e1.

SortedSet - It is an interface which extends Set.Java Collections Interview Questions Why do we need Collections in Java? What are the important methods that are declared in the Collection Interface?

When does a Java collection throw UnsupportedOperationException? All Java Collections extend Collection interface. So, they have to implement all the methods in the Collection interface.

Questions on java collection

Collection is an interface while Collections is a java class, both are present in plombier-nemours.com package and part of java collections framework. Q4 Which collection classes are synchronized or thread-safe? Java Collections is a topic often brought up on technical interviews for Java developers.

This article reviews some important questions that are asked most often and may be tricky to get right. List is an ordered collection, and its elements can be accessed by their index in the list.

Set is an.

Java Collections Interview Questions and Answers - codeNuclear

34 Java Collections Interview Questions. In Java, collection interview questions are most asked by the interviewers. Here is the list of the most asked collections interview questions with answers. Top 50 java collections interview questions covering about internals of HashMap, ArrayList, Set, Data Structure used, performance of collections.

Without argument, java collections is one of the most important area where you will be tested in any position whether junior or senior. The scope is so much wide, that its almost impossible to cover all the questions.

Java Collections Interview Questions & Answers | Java2Blog