Thursday, September 14, 2023

16 Examples of ArrayList in Java - Tutorial

Most of the Java interview questions on ArrayList asked freshers or Java developers with 1 to 2 years experience is just simply how to do tasks like how to sort ArrayList in Java, How to sort ArrayList in ascending and descending order, how to sort ArrayList on reverse order, how to search elements, how to remove an element using iterator etc. Since, I have written lots of Java tutorials on ArrayList, covering many general-purpose tasks like how to create an object of ArrayList and initialize to how to sort ArrayList in ascending and descending order, etc. 

At the request of my readers, I am creating this mega list of ArrayList tutorials, which will help you to learn everything about the ArrayList class. These tutorials are great for beginners and even experienced Java developers can learn a trick or two.

Once you have gone through these tutorials, you should be comfortable using ArrayList along with Generics. I have tried to cover as many how to do something in the ArrayList task as possible, but if you think something is not covered then please let me know and I will write about it.

If you are facing any challenges related to using ArrayList or doing something with ArrayList, feel free to post in the comments section and we'll look together.


ArrayList in Java - Examples, and Tutorial

Here is my list of tutorials on Java ArrayList. Each tutorial explains a particular concept and how to do something in ArrayList with simple, easy to understand example :

1. How to use ArrayList in Java (tutorial)

This is the beginner's guide to Java ArrayList. You will learn how to add, remove, and search elements in ArrayList using add(), remove() and contains() method. You will also learn how to get the size of ArrayList, how to get Iterator from ArrayList to traverse, and how to clear ArrayList for further reuse.


2. How to convert an Array to ArrayList and vice-versa? (tutorial)

This is another beginner's tutorial on Java ArrayList. Since ArrayList is backed by the array, it's easy to convert an array to ArrayList and vice-versa. In this tutorial, you will learn how to do that in multiple ways.




3. How to loop over ArrayList in Java? (tutorial)

In this Java ArrayList tutorial, you will learn how to loop over an ArrayList in Java. There are multiple ways to loop over ArrayList e.g. you can use new for loop introduced in Java 5, or you can use traditional for loop because ArrayList supports random access using the index as well.


4. How to convert ArrayList to Set in Java? (tutorial)

Often you need to convert one Collection to another and in this Java tutorial, you will learn how to convert ArrayList to HashSet in Java. Just remember that when you convert a List to a Set, you lose the ordering which is provided by List interface and any duplicate elements because Set doesn't allow duplicates.


5. How to sort ArrayList in Java? (tutorial)

Though ArrayList keeps the element in the order they are inserted it doesn't provide automatic sorting, but you can sort an ArrayList on-demand using the Collections.sort() method. In this Java ArrayList tutorial, you will learn how to sort ArrayList in their natural order using Comparable and any custom order using Comparator. You will also learn how to sort ArrayList in ascending and descending order.


6. How to synchronize ArrayList in Java? (tutorial)

Unlike Vector, ArrayList is not synchronized, which means you cannot share ArrayList between multiple threads if one of them structurally modifies the list e.g. add(), remove() or update elements, but, fortunately, you can synchronize the list. In this Java tutorial, you will learn how to synchronize ArrayList in Java.


7. How to remove elements from ArrayList in Java? (tutorial)

One of the most common tasks we do with ArrayList in our day-to-day programming is adding or removing elements. In these Java tutorials, you will learn how to remove objects from ArrayList using both the remove(int index) and remove(Object obj) methods. 

You will also understand the difference between these two methods and how auto-boxing can spoil the party sometimes. If you want to learn more, you can also see these Java collections and Stream API courses for beginners and experienced programmers. 

Java ArrayList tutorial


8. How to remove duplicates from ArrayList? (tutorial)

This is an extension of the previous tutorial, instead of removing elements, you will learn how to remove duplicates from ArrayList. Since List interface allows duplicate elements, they sometimes creep into ArrayList. Here you will learn how to get rid of them.


9. How to initialize ArrayList with an array in Java? (tutorial)

ArrayList is backed by an array and it's also easy to initialize an ArrayList by copying content from an Array in Java. Like several other utility methods, the Arrays class also provides a static utility method to initialize ArrayList from an array in Java. You can use Arrays.asList() method to do this job.


10. How to create read-only ArrayList in Java? (tutorial)

