File Name: collections interview questions and answers in java api.zip
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.
Yet based on my previous interviews, I am attempting to put as many as possible GOOD java collection interview questions you must know. I am aiming both beginners and senior level questions, so bear with me if you found some questions too basic because they might be useful for some junior developers.
By definition, a collection is an object that represents a group of objects. Like in set theory, a set is group of elements. Easy enough!! Prior to JDK 1. Later from JDK 1. Finally, the collections framework was designed and developed primarily by Joshua Bloch, and was introduced in JDK 1. As shown in above image, collection framework has one interface at top i. It is extended by Set, List and Queue interfaces.
Then there are loads of other classes in these 3 branches which we will learn in following questions. Framework also consist of Map interface, which is part of collection framework.
We will see the reason in 4th question in this question bank. Extending an interface simply means that you are creating a subtype of interface, in other words a more specialized behavior and Collection interface is not expected to do what Cloneable and Serializable interfaces do. Another reason is that not everybody will have a reason to have Cloneable collection because if it has very large data, then every unnecessary clone operation will consume a big memory. Beginners might use it without knowing the consequences.
Another reason is that Cloneable and Serializable are very specialized behavior and so should be implemented only when required. For example, many concrete classes in collection implement these interfaces. So if you want this feature. Collection has a method add Object o. Map can not have such method because it need key-value pair. There are other reasons also such as Map supports keySet, valueSet etc.
Collection classes does not have such views. Due to such big differences, Collection interface was not used in Map interface, and it was build in separate hierarchy. This ordering is a zero based index. It does not care about duplicates. Apart from methods defined in Collection interface, it does have its own methods also which are largely to manipulate the collection based on index location of element.
These methods can be grouped as search, get, iteration and range view. All above operations support index locations. Read more about them in java documentation. This is more of a programmatic question which is seen at beginner level.
The intent is to check the knowledge of applicant in Collection utility classes. For now, lets learn that there are two utility classes in Collection framework which are mostly seen in interviews i. Collections and Arrays. Collections class provides some static functions to perform specific operations on collection types.
And Arrays provide utility functions to be performed on array types. Please not that this function is not specific to String class, it will return List of element of any type, of which the array is.
This question is just like above to test your knowledge of Collections utility class. Use it reverse method to reverse the list. It models the mathematical set in set theory. Set interface is like List interface but with some differences. First, it is not ordered collection. So no ordering is preserved while adding or removing elements.
It does not support duplicate elements. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ. Two Set instances are equal if they contain the same elements. Based on above reasons, it does not have operations based on indexes of elements like List. It only has methods which are inherited by Collection interface. Read more on related java documentation.
You must know that HashMap store key-value pairs, with one condition i. In HashSet class, a map declaration is as below:. See declaration above. I will highly suggest you to read this post: How HashMap works in java? This post will help you in answering all the HashMap related questions very easily. As you see, There is no null check in add method in previous question.
TreeSet uses the same concept as HashSet for internal logic, but uses NavigableMap for storing the elements. NavigableMap is subtype of SortedMap which does not allow null keys. So essentially, TreeSet also does not support null keys.
Map interface is a special type of collection which is used to store key-value pairs. It does not extend Collection interface for this reason. This interface provides methods to add, remove, search or iterate over various views of Map. IdentityHashMap is similar to HashMap except that it uses reference equality when comparing elements.
IdentityHashMap class is not a widely used Map implementation. IdentityHashMap is designed for use only in the rare cases wherein reference-equality semantics are required. WeakHashMap is an implementation of the Map interface that stores only weak references to its keys. Storing only weak references allows a key-value pair to be garbage collected when its key is no longer referenced outside of the WeakHashMap.
Once such a key is discarded it can never be recreated, so it is impossible to do a look-up of that key in a WeakHashMap at some later time and be surprised that its entry has been removed.
A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates. This class obeys the same functional specification as Hashtable, and includes versions of methods corresponding to each method of Hashtable. However, even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access.
This class is fully interoperable with Hashtable in programs that rely on its thread safety but not on its synchronization details. Read more about how ConcurrentHashMap interview questions. The most important question which is most likely to be seen in every level of job interviews. You must be very clear on this topic. Answer to this question is very large and you should read it my post: How HashMap works?
For now, lets remember that HashMap works on principle of Hashing. To store such structure, it uses an inner class Entry :. Here key and value variables are used to store key-value pairs. Whole entry object is stored in an array. Another good question usually followed up after answering how hashmap works. Well, the most important constraint is you must be able to fetch the value object back in future.
Otherwise, there is no use of having such a data structure. If you understand the working of hashmap, you will find it largely depends on hashCode and equals method of Key objects.
So a good key object must provide same hashCode again and again , no matter how many times it is fetched. Similarly, same keys must return true when compare with equals method and different keys must return false. For this reason, immutable classes are considered best candidate for HashMap keys. Read more : How to design a good key for HashMap? HashMap is well known class and all of us know that. So, I will leave this part by saying that it is used to store key-value pairs and allows to perform many operations on such collection of pairs.
TreeMap is special form of HashMap. It maintains the ordering of keys which is missing in HashMap class. The default ordering can be override by providing an instance of Comparator class, whose compare method will be used to maintain ordering of keys.
Please note that all keys inserted into the map must implement the Comparable interface this is necessary to decide the ordering. Furthermore, all such keys must be mutually comparable: k1. If the user attempts to put a key into the map that violates this constraint for example, the user attempts to put a string key into a map whose keys are integers , the put Object key, Object value call will throw a ClassCastException.
Perhaps most easy question. List is collection of elements where as map is collection of key-value pairs.
These interview questions seems to be too common and appear in almost many interviews and that's the reason I never asked so called frequently asked or popular interview question, instead I ask something which is not so common like: 1 What is CopyOnWriteArrayList, how it is different than ArrayList and Vector? Generics, dynamic growing 7 What is BlockingQueue, how it is different than other collection classes flow control 8 How do you iterator over Synchronized HashMap, do you need to lock iteration and why? If anyone wants to answer this question , they must required a very good understanding of Java Collection framework which is whole purpose of these interview questions. By the way can you please share pdf version of your java collection interview for download? I think Collection interview questions listed here are very much appear in Capegemini or Tech Mahindra interview. Answer is : If you are using any custom object in Map as key, you need to override equals and hashCode method, and make sure they follow there contract.
For more java articles ,Click here to Subscribe JavaHungry. Java Hungry. Java developers tutorials and coding.
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. Yet based on my previous interviews, I am attempting to put as many as possible GOOD java collection interview questions you must know. I am aiming both beginners and senior level questions, so bear with me if you found some questions too basic because they might be useful for some junior developers. By definition, a collection is an object that represents a group of objects. Like in set theory, a set is group of elements. Easy enough!!
Check this out. Use this list of java Interview Questions for interview preparation, or just peruse! Topics: Tech Recruitment , Skills Assessment.
Java Collections is the type of the set of classes that help to implement the common reusable data. Java Collections framework is one of the most important parts of the Java programming language. Java Collections is the type of containers that help you group all the types of multiple items in the single units. The collection in Java is mainly a type of framework that helps to provide an architecture to store and manipulate all the other group of the objects. Java Collections will help you to achieve all the operations that one can perform on the data.
Java Collections interview questions and answers for experienced/beginner programmers. Java Stream API for collection classes for supporting sequential as well as parallel processing Download java collections pdf for more information.
Here I am listing some important java collections interview questions and answers to help you in the interview. Collections are used in every programming language and initial java release contained few classes for collections: Vector , Stack , Hashtable , Array. But looking at the larger scope and usage, Java 1.
Arrays are not dynamic. Once an array of a particular size is declared, the size cannot be modified. To add a new element to the array, a new array has to be created with bigger size and all the elements from the old array copied to new array.
In Java, collection interview questions are most asked by the interviewers. Here is the list of the most asked collections interview questions with answers. Collection Framework is a combination of classes and interface, which is used to store and manipulate the data in the form of objects.
Your email address will not be published. Required fields are marked *