Collection Flashcards
What is Java collection?
A collection is an object that contains multiple elements of the same type in a single unit.
What is Java Collections Framework?
Collections framework is a library that provides common architecture for creating, updating and accessing different types of collections.
What are the main benefits of collections framework?
Reusability: common classes and utility methods that can be used with different types of collections.
Quality: improves the program quality, since the code is already tested.
Speed: developers speed increased since they can focus on core logic and use the collections provided.
Maintenance: Since collections are open source and widely documented, it is easy to maintain the code.
What is the root of the collections framework?
Collection interface is the root of Collections Framework.
What are the thread safe classes in the Collections framework?
Stack, Vector, Hashtable, BlockingQueue, Concurrent Map, ConcurrentNavigableMap
How to convert a list to a set?
Use HashSet. We can put a list into a HashSet, internally the hashCode() method is used to identify duplicate elements.
How to remove duplicate elements from an ArrayList?
If ordering of elements is not important, then we can put the elements of ArrayList into a HashSet and then add them back to the ArrayList.
How can you make the elements of a Collection be in sorted order?
You can use the utility method Collections.sort to sort a list. This is nLog(n) order of performance.
In which scenario is LinkedList better than ArrayList?
LinkedList is better in the scenario when we do not need random access to elements, or if there are a lot of insertion or deletion actions on elements.
What are the differences between list & set?
Order: list is an ordered sequence of elements. Set is just a distinct collection that is unordered.
Positional Access: When using a list we can specify where we want to insert an element. In a set there is no order.
Duplicate: in a list we can store duplicates. In a set we cannot store duplicates.
How to decide when to use List, Set or Map?
If we do not want duplicates, use a set. If we want to frequently access elements operations based on an index value or we want to maintain insertion order then we used a list. For fast key-based search we use a HashMap.
Difference between HashMap & HashTable?
Synchronisation: HashMap is not synchronised. HashTable is synchronised.
Null values: HashMap only allows one null key and n null values. HashTable does not allow null keys or values.
Ordering: HashMap maintains the order of elements. A HashTable does not guarantee any kind of order.
What is the reason for overriding the equals method?
The equals method in Object class is used to check wether two objects are the same or not. If we want a custom implementation we override it. In HashMap implementation, if we want to use an object as a key, we override it.
How does hashCode() method work in java?
Object class in Java has hashCode() method which returns a hash code value, which is an integer. If two objects are the same, their hashcode is also the same. Object generates a hashcode based on the memory address of the instance of the object.
What is the difference between Iterator and Enumeration?
Both are interfaces in java to access Data Structures. Enumeration is an older interface. Enumeration can only traverse legacy collections. Iterator can traverse both legacy and newer collections.Iterator is fail-fast, enumeration is not fail-fast.