Hello guys, if you are preparing for Java interviews then you have come to the right place. In the past, I have shared 130+ Java interviews questions and online courses to prepare for Java interviews and In this article, I am going to share some of the good Java interview questions based upon the 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 questions to really check the breadth and depth of a candidate's knowledge.
To give you some idea about ArrayList, it's a collection class that implements the List interface. It's an alternative to an array data structure whose size you cannot change once created. ArrayList is a dynamic array, which can grow and resize itself.
By implementing the List interface it also got some properties like 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 like 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 a little costly because it can trigger re-sizing, which involves creating a new array and copying all elements from the old array to a 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.
I am sure you know the basics but you will learn more by going through some of these frequently asked Java ArrayList questions.
Btw, if you struggle to answer these ArrayList questions then I highly recommend you go through these Java collection online courses. This is the most up-to-date and comprehensive course to learn Java and fill the gaps in your understanding. I highly recommend this to every Java programmer.
20+ ArrayList Questions with Answers from Java Interviews
So without wasting any more time, let's start with the questions. The questions are mainly divided into two categories, one is a fact-based question, which checks how much you know about ArrayList, and the other is the task-based question, which evaluates how good you are at 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 a task-based question. Since the 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 the answer for a more detailed explanation.
2. How to reverse ArrayList in Java? (Answer)
You can reverse ArrayList by using the Collections.reverse() method. There are a couple of more ways like iterating through the list and copying elements into a new list. See the answer for more ways to do this task.
And if you want to learn more about ArrayList and other collection classes, you can also check out Java Fundamentals: Collections course by Richard Warburton on Pluralsight. Richard is a Java champion and this course is the best online course to learn the Collection framework in depth.
3. Difference between an array and an ArrayList in Java? (Answer)
This is a fresher-level interview question, the main difference between array and ArrayList is that the former is static and the latter is dynamic. You cannot change the size of the 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 the answer to understand the 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 structures, array, and linked list.
Since array provides constant-time search operation, it's better to use ArrayList if search outnumbers add and remove operation, otherwise use LinkedList which provides constant time add and remove operation. See the answer for a more detailed discussion on this topic.
6. Difference between ArrayList and HashSet in Java? (Answer)
One of the simplest questions you will ever see on a Java interview. The main difference is the former is List while the 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 the new foreach loop introduced in Java 5, which doesn't require an index. See the answer to 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 implement List interface, Vector is synchronized while ArrayList is not synchronized, which means the former is thread-safe and fast while the latter 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 the Arrays.asList() method, but remember the List returned by this class has some differences 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 the ArrayList by using Collections.sort() method, all you need to make sure is that elements implement either the Comparable or Comparator interface. The former is used to sort on natural order while the latter is used while sorting in the custom order.
11. Difference between HashMap and ArrayList in Java? (Answer)
There is a huge difference between HashMap and ArrayList, fundamental is former is a Map data structure that stores key-value pairs while the latter stores just an object. HashMap access object using key while ArrayList access elements using an 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 the answer page.
14. How to get a 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 the subList() method from the ArrayList class. This would be very helpful in case of a sorted list.
15. Difference between length() of array and size() of ArrayList in Java? (Answer)
This is one of the tricky questions, if you get the backed array and call the length() it will return how many elements you can store in this array, also known as capacity, but if you call the size() function of ArrayList class then it will return a total number of elements currently stored in ArrayList, which is always less than or equal to capacity.
16, What is CopyOnWriteArrayList in Java? (Answer)
It's a concurrent collection class that is introduced as an alternative to synchronized List in Java. This class takes advantage of the advanced thread-safety technique instead of locking. It's very efficient if ArrayList is mostly used for reading purposes because it allows multiple threads to read data without locking, which was not possible with synchronized ArrayList. See the answer to learn more about the 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 the remove(int index) method and pass on the index of the element you want to remove, and second, you can call the 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 an ambiguity between two remove methods. See the answer for a 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 are about the Collections utility class, which provides several wrappers of the standard ArrayList class e.g. you can use Collections to create a 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 questions related to sorting. By default, elements are sorted in 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 the Collections.reverseComparator() method.
And, if you need more ArrayList questions for practice then here is a list of new ArrayList questions to answer:
- How do you add multiple string values to an Arraylist in Java?
- How to convert Arraylist to XML in Java?
- How to print string array list in java?
- How do you create an Arraylist in Excel in Java?
- How to write an Arraylist of objects to a text file in Java?
- How to remove special characters from Arraylist in Java?
- How do you make a generic Arraylist in Java?
- How to iterate an Arraylist inside a Hashmap using Java?
- How to get key and value from Arraylist in Java?
- How to find even numbers in an Arraylist in Java?
- How to convert Arraynode to Arraylist in Java?
- How do you initialize a two dimensional Arraylist in Java?
- How to parse JSON Arraylist in Java?
- How to retrieve object values from Arraylist in Java 8?
- How to check if an Arraylist is sorted in Java?
- How to find the difference between two Arraylists in Java?
- How to convert Arraylist to CSV in Java?
- How to sort 2 Arraylist in Java?
- How do you add two values to an Arraylist in Java?
- How to get key from Arraylist in Java?
- How do you write a loop for an Arraylist in Java?
- How do you declare an Arraylist variable in Java?
- What is the size of empty Arraylist in Java?
- How do you add a unique element to an Arraylist in Java?
- How do you remove blank spaces from an Arraylist in Java?
- How to print duplicate values in Arraylist in Java?
- How to get an Arraylist from another method in Java?
- How do you implement an Arraylist in Java?
- How do you remove duplicate employee objects from Arraylist in Java?
- How do you write an Arraylist to a text file in Java?
- How do you remove one element from an Arraylist in Java?
- How do you add an Arraylist to a string array in Java?
- How do you create an Arraylist of a class object in Java?
- How to inherit Arraylist in Java?
- How do you fill an array list in java?
- How do you make an Arraylist an array in Java?
- How to compare two Arraylists of different size in Java?
- How to validate Arraylist in Java?
- How to compare two Arraylist of objects in Java and find difference?
- Can Arraylist be static in Java?
- How do you remove square brackets from an Arraylist in Java?
- How to get specific object from Arraylist in Java 8?
- How do you create an Arraylist list in Java?
- How do you initialize an Arraylist of objects in Java?
- How to iterate Arraylist of Linkedhashmap in Java?
- How to add elements to an Arraylist in Java using for loop?
- How do you fill an Arraylist in Java?
- How to write an Arraylist to a CSV file in Java?
- What is the initial size of Arraylist in Java?
- How do you add elements to an Arraylist for a loop in Java?
- How do you pass an Arraylist as an argument in Java?
- How to assign values to an Arraylist in Java?
- How do you assign a value to an Arraylist in Java?
- How do you add an Arraylist to an array in Java?
- How many elements can an Arraylist hold Java?
- How do you return an Arraylist to a string in Java?
- How do you add different objects to an Arraylist in Java?
- How to store array list in java?
- How do you add a key value pair to an Arraylist in Java?
- How to Add Employee object to Arraylist in Java?
- How do you make an Arraylist anonymous in Java?
- How to get user input in Arraylist in Java?
- How do I change the order of an arraylist in Java?
- How to get value from Arraylist of objects in Java?
- How do you process an Arraylist in Java?
- How do you make an Arraylist immutable in Java?
- How to store Arraylist data into Excel in Java?
- How do you add an int to an Arraylist in Java?
- How to process Arraylist in Java?
- How to add object array to Arraylist in Java?
- How do you assign a string value to an Arraylist in Java?
- How to get input from user in Arraylist in Java?
- What does Arraylist mean in Java?
- How to create a float Arraylist in Java?
- How to add employee objects to Arraylist in Java?
- How to override equals method in Arraylist Java?
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 give more importance to coding skills can also ask you to implement your own ArrayList class in Java, so be prepared for that also.
And if you really want to do well on Java interviews, I suggest taking a look at these Java interview courses from my curated list. It's one of the best online courses to prepare Java interviews. It contains questions from Core Java, Multithreading, GC, JVM internals, and related technology questions like Maven, JUnit, Servlet, JSP, Android, etc.
The best thing about this course is how well they explain the answer. You will even learn from this book and be able to fill the gaps in your understanding.
Related Interview Questions from Java67 blog
- 10 most asked SQL Query Interview Questions
- 10 tricky questions asked on Java Interviews
- Java Interview Questions for 3 years experienced Programmers
- 10 ways to use HashMap in Java
- 7 Best Data Structure and Algorithms Courses
- How to get a method of HashMap works in Java
- 10 Free Java Programming Courses for Beginners
- 20 Spring and REST Interview Questions
- 10 Best Spring Framework Courses for Java Programmers
- 75 Coding Questions to Crack Any Programming Interview
- 10 Advanced Spring Boot Courses for Java developers
- 100+ Data Structure and Algorithm Questions
- 10 Courses to learn Java for Beginners
- My Favorite books to learn Java Programming in depth
Thanks for reading this article so far. If you like these Java ArrayList interview questions or have seen them on your telephonic round of interviews, then please share this post with your friends and colleagues on Facebook, Twitter, Email, etc. If you have any questions or feedback, please drop a note.
P.S. - If you are preparing for a Programming Job Interview and you need more such questions, can check these Data Structures and Algorithms courses from Coursera, Udemy, and Educative to refresh your knowledge in quick time.
very much helpful...
ReplyDeleteIn arralist get is constant time operation. Search ( contains ) is not constant time. It takes o(n) time
ReplyDeletegreat post
ReplyDeleteAWESOME QUESTIONS WITH SOLUTION/EXPLANATION
ReplyDeleteHow to arrabge the list of objects with a specific attribute of object. Suppose arrange a list of enployess using employee id
ReplyDeleteNice article amazing keep it up bro
ReplyDeleteThank you
ReplyDelete