What is difference between fail-fast and fail-safe?
Iterator fail-safe property work with the clone of underlying collection, hence it’s not affected by any modification in the collection. By design, all the collection classes in
java.util
package are fail-fast whereas collection classes in java.util.concurrent
are fail-safe. Fail-fast iterators throw ConcurrentModificationException whereas fail-safe iterator never throws How to avoid ConcurrentModificationException while iterating a collection?
We can use concurrent collection classes to avoid
ConcurrentModificationException
while iterating over a collection, for example CopyOnWriteArrayList instead of ArrayList.What is Comparable and Comparator interface?
Java provides Comparable interface which should be implemented by any custom class if we want to use Arrays or Collections sorting methods. Comparable interface has compareTo(T obj) method which is used by sorting methods. We should override this method in such a way that it returns a negative integer, zero, or a positive integer if “this” object is less than, equal to, or greater than the object passed as argument.
But, in most real life scenarios, we want sorting based on different parameters. This is the situation where we need to use
Comparator
interface because Comparable.compareTo(Object o)
method implementation can only sort based on predefined fields or logic and we can’t chose the field on which we want to sort the Object.Comparator interface compare(Object o1, Object o2)
method need to be implemented that takes two Object argument, it should be implemented in such a way that it returns negative int if first argument is less than the second one and returns zero if they are equal and positive int if first argument is greater than second one.How can we sort a list of Objects?
If we need to sort an array of Objects, we can use
Arrays.sort()
. If we need to sort a list of objects, we can useCollections.sort()
. Both these classes have overloaded sort() methods for natural sorting (using Comparable) or sorting based on criteria (using Comparator). Collections internally uses Arrays sorting method, so both of them have same performance except that Collections take sometime to convert list to array.What are best practices related to Java Collections Framework?
- Chosing the right type of collection based on the need, for example if size is fixed, we might want to use Array over ArrayList. If we have to iterate over the Map in order of insertion, we need to use TreeMap. If we don’t want duplicates, we should use Set.
- Some collection classes allows to specify the initial capacity, so if we have an estimate of number of elements we will store, we can use it to avoid rehashing or resizing.
- Write program in terms of interfaces not implementations, it allows us to change the implementation easily at later point of time.
- Always use Generics for type-safety and avoid ClassCastException at runtime.
- Use immutable classes provided by JDK as key in Map to avoid implementation of hashCode() and equals() for our custom class.
- Use Collections utility class as much as possible for algorithms or to get read-only, synchronized or empty collections rather than writing own implementation. It will enhance code-reuse with greater stability and low maintainability
No comments:
Post a Comment