A read-only ArrayList is the one on which you can not insert or delete any element.  You cannot update any element and so on. This is also known as an immutable ArrayList in Java and you can create one by using the Collections class. You will learn how to do that in this Java ArrayList tutorial.


11. What is ArrayList Performance Improvement in Java 7? (tutorial)

JDK 7 has done a great job in improving the performance of various classes and two of them is ArrayList and HashMap, coincidentally the two of the most used class from the Java Collection framework as well. Find out the improvement done in this tutorial.


12. How to reverse an ArrayList in Java? (tutorial)

One of the common tasks is to reverse the order of elements stored inside ArrayList. Unlike StringBuffer, there is no reverse() method in ArrayList to do this but you can use the Comparator class to reverse an ArrayList in Java. Sometimes this is also asked as an interview question to reverse an ArrayList using recursion, see Java Programming Interview Exposed for more details.




13. How to initialize ArrayList in one line? (tutorial)

Sometimes it's easy to initialize ArrayList with some test data for quick testing.  In this Java tutorial, you will learn a nice trick to create and initialize the ArrayList in the same line. You can apply this trick to any type of ArrayList.


14. How to convert ArrayList to String in Java? (tutorial)

The string is no doubt the most used type in Java. It is often used to transfer data from one class to another, one module to another, and so on. That's why you often need to convert an ArrayList to String in Java. Unfortunately, JDK API doesn't provide a direct method to do this, but in this tutorial, you will learn how you can do it by yourself.


15. How to convert String ArrayList to Array of String? (tutorial)

This is an extension of the previous tutorial. In the last ArrayList tutorial, we have learned how to convert ArrayList to String but here we will learn how to convert ArrayList to an array of String objects. Since ArrayList is backed by an array itself, it's not that difficult to carry out this task.


16. How to get a sublist from ArrayList in Java? (tutorial)

In this tutorial, you will learn how to get a sublist of elements from ArrayList in Java. You can get the subset of elements from any specified range by using methods provided by java.util.ArrayList class. But, worth remembering is the point that the sublist is backed by the original list and if you remove any element from the sublist then it will also get removed from the original ArrayList.


That's all in this list of Java ArrayList tutorials. You can run those programs in your Eclipse IDE or just from the command line to learn more. If you face any issue while doing something with ArrayList like searching, sorting, looping, converting, adding elements or removing elements, you can always post here.

Other Java Interview Questions Articles.
  • 21 HashMap Interview Questions with answers (hashmap questions)
  • Top 10 Spring Framework Interview Questions with Answers (see here)
  • 10 ConcurrentHashMap Interview questions with answers (concurrenthashmap questions)
  • 25 System Design Interview Questions for Programmers (questions)
  • 20 Docker Interview Questions with answers (docker questions)
  • 15 Data Structure and Algorithm Questions from Java Interviews (read here)
  • 20 Kubernetes Interview Questions with answers (K8s questions)
  • 20  Java Design Pattern Questions asked on Interviews (see here)
  • 25 DevOps Interview Questions with answers (DevOps questions)
  • 10 frequently asked Servlet Interview Questions with Answers (see here)
  • 20+ Array-based coding questions from Interviews (Questions)
  • Top 10 JSP Questions  from J2EE Interviews (read here)
  • 20+ binary tree coding problems for Java programmers (questions)
  • 10 Hibernate Interview Questions for Java EE Developer (see here)
  • 12 RESTful Web Services Questions from Interviews (read here)
  • 21 String Algorithms problems from Coding interview (questions)
  • 20+ linked list coding problems from interviews (questions)
  • 10  JDBC Interview Questions for Java Programmers (questions)
  • 15 Java NIO and Networking Interview Questions with Answers (see here)
  • Top 40 Core Java Phone Interview Questions with Answers (list)
  • Top 10 Trick Java Interview Questions and Answers (see here)
  • 20+ String-based Coding Problems from Interviews (questions)

Thanks for reading this article so far. If you like these ArrayList  examples and Interview questions, then please share them with your friends and colleagues too. If you have any questions or feedback, then please drop a note.

P. S. - If you are looking to fill gaps in your Java knowledge or want to learn Java from scratch, here are some useful free Java online courses to check out at your convenience; they are good for online learning from your office or home.

Also, what is your favorite way ArrayList interview question? Let me know in comments. 

1 comment :

Post a Comment