Java / List and its implementations
1. How do I get the current index of an ArrayList while iterating using for-loop in Java.
using the traditional for-loop, we always have the index of the element. for (int i = 0; i < myList.size(); i ++) { // i hold the index....
2. What are the similarities between an array and an ArrayList?
An array and an array list share the below properties that make its behavior similar in Java. Both array and ArrayList allow duplicate elements....
3. How do I reverse an ArrayList in Java?
Using Collections.reverse (list) an ArrayList elements can be reversed. The method does not return a reversed list instead it reverses the same list.
4. Java collections: How do I identify duplicate elements in a List?
Using HashSet implementation which doesn't allow duplicates can be used to identify the elements that are duplicate at the List. Set has a method add() that return boolean value true if the element already exists else returns false which can...
5. Difference between removeAll() and retailAll() methods in Java collection.
removeAll drops every element also present in the argument collection. retainAll keeps only the intersection and removes everything else.
6. How do you remove all the elements from an ArrayList in Java?
ArrayList.clear() drops every element and keeps the capacity. removeAll(list) also empties it but walks the argument. Do not confuse either with = new ArrayList().
7. Java Collection: Difference between ArrayList.clear() and ArrayList.removeAll().
ArrayList.clear() will traverse the underlying Array and set each entry to null that eventually removes all the elements of ArrayLIst. removeAll(collection) traverse through the ArrayList comparing for collection and remove the element, if it exists....
8. Big O Notation for Arrays vs. Linked List insertions.
O(1) accurately describes inserting at the end of the array. However, if you're inserting into the middle of an array, you have to shift all the elements after that element, so the complexity for insertion in that case is O(n)...
9. Give an example of O(log(n)).
Binary search.
10. Difference between CopyOnWriteArrayList and ArrayList in Java.
CopyOnWriteArrayList is a thread-safe collection while ArrayList is not thread-safe. Iterator of ArrayList is fail-fast and throw ConcurrentModificationException once detect any modification in List once iteration begins but Iterator of CopyOnWriteArrayList is fail-safe and doesn't throw Concurre
11. Explain CopyOnWriteArrayList in Java collections.
CopyOnWriteArrayList implements List interface but its a thread-safe collection. CopyOnWriteArrayList creates copy of underlying ArrayList with every mutation operation e.g.
12. Why does ArrayList implement RandomAccess Interface?
RandomAccess is a marker interface used by List implementations to indicate that they support fast (constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied...
13. How to create Immutable List in java?
Using Collections.unmodifiableList(list) we can create an immutable list in Java. UnsupportedOperationException is thrown when we try to modify or add element in an unmodifiableList.
14. Explain set method of List in Java.
The method set(int index, Element E) updates the element of specified index with the given element E and also returns the element E previously stored at this index.
15. Which is faster? ArrayList or Vector. Give reason.
ArrayList is faster than Vector since ArrayList is not synchronized while Vector is. Synchronization usually affects performance.
16. Difference between Collection.stream().forEach() and Collection.forEach().
The order of Stream.forEach is random while Iterable.forEach is always executed in the iteration order of the Iterable. If Iterable.forEach is iterating over a synchronized collection, Iterable.forEach takes the collection's lock once and holds it across all the calls to...
17. What happens when an Arraylist is resized?
A new array is created and the contents of the old one are copied over.
18. When ArrayList is resized?
While an element is being added to the ArrayList, JVM checks if ArrayList has adequate space available by calling ensureCapacity method. If a space exists, it adds the element to the ArrayList otherwise Array resizing happens....
19. What is the growing factor of ArrayList?
The growing factor is 1.5 while hashmap is 2, If the current capacity of ArrayList is 10, then the new capacity would be 16.
20. Performance of ArrayList.
The add operation runs in amortized constant time, meaning that adding n elements requires O(n) time. All of the other operations run in linear time.
21. Default capacity of LinkedList in Java.
LinkedList does NOT have initial capacity. It is initiated to empty.
22. Limitations of iterator in Java.
The list items can be iterated only in forward direction. Once the end of the list is reached, we cannot reiterate and need to create new iterator.