Java Collection Flashcards
What is the difference between Collection and Collections Framework in Java?
In Java, a Collection is an object that contains multiple elements of
same type in a single unit. These multiple elements can be accessed through one Collection object.
In Java Collections Framework is a library that provides common architecture for creating, updating and accessing different types of
collections.
In Collections framework there are common methods that are frequently used by developers for working on a Collection
object.
What are the main benefits of
Collections Framework in Java?
Reusability: Java Collections Framework provides
common classes and utility methods than can be used with
different types of collections. This promotes the reusability
of the code. A developer does not have to re-invent the
wheel by writing the same method again.
2. Quality: Using Java Collection Framework improves the
program quality, since the code is already tested and used
by thousands of developers.
3. Speed: Most of programmers report that their development
speed increased since they can focus on core logic and use
the generic collections provided by Java framework.
4. Maintenance: Since most of the Java Collections
framework code is open source and API documents is
widely available, it is easy to maintain the code written
with the help of Java Collections framework. One
developer can easily pick the code of previous developer.
What is the root interface of Collection hierarchy in Java?
The root interface of Collection hierarchy in Java is Collection interface.
But the Collection interface extends Iterable interface. Due to this some people consider Iterable interface as the root interface.
Iterable interface is present in java.lang package but Collection interface is present in java.util package.
Oracle Java API docs mention that Collection interface is a member of the Java Collections framework. Whereas, Iterable interface is not stated as a part of Java Collections framework in Java docs. Due to this Collection interface is the root of Collections Framework.
What are the main differences between Collection and Collections?
Type: Collection is an interface in Java. Collections is a class.
Features: Collection interface provides basic features of data structure to List, Set and Queue interfaces. Collections is a utility class to sort and synchronize collection elements. It has polymorphic algorithms to operate on collections.
Method Type: Most of the methods in Collection are at instance level. Collections class has mainly static methods that can work on an instance of Collection.
What are the Thread-safe classes in Java Collections framework?
Stack Properties Vector Hashtable BlockingQueue ConcurrentMap ConcurrentNavigableMap
How will you efficiently remove elements while iterating a Collection?
The right way to remove elements from a collection while iterating is by using ListIterator.remove() method.
How will you convert a List to a Set?
Option 1: Use HashSet
Set mySet = new HashSet(myList);
In this case we put a list into a HashSet. Internally hashCode()
method is used to identify duplicate elements.
Option 2: Use TreeSet
In this case we use our own comparator to find duplicate objects.
Set mySet = new TreeSet(myComparator);
mySet.addAll(myList);
How will you remove duplicate elements from an ArrayList?
Option 1: Use Set
If ordering of elements is not important then we just put the elements of ArrayList in a HashSet and then add them back to the ArrayList.
Option 2: Use LinkedHashSet
If ordering of elements is important then we put the elements of ArrayList in a LinkedHashSet and then add them back to the ArrayList.
How can you maintain a Collection with elements in Sorted order?
Using TreeSet
By providing custom comparator inside the collection
Collections.sort
PriorityQueue
The main difference between PriorityQueue and
Collections.sort() is that PriorityQueue maintains a queue in Order
all the time, but we can only retrieve head element from queue. We
cannot access the elements of PriorityQueue in Random order.
What are the differences between
the two data structures: a Vector and
an ArrayList?
Synchronization: Vector is synchronized, but the ArrayList is not synchronized. So an ArrayList has faster operations
than a Vector.
Data Growth: Internally both an ArrayList and Vector use an array to store data. When an ArrayList is almost full it increases its size by 50% of the array size. Whereas a
Vector increases it by doubling the underlying array size.
In which scenario, LinkedList is better than ArrayList in Java?
ArrayList is more popular than LinkedList in Java due to its ease of use and random access to elements feature.
But LinkedList is better in the scenario when we do not need random access to elements or there are a lot of insertion, deletion of
elements.
What are the differences between a List and Set collection in Java?
Order: List collection is an ordered sequence of elements. A Set is just a distinct collection of elements that is unordered.
Positional Access: When we use a List, we can specify where exactly we want to insert an element. In a Set there is no order, so we can insert element anywhere without
worrying about order.
Duplicate: In a List we can store duplicate elements. A Set can hold only unique elements.
What are the differences between a HashSet and TreeSet collection in Java?
Ordering: In a HashSet elements are stored in a random order. In a TreeSet, elements are stored according to natural ordering.
Null Value Element: We can store null value object in a HashSet. A TreeSet does not allow to add a null value object.
Performance: HashSet performs basic operations like add(), remove(), contains(), size() etc in a constant size time. A TreeSet performs these operations at the order of
log(n) time.
Speed: A HashSet is better than a TreeSet in performance for most of operations like add(), remove(), contains(), size() etc .
Internal Structure: a HashMap in Java internally backs a HashSet. A NavigableMap backs a TreeSet internally.
Features: A TreeSet has more features compared to a HashSet. It has methods like pollFirst(), pollLast(), first(), last(), ceiling(), lower() etc.
In Java, how will you decide when to use a List, Set or a Map collection?
If we want a Collection that does not store duplicate values, then we use a Set based collection.
If we want to frequently access elements operations based on an index value then we use a List based collection. E.g. ArrayList
If we want to maintain the insertion order of elements in a collection then we use a List based collection.
For fast search operation based on a key, value pair, we use a HashMap based collection.
If we want to maintain the elements in a sorted order, then we use a TreeSet based collection.
What are the differences between a HashMap and a Hashtable in Java?
Synchronization: HashMap is not a synchronized collection. If it is used in multi-thread environment, it may not provide thread safety. A Hashtable is a synchronized collection. Not more than one thread can access a Hashtable at a given moment of time. The thread that works on Hashtable acquires a lock on it and it makes other threads wait till its work is completed.
Null values: A HashMap allows only one null key and any number of null values. A Hashtable does not allow null keys and null values.
What are the differences between a HashMap and a TreeMap?
Order: A HashMap does not maintain any order of its keys. In a HashMap there is no guarantee that the element inserted first will be retrieved first.
In a TreeMap elements are stored according to natural
ordering of elements. A TreeMap uses compareTo()
method to store elements in a natural order.
A HashMap uses Hashing internally. A TreeMap internally uses Red-Black tree implementation.
A HashMap can store one null key and
multiple null values. A TreeMap can not contain null key but it may contain multiple null values.
What are the differences
between Comparable and
Comparator?
In Comparable, we can only create one sort
sequence. In Comparator we can create multiple sort sequences.
Comparator interface in Java has method public int compare (Object o1, Object o2) that returns a negative integer, zero, or a positive integer when the object o1 is less than, equal to, or greater than the object o2. A Comparable interface has method public int compareTo(Object o) that returns a negative integer, zero, or a positive integer when this object is less than, equal to, or greater than the object o
In Java, what is the purpose of Properties file?
A Properties file in Java is a list of key-value pairs that can be parsed by java.util.Properties class.
Some of the uses are to store configuration, initial data, application options etc.
When we change the value of a key in a properties file, there is no need to recompile the Java application. So it provides benefit of
changing values at runtime
What is the reason for overriding equals() method?
The equals() method in Object class is used to check whether two objects are same or not. If we want a custom implementation we can override this method.
How does hashCode() method work in Java?
Object class in Java has hashCode() method. This method returns a hash code value, which is an integer.
And we use the hascode generated as a key inside the any data structure that is going to implement the key value data structure