20 Java ArrayList Interview Questions Answers

In this article, I am going to share some of the good Java Interview questions based upon ArrayList class. I have hardly seen a Java interview without any question from ArrayList, and why not its one of the most popular collection class and every Java developer use it on their day to day work. Another reason for asking a question related to ArrayList is that you can ask a wide variety of question to really check breadth and depth of candidate's knowledge. To give you some idea about ArrayList, it's a collection class which implements List interface. It's an alternative to array data structure whose size you cannot change once created. ArrayList is a dynamic array, which can grow and resize itself. By implementing List interface it also got some properties e.g. ordering, ArrayList keeps the element in the order they are inserted and it also provides constant time search operation if you know the index of element e.g. get(index) is O(1) operation. This makes an ArrayList ideal choice when you are looking to retrieve values based upon the index.

On contrary to search, adding and removing in ArrayList is little costly because it can trigger re-sizing, which involves creating a new array and copying all elements from the old array to new array.

I am sure you know the basics but you will learn more by going through some of these frequently asked Java ArrayList questions. You can also take help of the Java Programming Interviews Exposed by Markham, one of the best book to prepare for Java interviews.

Java ArrayList Interview Questions and Answers



ArrayList Questions from Java Interviews

So without wasting any more time, let's start with the questions. The questions are mainly divided in to two category, one is fact based question, which checks how much you know about ArrayList and other is task based question, which evaluate how good you are with doing things with ArrayList. In my list, I have combined both of them.




1. How to remove duplicates from ArrayList in Java?  (Answer)
This is as task based question. Since List interface allows duplicates, ArrayList also allowed it but if you remember Set interface doesn't allow duplicates, which means you can remove duplicates from ArrayList by converting it into a Set and then back to ArrayList, but how will you keep the order intact? See answer for more detail explanation.


2. How to reverse ArrayList in Java? (Answer)
You can can reverse ArrayList by using Collections.reverse() method.  There are couple of more ways e.g. iterating through list and copying element into new list. See answer for more ways to do this task.


3. Difference between an array and ArrayList in Java? (Answer)
This is a fresher level interview question, main difference between array and ArrayList is that former is static and later is dynamic. You cannot change the size of array once created, but ArrayList can grow and increase its size automatically.


4. How to synchronize ArrayList in Java? (Answer)
This is a very good task based question. If you remember, ArrayList is not thread-safe, its not synchronized either, which means you cannot share it between multiple threads if one of them modifies it. Don't worry, you can synchronize ArrayList by using Collections.synchronizedList() method. Check answer to understand steps.


5. When to use ArrayList and LinkedList in Java? (Answer)
This is by far the most popular ArrayList based question from Java Interviews and you can answer it very easily if you are familiar with two key data structure, array and linked list. Since array provides constant time search operation, its better to use ArrayList if search outnumber add and remove operation, otherwise use LinkedList which provides constant time add and remove operation. See answer for more detailed discussion on this topic.
Java ArrayList Interview Questions and Answers


6. Difference between ArrayList and HashSet in Java? (Answer)
One of the simplest question you will ever see on a Java interview. Main difference is former is List while later is Set which means ArrayList allowed duplicates, keeps elements in order while HashSet doesn't allow duplicates and provides no ordering guarantee.


7. How to loop over ArrayList in Java? (Answer)
There are many ways to traverse over ArrayList, you can use classic for loop with index, or you can take iterator from ArrayList and can use while loop in conjunction with Iterator.hasNext() method, Or you can use new foreach loop introduced in Java 5, which doesn't require an index. See the answer for live examples.


8. Difference between Vector and ArrayList in Java? (Answer)
This is the second most popular question based on ArrayList in Java. Though both Vector and ArrayList implements List interface, Vector is synchronized while ArrayList is not synchronized, which means former is thread-safe and fast while later is not thread-safe and slow.


9. How to create and initialize ArrayList in one line? (Answer)
There is a nice little trick to do this by using Arrays.asList() method, but remember the List returned by this class has some difference with ArrayList, please check the answer to understand what are those differences.


10. How to sort ArrayList in Java? (Answer)
Another task based ArrayList interview question. You can easily sort ArrayList by using Collections.sort() method, all you need to make sure is that elements implements either Comparable or Comparator interface. Former is used to sort on natural order while later is used while sorting in custom order.


11. Difference between HashMap and ArrayList in Java? (Answer)
There is huge difference between HashMap and ArrayList, fundamental is former is a Map data structure which stores key value pair while later stores just object. HashMap access object using key while ArrayList access elements using index. Though both provide O(1) search performance, ArrayList's performance is guaranteed but HashMap can vary depending upon collision level.


12. How to use ArrayList in Java? (Answer)
Just see the article to learn different ways of using this popular class in Java.


13. How to convert ArrayList to String in Java? (Answer)
Simple, just call toString()  right? unfortunately the String representation is not very helpful. If you are looking for a comma separated String containing all elements of ArrayList then you can either use Java 8 String joiner or some older library method as shown in answer page.


14. How to get sublist from ArrayList in Java? (Answer)
This is another task based question, but you can easily do it if you remember API. You can get a list of elements in a range by using subList() method from ArrayList class. This would be very helpful in case of sorted list.


15. Difference between length() of array and size() of ArrayList in Java? (Answer)
This is one of the tricky question, if you get the backed array and call the length() it will return how many element you can store in this array, also known as capacity, but if you call size() function of ArrayList class then it will return total number of elements currently stored in ArrayList, which is always less than or equal to capacity.


16, What is CopyOnWriteArrayList in Java? (Answer)
Its a concurrent collection class which is introduced as an alternative of synchronized List in Java. This class take advantage of advanced thread-safety technique instead of locking. Its very efficient if ArrayList is mostly used for reading purpose, because it allows multiple threads to read data without locking, which was not possible with synchronized ArrayList. See answer to learn more about CopyOnWriteArrayList class.


17. How to remove objects from ArrayList in Java? (Answer)
There are two ways to remove elements from ArrayList, first you can call remove(int index) method and pass on index of the element you want to remove and second you can call remove(Object obj) method and pass the element you want to remove. By the way, just be careful while working with ArrayList of integers because autoboxing can cause issues by creating ambiguity between two remove methods. See answer for more detailed discussion on the topic.


18. How to make ArrayList read only in Java? (Answer)
Another task and API based interview question. You can answer it easily if you about Collections utility class, which provides several wrappers of standard ArrayList class e.g. you can use Collections to create synchronized version or read only version of ArrayList in Java.


19. How to sort ArrayList in descending order in Java? (Answer)
This is the follow-up question of one of the previous question related to sorting. By default, elements are sorted on increasing order as this is how their compareTo() or compare() method compares them. If you want to sort into descending order, just reverse the comparison logic using Collections.reverseComparator() method.


That's all about ArrayList questions from Java Interviews. I have covered almost everything I know which has been asked previously to Java programmers, both freshers and experienced with 2 to 5 years. Some companies which gives more important to coding skill can also ask you to implement your own ArrayList class in Java, so be prepare for that also and if you really want to do well on Java interviews, I suggest to take a look at Java Programming Interviews Exposed By Markham.

Its from Wrox publisher and one of the best book to prepare Java interviews. It contain questions from Core Java, Multithreading, GC, JVM internals and related technology questions e.g. Maven, JUnit, Servlet, JSP, Android etc. Best thing about this book is how well they explain the answer. You will even learn from this book and able to fill the gaps in your understanding.


2 comments:

  1. In arralist get is constant time operation. Search ( contains ) is not constant time. It takes o(n) time

    ReplyDelete