Loading...

How to do with ArrayList in Java - Tutorial

I have written lots of Java tutorials on ArrayList, covering many general purpose task e.g. how to create object of ArrayList and initialize to how to sort ArrayList in ascending and descending order etc. On request of my readers, I am creating this mega list of ArrayList tutorials, which will help you to learn everything about 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 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 challenge related to using ArrayList or doing something with ArrayList, feel free to post in comments section and we'll look together. BTW, if you are really serious about mastering Java Collection framework, you can also take a look at one of the best book on topic, Java Generics and Collection by Maurice Naftaline. He has covered almost all Collection classes along with tricky Generics, which is required to become an expert Java developer.


How to ArrayList in Java

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 :


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.


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


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 index as well.


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.


How to sort ArrayList in Java? (tutorial)
Though ArrayList keeps element in the order they are inserted it doesn't provide automatic sorting, but you can sort an ArrayList on demand using 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.


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.


How to remove elements from ArrayList in Java? (tutorial)
One of  most common task we do with ArrayList in our day to day programming is adding or removing elements. In this Java tutorials, you will learn how to remove objects from ArrayList using both remove(int index) and remove(Object obj) method. You will also understand the difference between these two methods and how auto-boxing can spoil party some time.

Java ArrayList tutorial


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


How to initialize ArrayList with array in Java? (tutorial)
ArrayList is backed by array and its also easy to initialize an ArrayList by copying content from an Array in Java. Like several other utility methods, Arrays class also provides static utility method to initialize ArrayList from array in Java. You can use Arrays.asList() method to do this job.


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 immutable ArrayList in Java and you can create one by using Collections class. You will learn how to do that in this Java ArrayList tutorial.


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


How to reverse ArrayList in Java? (tutorial)
One of the common task 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 Comparator class to reverse an ArrayList in Java.


How to initialize ArrayList in one line? (tutorial)
Sometime its 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 same line. You can apply this trick to any type of ArrayList.


How to convert ArrayList to String in Java? (tutorial)
String is no doubt the most used type in Java. It often used to transfer data between 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.


How to convert String ArrayList to Array of String? (tutorial)
This is extension of previous tutorial. In last ArrayList tutorial, we have learned how to convert ArrayList to String but here we will learn how to convert ArrayList to array of String object. Since ArrayList is backed by an array itself, its not that difficult to carry out this task.


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


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

Recommended books for further reading 

  • Java Generics and Collection by Maurice Naftaline (check here)
  • Effective Java 2nd Edition by Joshua Bloch (check here)

No comments :

Post a Comment