Java Collections framework is fundamental utility tools provided by Java that are not only topic of interview but also heavily used in java programming on all types of programs including web-based and desktop applications.
Your knowledge of Java will be considered incomplete without a good working experience of collections API, therefore I highly recommend writing more code and experimenting with it as much as possible.
Here are a few important practical collection framework interview questions that can be asked in a Java interview.
Java Interview Preparation Tips
- Part 0: Things You Must Know For a Java Interview
- Part 1: Core Java Interview Questions
- Part 2: JDBC Interview Questions
- Part 3: Collections Framework Interview Questions
- Part 4: Threading Interview Questions
- Part 5: Serialization Interview Questions
- Part 6: Classpath Related Questions
- Part 7: Java Architect Scalability Questions
What is Java Collections API?
Java Collections framework API is a unified architecture for representing and manipulating collections. The API contains Interfaces, Implementations & Algorithm to help java programmer in everyday programming. In nutshell, this API does 6 things at high level
- Reduces programming efforts. – Increases program speed and quality.
- Allows interoperability among unrelated APIs.
- Reduces effort to learn and to use new APIs.
- Reduces effort to design new APIs.
- Encourages & Fosters software reuse.
To be specific, There are six collection java interfaces. The most basic interface is Collection. Three interfaces extend Collection: Set, List, and SortedSet. The other two collection interfaces, Map and SortedMap, do not extend Collection, as they represent mappings rather than true collections.
What is an Iterator?
Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.
What is the difference between java.util.Iterator and java.util.ListIterator?
- Iterator : Enables you to traverse through a collection in the forward direction only, for obtaining or removing elements
- ListIterator : extends Iterator, and allows bidirectional traversal of list and also allows the modification of elements.
What is HashMap and Map?
Map is Interface which is part of Java collections framework. This is to store Key Value pair, and Hashmap is class that implements that using hashing technique.
Difference between HashMap and HashTable? Compare Hashtable vs HashMap?
Both Hashtable and HashMap provide key-value access to data. The Hashtable is one of the original collection classes in Java (also called as legacy classes). HashMap is part of the new Collections Framework, added with Java 2, v1.2. There are several differences between HashMap and Hashtable in Java as listed below
- The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn’t allow nulls).
- HashMap does not guarantee that the order of the map will remain constant over time. But one of HashMap’s subclasses is LinkedHashMap, so in the event that you’d want predictable iteration order (which is insertion order by default), you can easily swap out the HashMap for a LinkedHashMap. This wouldn’t be as easy if you were using Hashtable.
- HashMap is non synchronized whereas Hashtable is synchronized.
- Iterator in the HashMap is fail-fast while the enumerator for the Hashtable isn’t. So this could be a design consideration.
What does synchronized means in Hashtable context?
Synchronized means only one thread can modify a hash table at one point of time. Any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.
What is fail-fast property?
At high level – Fail-fast is a property of a system or software with respect to its response to failures. A fail-fast system is designed to immediately report any failure or condition that is likely to lead to failure. Fail-fast systems are usually designed to stop normal operation rather than attempt to continue a possibly-flawed process.
When a problem occurs, a fail-fast system fails immediately and visibly. Failing fast is a non-intuitive technique: “failing immediately and visibly” sounds like it would make your software more fragile, but it actually makes it more robust. Bugs are easier to find and fix, so fewer go into production.
In Java, Fail-fast term can be related to context of iterators. If an iterator has been created on a collection object and some other thread tries to modify the collection object “structurally”, a concurrent modification exception will be thrown.
It is possible for other threads though to invoke “set” method since it doesn’t modify the collection “structurally”. However, if prior to calling “set”, the collection has been modified structurally, “IllegalArgumentException” will be thrown.
Why doesn’t Collection extend Cloneable and Serializable?
From Sun FAQ Page: Many Collection implementations (including all of the ones provided by the JDK) will have a public clone method, but it would be mistake to require it of all Collections.
For example, what does it mean to clone a Collection that’s backed by a terabyte SQL database? Should the method call cause the company to requisition a new disk farm? Similar arguments hold for serializable.
If the client doesn’t know the actual type of a Collection, it’s much more flexible and less error prone to have the client decide what type of Collection is desired, create an empty Collection of this type, and use the addAll method to copy the elements of the original collection into the new one.
Note on Some Important Terms
- Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.
- Fail-fast is relevant from the context of iterators. If an iterator has been created on a collection object and some other thread tries to modify the collection object “structurally”, a concurrent modification exception will be thrown. It is possible for other threads though to invoke “set” method since it doesn’t modify the collection “structurally”. However, if prior to calling “set”, the collection has been modified structurally, “IllegalArgumentException” will be thrown.
How can we make Hashmap synchronized?
HashMap can be synchronized by Map m = Collections.synchronizedMap(hashMap);
Where will you use Hashtable and where will you use HashMap?
There Are Multiple Aspects To This Decision:
- The basic difference between a Hashtable and an HashMap is that, Hashtable is synchronized while HashMap is not. Thus whenever there is a possibility of multiple threads accessing the same instance, one should use Hashtable. While if not multiple threads are going to access the same instance then use HashMap. Non synchronized data structure will give better performance than the synchronized one.
- If there is a possibility in future that – there can be a scenario when you may require to retain the order of objects in the Collection with key-value pair then HashMap can be a good choice. As one of HashMap’s subclasses is LinkedHashMap, so in the event that you’d want predictable iteration order (which is insertion order by default), you can easily swap out the HashMap for a LinkedHashMap. This wouldn’t be as easy if you were using Hashtable. Also if you have multiple thread accessing you HashMap then Collections.synchronizedMap() method can be leveraged. Overall HashMap gives you more flexibility in terms of possible future changes.
Difference between Vector and ArrayList? What is the Vector class?
Vector and ArrayList both classes are implemented using dynamically resizable arrays, providing fast random access and fast traversal. ArrayList and Vector class both implement the List interface. Both the classes are member of Java collection framework, therefore from an API perspective, these two classes are very similar. However, there are still some major differences between the two. Below are some key differences
- Vector is a legacy class which has been retrofitted to implement the List interface since Java 2 platform v1.2
- Vector is synchronized whereas ArrayList is not. Even though Vector class is synchronized, still when you want programs to run in multithreading environment using ArrayList with Collections.synchronizedList() is recommended over Vector.
- ArrayList has no default size while vector has a default size of 10.
- The Enumerations returned by Vector’s elements method are not fail-fast. Whereas ArraayList does not have any method returning Enumerations.
What is the Difference between Enumeration and Iterator interface?
Enumeration and Iterator are the interface available in java.util package. The functionality of Enumeration interface is duplicated by the Iterator interface. New implementations should consider using Iterator in preference to Enumeration. Iterators differ from enumerations in following ways:
- Enumeration contains 2 methods namely hasMoreElements() & nextElement() whereas Iterator contains three methods namely hasNext(), next(),remove().
- Iterator adds an optional remove operation, and has shorter method names. Using remove() we can delete the objects but Enumeration interface does not support this feature.
- Enumeration interface is used by legacy classes. Vector.elements() & Hashtable.elements() method returns Enumeration. Iterator is returned by all Java Collections Framework classes. java.util.Collection.iterator() method returns an instance of Iterator.
Why Java Vector class is considered obsolete or unofficially deprecated? or Why should I always use ArrayList over Vector?
You should use ArrayList over Vector because you should default to non-synchronized access. Vector synchronizes each individual method. That’s almost never what you want to do. Generally you want to synchronize a whole sequence of operations.
Synchronizing individual operations is both less safe (if you iterate over a Vector, for instance, you still need to take out a lock to avoid anyone else changing the collection at the same time) but also slower (why take out a lock repeatedly when once will be enough)? Of course, it also has the overhead of locking even when you don’t need to. It’s a very flawed approach to have synchronized access as default.
You can always decorate a collection using Collections.synchronizedList – the fact that Vector combines both the “resized array” collection implementation with the “synchronize every operation” bit is another example of poor design; the decoration approach gives cleaner separation of concerns.
Vector also has a few legacy methods around enumeration and element retrieval which are different than the List interface, and developers (especially those who learned Java before 1.2) can tend to use them if they are in the code. Although Enumerations are faster, they don’t check if the collection was modified during iteration, which can cause issues, and given that Vector might be chosen for its syncronization – with the attendant access from multiple threads, this makes it a particularly pernicious problem.
Usage of these methods also couples a lot of code to Vector, such that it won’t be easy to replace it with a different List implementation. Despite all above reasons Sun may never officially deprecate Vector class. (Read details Deprecate Hashtable and Vector)
What is an enumeration?
An enumeration is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It is a construct which collection classes return when you request a collection of all the objects stored in the collection. It allows sequential access to all the elements stored in the collection.
What is the difference between Enumeration and Iterator?
The functionality of Enumeration interface is duplicated by the Iterator interface. Iterator has a remove() method while Enumeration doesn’t. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as using Iterator we can manipulate the objects also like adding and removing the objects. So Enumeration is used when ever we want to make Collection objects as Read-only.
Where will you use Vector and where will you use ArrayList?
The basic difference between a Vector and an ArrayList is that, vector is synchronized while ArrayList is not. Thus whenever there is a possibility of multiple threads accessing the same instance, one should use Vector. While if not multiple threads are going to access the same instance then use ArrayList. Non synchronized data structure will give better performance than the synchronized one.
What is the importance of hashCode() and equals() methods? How they are used in Java?
The java.lang.Object has two methods defined in it. They are
- public boolean equals(Object obj)
- public int hashCode().
These two methods are used heavily when objects are stored in collections. There is a contract between these two methods which should be kept in mind while overriding any of these methods. The Java API documentation describes it in detail.
The hashCode Method
The hashCode() method returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable or java.util.HashMap.
The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. The equals(Object obj) method indicates whether some other object is “equal to” this one.
The equals Method
The equals method implements an equivalence relation on non-null object references:
It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true). Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
A practical Example of hashcode() and equals():
This can be applied to classes that need to be stored in Set collections. Sets use equals() to enforce non-duplicates, and HashSet uses hashCode() as a first-cut test for equality. Technically hashCode() isn’t necessary then since equals() will always be used in the end, but providing a meaningful hashCode() will improve performance for very large sets or objects that take a long time to compare using equals().
What is the difference between Sorting performance of Arrays.sort() vs Collections.sort() ? Which one is faster? Which one to use and when?
Many developers are concerned about the performance difference between java.util.Array.sort() java.util.Collections.sort() methods. Both methods have same algorithm the only difference is type of input to them. Collections.sort() has a input as List so it does a translation of List to array and vice versa which is an additional step while sorting.
So this should be used when you are trying to sort a list. Arrays.sort is for arrays so the sorting is done directly on the array. So clearly it should be used when you have a array available with you and you want to sort it.
What is java.util.concurrent BlockingQueue? How it can be used?
Java has implementation of BlockingQueue available since Java 1.5. Blocking Queue interface extends collection interface, which provides you power of collections inside a queue. Blocking Queue is a type of Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.
A typical usage example would be based on a producer-consumer scenario. Note that a BlockingQueue can safely be used with multiple producers and multiple consumers. An ArrayBlockingQueue is a implementation of blocking queue with an array used to store the queued objects. The head of the queue is that element that has been on the queue the longest time.
The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.
ArrayBlockingQueue requires you to specify the capacity of queue at the object construction time itself. Once created, the capacity cannot be increased. This is a classic “bounded buffer” (fixed size buffer), in which a fixed-sized array holds elements inserted by producers and extracted by consumers. Attempts to put an element to a full queue will result in the put operation blocking; attempts to retrieve an element from an empty queue will be blocked.
Set and List interface extend Collection, so Why doesn’t Map interface extend Collection?
Though the Map interface is part of collections framework, it does not extend collection interface. This is by design, and the answer to this questions is best described in Sun’s FAQ Page: This was by design. We feel that mappings are not collections and collections are not mappings. Thus, it makes little sense for Map to extend the Collection interface (or vice versa).
If a Map is a Collection, what are the elements? The only reasonable answer is “Key-value pairs”, but this provides a very limited (and not particularly useful) Map abstraction. You can’t ask what value a given key maps to, nor can you delete the entry for a given key without knowing what value it maps to.
Collection could be made to extend Map, but this raises the question: what are the keys? There’s no really satisfactory answer, and forcing one leads to an unnatural interface. Maps can be viewed as Collections (of keys, values, or pairs), and this fact is reflected in the three “Collection view operations” on Maps (keySet, entrySet, and values).
While it is, in principle, possible to view a List as a Map mapping indices to elements, this has the nasty property that deleting an element from the List changes the Key associated with every element before the deleted element. That’s why we don’t have a map view operation on Lists.
Which implementation of the List interface provides for the fastest insertion of a new element into the middle of the list?
List interface has three main implementation classes
- Vector
- ArrayList
- LinkedList
ArrayList and Vector both use an array to store the elements of the list. When an element is inserted into the middle of the list the elements that follow the insertion point must be shifted to make room for the new element.
The LinkedList is implemented using a doubly linked list; an insertion requires only the updating of the links at the point of insertion. Therefore, the LinkedList allows for fast insertions and deletions.
What is the difference between ArrayList and LinkedList? (ArrayList vs LinkedList.)
java.util.ArrayList and java.util.LinkedList are two Collections classes used for storing lists of object references Here are some key differences:
- ArrayList uses primitive object array for storing objects whereas LinkedList is made up of a chain of nodes. Each node stores an element and the pointer to the next node. A singly linked list only has pointers to next. A doubly linked list has a pointer to the next and the previous element. This makes walking the list backward easier.
- ArrayList implements the RandomAccess interface, and LinkedList does not. The commonly used ArrayList implementation uses primitive Object array for internal storage. Therefore an ArrayList is much faster than a LinkedList for random access, that is, when accessing arbitrary list elements using the get method. Note that the get method is implemented for LinkedLists, but it requires a sequential scan from the front or back of the list. This scan is very slow. For a LinkedList, there’s no fast way to access the Nth element of the list.
- Adding and deleting at the start and middle of the ArrayList is slow, because all the later elements have to be copied forward or backward. (Using System.arrayCopy()) Whereas Linked lists are faster for inserts and deletes anywhere in the list, since all you do is update a few next and previous pointers of a node.
- Each element of a linked list (especially a doubly linked list) uses a bit more memory than its equivalent in array list, due to the need for next and previous pointers.
- ArrayList may also have a performance issue when the internal array fills up. The arrayList has to create a new array and copy all the elements there. The ArrayList has a growth algorithm of (n*3)/2+1, meaning that each time the buffer is too small it will create a new one of size (n*3)/2+1 where n is the number of elements of the current buffer. Hence if we can guess the number of elements that we are going to have, then it makes sense to create a arraylist with that capacity during object creation (using construtor new ArrayList(capacity)). Whereas LinkedLists should not have such capacity issues.
Where will you use ArrayList and Where will you use LinkedList? Or Which one to use when (ArrayList / LinkedList).
Below is a snippet from SUN’s site.
The Java SDK contains 2 implementations of the List interface – ArrayList and LinkedList. If you frequently add elements to the beginning of the List or iterate over the List to delete elements from its interior, you should consider using LinkedList. These operations require constant-time in a LinkedList and linear-time in an ArrayList. But you pay a big price in performance. Positional access requires linear-time in a LinkedList and constant-time in an ArrayList.
What is performance of various Java collection implementations/algorithms? What is Big ‘O’ notation for each of them ?
Each java collection implementation class have different performance for different methods, which makes them suitable for different programming needs.
Performance of Map interface implementations
Hashtable
An instance of Hashtable has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. Note that the hash table is open: in the case of a “hash collision”, a single bucket stores multiple entries, which must be searched sequentially. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. The initial capacity and load factor parameters are merely hints to the implementation. The exact details as to when and whether the rehash method is invoked are implementation-dependent.
HashMap
This implementation provides constant-time [ Big O Notation is O(1) ] performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the “capacity” of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it’s very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
TreeMap
The TreeMap implementation provides guaranteed log(n) [ Big O Notation is O(log N) ] time cost for the containsKey, get, put and remove operations.
LinkedHashMap
A linked hash map has two parameters that affect its performance: initial capacity and load factor. They are defined precisely as for HashMap. Note, however, that the penalty for choosing an excessively high value for initial capacity is less severe for this class than for HashMap, as iteration times for this class are unaffected by capacity.
Performance of Set interface implementations
HashSet
The HashSet class offers constant-time [ Big O Notation is O(1) ] performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the HashSet instance’s size (the number of elements) plus the “capacity” of the backing HashMap instance (the number of buckets). Thus, it’s very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
TreeSet
The TreeSet implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).
LinkedHashSet
A linked hash set has two parameters that affect its performance: initial capacity and load factor. They are defined precisely as for HashSet. Note, however, that the penalty for choosing an excessively high value for initial capacity is less severe for this class than for HashSet, as iteration times for this class are unaffected by capacity.
Performance of List interface implementations
LinkedList
– Performance of get and remove methods is linear time [ Big O Notation is O(n) ] – Performance of add and Iterator.remove methods is constant-time [ Big O Notation is O(1) ]
ArrayList
– The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. [ Big O Notation is O(1) ] – The add operation runs in amortized constant time [ Big O Notation is O(1) ] , but in worst case (since the array must be resized and copied) adding n elements requires linear time [ Big O Notation is O(n) ] – Performance of remove method is linear time [ Big O Notation is O(n) ] – All of the other operations run in linear time [ Big O Notation is O(n) ]. The constant factor is low compared to that for the LinkedList implementation.
Can you think of a question which is not part of this post? Please don’t forget to share it with me in the comments section & I will try to include it in the list.
Article Updates
- Article Updated in April 2019 – Updated Introduction section, fixed minor text and updated links.
- Article updated in Feb 2018 – Minor updates.
Thanks!! this is a great collection. I have bookmarked it in delicious.
Nice collection
good collection
Thanks for putting in all the effort to create this entry.
Excellent.
useful and sensible collation
Collection tells you what collections are, and how they'll make your job easier and your programs better. You'll learn about the core elements that comprise the Collections Framework: interfaces, implementations and algorithms.
good questions collection.
nice collection…keep it up!!!
A question I was asked recently was what is the difference between an ArrayList and a LinkedList.
@Anonymous : Thanks for your comment. I have not included this in my list of questions here. – Difference between ArrayList and LinkedList.- Where to use ArrayList or LinkedList.
Great collection. very useful.
Lakshmanan Venkatachalam
it is really good. however, if you provide real world examples on equals and hashCode, then it would be perfect.
@moorthy – Thanks for your comment. I have updated the post with an example for hashCode/equals. Hope this helps. Let me know if you have any other questions/suggestions.
Good Collection..Keep up the good work and do keep posting more of this..
I take exception to a lot of these points. For example there's almost no excuse to ever use a Vector, Hashtable or Enumeration. They should be considered deprecated. Vector and Hashtable's synchronization is flawed (eg fine grained only, open to DoS) and their performance is poor (eg they have inefficient growth algorithms). The only excuse for using them is when interfacing with legacy APIs that require them. Stating that Vector should be used in multithreaded code over ArrayList makes me cringe.
Also, while what you say about ArrayList vs LinkedList is true, but often an ArrayList still wins out over LinkedList. LinkedList puts more strain on the heap, the garbage collector, and it has little spatial locality so tends to result in more cache misses. My general rule of thumb is that if you'd be better off with a LinkedList, benchmark it. You'll probably be surprised.
7. iterators do fail-fast. They aren't fail-save.
@Anonymous – Thanks for pointing out. I am going to correct it. Also this gives room for few more interesting questions which can be added.
@Anonymous – Thanks again. I have now updated this post with fail-fast. I had to read several places to understand what fail-fast means. I hope now the #7 has correct answer/question.
I am going to do more reading on "difference between fail-fast vs fail-safe" and try to update it in this post when I have some good information with me.
Please feel free to share any information/resources you have.
@Anonymous – Updated the Vector vs ArrayList section after doing more reading on it.
Also compiled a new question about why Vector should be considered a deprecated class. Hope this improves the ArrayList/Vector part.
I still need to do more reading on Why Hashtable/Enumeration should not be used.
Will be back with updates soon.
nice questions.ThanksPrashanthttp://prasanthaboutjava.blogspot.com/
Very few of items are interesting…
Good Collection….but expected copyonwrite collections qns as well…See my blog for ConcurrentHashMap Vs Hashmap Vs hashtable
Thanks,
Apurv
My Java Blog: http://apurvagnihotri.blogspot.com/
@Apurva – Thanks for feedback. I will try to add that too.
its really helpful for me for my infosys interview.Pradeepmala
hi its really work for my infosys interview…Pradeepmala
@Pradeepmala – I am really glad to see your comment. This makes me really happy that this collection is proving helpful for real interviews. Feel free to provide any suggestions on missing questions in this list. Thanks again for your comment.
realy very good collection ,realy very helpfull
Quite impressive collection on Collections
Really great collection….very nice
Very good collection. But I'd like to see more questions on the big o complexity wrt collections especially in regards to hashtable and hashmap. What if the value for a specific key is list? How does big o effect etc.
Thanks again,
Usha
@Usha – Thanks for your comment. I have now added a new question at the end covering the performance and Big O notation aspect. Hope this helps.
I would like to get more details on the specific question you have about a "key is list". Let me know.
You have provided such a great information. These questions are always asked in an interview. You have a huge collection of questions. I really appreciate sharing this great post. Keep up your work..
This is really a nice post and really helpful.
god bless you mate! this is very useful for people entering the real Java world.
On 5) Difference between HashMap and HashTable? Compare Hashtable vs HashMap?
you say HashMap is fail-fast, isn't it fail-safe? because during iteration it creates a separate copy of the objects and if there was a modification to the objects during iteration it doesn't halt (fail-fast).
@Selvam – Thanks for commenting. The Fail-fast vs Fail-safe is a tricky one. 1. The iterator of HashMap will throw ConcurrentModificationException when the object is "Structurally" modified (except through the iterator's own remove or add methods). Refer the Java API doc here [HashMap API Doc]2. Also check out this link to get a sample code which can demonstrate the difference [ Fail-fast vs Fail-safeI find this link really useful and referred it several times. Hope you find it useful.
The iterator of HashMap will throw ConcurrentModificationException when the object is "Structurally" modified (except through the iterator's own remove or add methods). Refer the Java API doc here
good collection
Good Knowledge Questions
Nice job, but in 22 insertion time is not constant everywhere on linkedlist. Inserting at the begining in the end is constant becaise you have references ti the first and the last element, but ti insert in the middle you have ti traverse the nodes ti reach the position you want, so is linear time. Actually n/2 in java because it knows where to start counting (begining or end)
collection framework is very confusing topic so switch of your iPod,cellphone,facebook and do it sharply 🙂
Thanks a Lot good collection….
its too good collections….
very useful Question Answers………Thanks
You are wrong there: ArrayList certainly has a default size of 10.
thank you for the answers…helped alot to study..keep going
Excellent.you got very good understanding of very important concepts
Thanks, this is very helpful
very helpful
very helpful
can you write something about Properties class. Thank you.
Thats correct. See the constructors of these classes.
public Vector() {
this(10);
}
public ArrayList() {
this(10);
}
Thats correct. ArrayList as well Vector has initial size as 10. Please see below the constructors of Vector and ArrayList:
public ArrayList() {
this(10);
}
public Vector() {
this(10);
}
Let me know what would you like to learn about properties class, I try to create a post about a simple tutorial on it.
hi,
i am getting collection from entity-bean then i am trying to remove element from colletion it gives me error like
javax.ejb.EJBException: This collection is a read-only snapshot
if any-body know how i can resolve this error please reply me [at ]patel_niket9@yahoo.co.in
This may be a scenario of unmodifiableCollection. You can not change a read-only collection which is returned from unmodifiableCollection method. However you can copy it and then modify the copy. not sure if that would help in your case.
Hi,
Which is the best Java collection that can be used to store huge data, say a million objects? And why?
Thx
Great collection on collections! Thanks for sharing.
Thanks for your efforts…
Thanks for your efforts…
Really thanks for great work..
Nice Collections.
I was wondering wouldn't it be great to add jdk 7 features questions too!!
Thanq
very nice
very well explained!! thanks…
Clear Explanation thank You
Good Collection Dev…
Thanks a lot 🙂
Dude thnx first of all for the above collection kindly add the below ques:-1.How can a Collection be made unmodifiable?.2.How can a Collection be made Singleton.?3.Difference between Collection and Collections.4.Difference between Comparable and Comparator interfaces.5.What is type safety and why is it important in collection.?6.How can a Collection be made Synchronized.?7.How the implementation of Enumeration and Iterator is provided by the Collection framework….?
Its realy a good collection of collections..
bt am not getting the ques. no. 8.. can u please explain it to me.. sychronization is clr to me.. bt the clonable and serialization part is not clr at all..
good 🙂 thanks
Thanks for suggesting – this probably qualifies to be part 2 of Collections questions. Will do it shortly. I appreciate your inputs.
Nice
very helpful, short n meaningful thanks::)
very good explanation.. Thanks for your efforts
ya its good collection…
Very Important Collection Questions and Answer post with detailed explanation.Thanks for the post
arraylist is primitive object array and support random access interface where as LinkedList is collection of nodes to make a chain pointing one to another and it is not random access interface support….
awsome collections
Ur god sent(from dev :P). Thanks… This was very helpful :)…
Thanx such a huge info….
good collections….i have learned a lot from this…
I am fresher as Java developer i am looking for job.Thank you for providing this interview questions.which can help me while facing interview.
ArrayList since it implements RandomAccess interface is very fast in iterations, so should be preferred over LinkedList when there are lot of searches and very few insersions and deletions.
Hi,
Hashtable & HashMap both allows key and value as nulls. Why you have wrote like Hashtable does not allow key and value as nulls. Pls correct it.
hi sanjay yadav how use multi dimentional in collections
Its wonderful..
Its a great compilation indeed. Found many interesting subjects for my next area of research.
Plz specify the topics to be covered for corejava developer interview.. plz don't use etc etc specify exactly.. I hav short time to prepare 😛
Well he wont use dem…lol
Too Good!!
right
Great collection, few questions on concurrent collections would be great
Java Interview Club
techquestforintervie.wix.com/javainterviewclub
Well, I tried to print the size as soon as they are initialized…
List SampleArrayList = new ArrayList();
System.out.println("Size of SampleArrayList = " + SampleArrayList.size());
List SampleVector = new Vector();
System.out.println("Size of SampleVector = " + SampleVector.size());
————
Size of SampleArrayList = 0
Size of SampleVector = 0
… I am confused:
Those 7 questions are really important…
But I do not know the answer for 1st and 2nd 🙁
CORE JAVA hehe
thanks a lot Sachin… your effort is highly appreciated…
I would suggest one more set of collection questions…
great job!!
Got the answer!
great job!! mr.sachin
y hehe dude.. anything u knew abt it or wat.
great job!! nice article 🙂
Thank you for the excellent explanation of all the question.
what is ListIterator??
Do it practically you will come to know that Hashtable not allows null value but HashMap allows null values.
Nice collection of important collections questions. Thanks 🙂
thanxs buddy u have really finished my fear for collections framework which i earlier used to have
nicely cleared my doubts
nicely cleared my doubts
Really useful
Got confused with this
" ArrayList has no default size while vector has a default size of 10."
Can you further explain this?
nice post !!!
Hi Sachin FromDev,Thanks
This is the best details I have ever seen on Java Collection Framework
good list – thanks for putting this together!!
Nice collection. Thanks for sharing. Add more…
Hey… one change, vector is from java 1.0
Great Post! all most cover all the major things in collection framework. But readability is very poor.
good
I follow your blog always while getting stuck in some code in my office. I find it really very useful.I have also created my blog on java. Its in an initial phase itself.Java Programming Language
Good one. Thanks for your effort!!!!
Like the blog…good collection to start of. Thanks.
Very good collection of questions with appropriate answers. It will be useful for many java developers.
Good niche. I will select and apply for some cases.
You explain the topics well. But when you are sitting through your interview, you are expected to answer in a line or two. If you provide a concise answer at the end of the explanation.. That helps.
Thanks a bunch anyways …