Prev Next

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. // myList.get(i) retrives the element. } In case of enhanced/condensed for loop, we need to manually add index variable and increment on each iteration as shown...

Read full answer

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. Both are unordered lists. Both uses index to refer and retrieve its elements and index starts with 0. Both supports null values. Both maintain the order...

Read full answer

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. ArrayList aList = new ArrayList(); //Add elements to ArrayList object aList.add("1"); aList.add("2"); aList.add("3"); aList.add("4"); aList.add("5"...

Read full answer

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 be used as a tracker to find the duplicate elements as ...

Read full answer

5. Difference between removeAll() and retailAll() methods in Java collection.

removeAll method removes the collection elements (List, Set, Vector also Collection) that exist in the specified collection of elements. public class ArrayListEx { public static void main (String [] args) { List < String > myList = new ArrayList <> (); myList.add( "A" ); myList.add( "B" ); myList...

Read full answer

6. How do you remove all the elements from an ArrayList in Java?

using clear() method, we can remove all the elements from an ArrayList in Java. Another way is passing the same ArrayList reference as an argument to the removeAll() method removes all the elements.

Read full answer

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. ArrayList.clear() is comparatively faster as i...

Read full answer

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) for arrays. For linked list, you have to traverse the list to do middl...

Read full answer

9. Give an example of O(log(n)).

Binary search.

Read full answer

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...

Read full answer

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. add or set. Normally CopyOnWriteArrayList is very expensive because it involves costly Array copy with every write operatio...

Read full answer

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 to either random or sequential ac...

Read full answer

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.

Read full answer

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.

Read full answer

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.

Read full answer

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 the action method. The St...

Read full answer

17. What happens when an Arraylist is resized?

A new array is created and the contents of the old one are copied over.

Read full answer

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. During resizing process, a new array of a greater size is created, the...

Read full answer

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.

Read full answer

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.

Read full answer

21. Default capacity of LinkedList in Java.

LinkedList does NOT have initial capacity. It is initiated to empty.

Read full answer

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.

Read full answer

«
»

Comments & Discussions