Java Collection Interview Questions And Answers PdfBy PlatГіn M. In and pdf 27.03.2021 at 03:57 3 min read
File Name: java collection interview questions and answers .zip
For more java articles ,Click here to Subscribe JavaHungry. Java Hungry. Java developers tutorials and coding.
- java interview questions and answers
- Top 80 Java Collections Interview Questions & Answers
- Top 40 Java collection interview questions and answers
- Java Collections Interview Questions & Answers
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.
Here are Java Collections Interview Questions for fresher as well as experienced candidates to get their dream job. A framework is a popular and readymade architecture that contains a set of classes and interfaces. Collection Framework is a grouping of classes and interfaces that is used to store and manage the objects. Java Collection framework can also be used for interfaces like Queue, Set, List, etc.
java interview questions and answers
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.
Top 80 Java Collections Interview Questions & Answers
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!!
1) What is framework in Java? A framework is a popular and readymade architecture that contains a set of classes and interfaces. 2) What is the.
Top 40 Java collection interview questions and answers
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. Array and Collection are somewhat similar regarding storing the references of objects and manipulating the data, but they differ in many ways. The main differences between the array and Collection are defined below:.
Solved examples with detailed answer description, explanation are given and it would be easy to understand.
Java Collections Interview Questions & Answers
Looking for Java Collection job? Collections in Java is a group of objects to store and manipulate them. Please have a look at our Java Collection job interview questions and answers page to win your job.
From last couple of years Java interviews has changed drastically, now days no one ask about classical questions like difference between Iterator and Enumerator or difference between ArrayList or Vector. In , the most popular question was about usage of different collection interface e. Map, Set, List and Queue. Interviewer was also very keen to know how much you know about certain implementation e. In order to prepare on Collections framework I just following items : 1 Understand interfaces e. Map, Set, List, Queue and otherws to figure out key difference e. Set doesn't allow duplicate but List does.
ArrayList, HashMap, TreeMap, Hashtable, and Vector classes provide random access to its elements. Download java collections pdf for more information. What is.
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. Collections are used in situations where data is dynamic. Collections allow adding an element, deleting an element and host of other operations.
Или это его подвинули. Голос все звал его, а он безучастно смотрел на светящуюся картинку. Он видел ее на крошечном экране. Эту женщину, которая смотрела на него из другого мира. Она наблюдает за тем, как я умираю.
Надо звонить Стратмору. - Домой? - ужаснулся Бринкерхофф. - Вечером в субботу.