ArrayList | Vector | |
---|---|---|
1 | No method is synchronized in the ArrayList class. | All methods in Vector are synchronized. |
2 | ArrayList object is not thread safe. | Vector is thread safe. |
3 | Relatively performance is high | Relatively performance is low |
4 | Introduced in 1.2 version and it is non legacy | Introduced in 1.0 version and it is legacy |
ArrayList | LinkedList | |
---|---|---|
1 | The underlying data structure is resizable or growable array. | The underlying data structure is Double Linked List. |
2 | This is Best choice if frequent operation is retrieval and worst choice if frequent operation is insertion or deletion in the middle. | This is Best choice if frequent operation is insertion or deletion in the middle and worst choice if frequent operation is retrieval . |
3 | This class implements Serializable , Cloneable and Random Access interfaces. | This class implements Serializable , Cloneable but not Random Access interface. |
Enumeration | Iterator | |
---|---|---|
1 | It is legacy interface and introduced in 1.0 version. | It is non-legacy and introduced in 1.2 version. |
2 | Applicable only for legacy classes and it is not universal cursor. | Applicable for any Collection implemented class object. |
3 | While iterating the elements we are not allowed to remove the objects just we can perform only read operation. | While iterating we can perform removal also in addition to read operation. |
4 | By using elements() method we can get Enumeration object. | By using iterator() method we can get Iterator object. |
Q13. If we are trying to insert duplicate values in Set what will happen?
If we are trying to insert duplicate objects to the HashSet , we wont get any compile time or run time errors just the add(Object o) returns false and it doesn’t add that object.
Q14. Differences between HashSet and LinkedHashSet?
HashSet | LinkedHashSet | |
---|---|---|
1 | The Underlying datastructure is Hashtable. | The underlying datastructure is combination of LinkedList and Hashtable. |
2 | Insertion Order is not preserved | Insertion Order is preserved |
3 | Introduced in 1.2 version | Introduced in 1.4 version |
Q15. What is LinkedHashSet?
It is the child class of HashSet.In the case of HashSet insertion order is not preserved , but in the case of LinkedHashSet insertion will be preserved.
Q16. Explain about TreeSet?
It is Collection object which can be used to represent a group of objects according to some sorting order.
• The underlying data structure is Balanced tree
• Duplicates are not allowed
• All objects are stored according to some sorting order hence insertion order is not preserved
• Heterogeneous objects are not allowed violation leads to ClassCastException
• For an Empty TreeSet as firs element null value can be inserted but after inserting that first value if we are trying to insert any other objects then we will get NullPointerException
• For an non empty TreeSet if we are trying to inser null value at run time u will get NullPointerException
Q17. What are differences between List and Set interfaces?
List | Set | |
---|---|---|
1 | Insertion Order is preserved | Insertion Order is not preserved |
2 | Duplicate Objects are allowed | Duplicate Objects are not allowed |
3 | The implemented classes are ArrayList,LinkedList , Vector and Stack classes | The implemented classes are HashSet, LinkedHashSet and Tree |
Comparable | Comparator | |
---|---|---|
1 | This can be used for natural sorting order version. | This can be used for implementing customized sorting |
2 | This interface present in java.lang package | This is present in java.util package |
3 | It contains only one method: public int compareTo(Object obj1) |
It contains two methods: public int compare(Object ,Object) public Boolean equals(Object) |
4 | It is marker interface | It is not a marker interface. |
Recommended Posts
Java Collections Interview Questions Part 1
Java Collections Interview Questions Part 3
Core Java Important Questions Part 1
Core Java Important Questions Part 2
Core Java Important Questions Part 3
Core Java Important Questions Part 4
Core Java Important Questions Part 5
Core Java Important Questions Part 6
Core Java Important Questions Part 7
Core Java Important Questions Part 8
Core Java Important Questions Part 9
Core Java Important Questions Part 10
Java MCQs with Answers Set III
If you have any doubts join the discussion below ,our Moderator will reply to your Queries