Java ArrayList Tutorial - The MEGA List

I have written several ArrayList tutorials, touching different ArrayList concepts and many how to do examples with ArrayList. In this tutorial, I am giving a summary of each of them. Why? So that any Java beginner who wants to learn ArrayList in detail, can go through the relevant tutorial and learn. It's also on request of many of my readers, who asked in past to share all the relevant tutorials in one place. Why should you learn ArrayList? Because it's the most important Collection class in Java. You will often find yourself using ArrayList and HashMap in tandem. It's your dynamic array which can resize itself as it grows. In another word, ArrayList is as much important as an array. When I started learning Java, my quest to ArrayList starts as a dynamic array, because there were many scenarios where we don't know the size of the array in advance. We end up either allocating less space or more space, both are not ideal. Btw, you should also check out Head First Java 2nd Edition if you are newbie and Effective Java 2nd Edition, if you know Java but wants to become a Java expert.


What is in this tutorial of ArrayList?

In this list you will find two kinds of Java ArrayList tutorials, first, how to do something with ArrayList e.g. how to declare ArrayList with values, how to sort ArrayList in reverse order, how to filter elements from ArrayList, how to convert ArrayList to an array and so on. Second type of tutorial is for concept building which is based upon different properties of ArrayList e.g. what is difference between Vector and ArrayList etc. These are also the type of question you will often see in phone round of Java Interviews. So, those will help you to do well there as well.



How to do ArrayList Tutorials

Below is one of the most comprehensive how to guide for Java ArrayList. You will learn almost everything about AraryList by going through thise list. These are also solution of many common requirement Java developers face in their day to day development work, ofcourse related to ArrayList.

How to initialize ArrayList in one line? (the trick)
Java doesn't support collection literals similar to array but you can initialize the arraylist in just one line, at the same time you declare it by following this nice little trick. It's better than another alternative called double brace initialization, which is considered anti-pattern in Java because it creates annonymous class everyime you use it to initialize the ArrayList.

How to remove Duplicates from ArrayList in Java? (solution)
Since ArrayList is subclass of List it does not prevent you from adding duplicate elements but if you really need an ArrayList of unique elements then we have a got a solution for you. You will learn a nice little trick to eliminate duplicate elements from ArrayList in Java.

How to reverse ArrayList in Java? (solution)
Since List doesn't provide a built-in reverse() method you need to do that by yourself. This tutorial will teach you a simple Java program to reverse the ArrayList in Java.

How to Synchronize ArrayList in Java? (tutorial)
ArrayList is not synchronized and that's why it's fast also, but if you have to share your ArrayList in a multi-threaded program, you better synchronize the list to avoid multi-threading problems like deadlock, corrupted objects, race conditions etc.

How to loop over ArrayList in Java? (example)
Simple Java program to learn how to loop over ArrayList in Java e.g. for loop, while loop, advanced for loop and by using Iterator.

How to sort ArryList in Java with ascending order? (example)
Remember, ArrayList is an ordered collection, which means the order you add elements are preserved, so if you are already added object in ascending order then you don't need to sort again, but if you have added elments in random order and wants them to arrange in ascending order, then this tutorial will help you.

How to use ArrayList in Java? (guide)
This is the beginner's guide to use ArrayList in Java. You will learn how to use different methods from java.util.ArrayList class e.g. add() to insert objects, remove() to delete objects, get() to retreive objects and contains() to check if an object is present in ArrayList or not.

How to convert ArrayList to String in Java? (tutorial)
Even though ArrayList has a toString() method it's not really helpful other than some debugging purpsoe. If you want to get all elemetns form ArrayList as comma separated String then there is no method in Java. This tutorial will teach you how to convert an ArrayList to delimited String in Java.

How to get subList from ArrayList in Java? (tutorial)
A simple Java program to demonstrate how to use subList() function from java.util.ArrayList to get the only some elements from ArrayList instead of all elements.

How to remove elements from ArrayList in Java? (guide)
There are multiple ways to remove objects from ArrayList but they don't work perfectly in all situation, for example you can use remove() method of java.util.ArrayList to take out objects from ArrayList but if you do so while iteating then you will face ConcurrentModificationException, in that situation you need to use iterator's remove() method. This tutorial will teach  you the right way to delete elements from ArrayList in Java.

How to convert ArrayList of String to Array of String? (tutorial)
One of the most common task in day to day programming is covnerting Arraylist to array and vice-versa. No mattter how carefully you design your software, you will find that doing quite often, many times due to using third-party libraries, which accept other type. I really hope Java auto-boxing should convert array to arraylist and hopefully it might do it in future, till then you can use this nice little trick.

How to make an ArrayList read only in Java? (tutorial)
Collections class provides several utility method to create wrapper objects e.g a read only wrapper of ArrayList, you can use Collections.unmodifiableList() method to make the arraylist read only.

How to avoid ConcurrentModificationException while iterating over ArrayList? (solution)
Don't use ArrayList's remove() method while iterating, if you have to remove elements during traversal use the Iterator's remove() method to avoid CME in Java.

Basic examples of ArrayList in Java (answer)
You will learn more by looking at examples because they contains more details. This tutorial contains some basic examples of using ArrayList class in Java.

How to sort ArrayList in descending order in Java? (answer)
This is the second part of sorting tutorial. Earlier you learn how to sort the ArrayList in ascending order and in this tutorial you will learn the other way, sorting ArrayList in decreasing order.

How to traverse over ArrayList in Java? (answer)
Simple Java program to traverse over ArrayList using Iterator and ListIterator in Java. You will also learn how to add and remove elements while iterating.

Java ArrayList Tutorial


ArrayList based Java Interview Questions

Here are some Java Interview Questions which is based upon ArrayList, you might have seen them already, but it's always good to revise if you want to learn the concept behind these questions. Here are some of the good Java Interview questions based upon ArrayList class:

What is the difference between an array and ArrayList in Java? (answer)
You cannot change the size of array once created but ArrayList can re-size itself. Also for same number of elements, array will take less memory than ArrayList.

When to use ArrayList and LinkedList in Java? (answer)
When you do search more often than addition or removal of objects then use ArrayList but if you keep find yourself adding new elements or removing old elements than use LinkedList.

The difference between ArrayList and HashSet in Java? (answer)
ArrayList is an ordered collection and allow duplicates but HashSet is a set, hence there is no ordering guarantee but it doesn't allow duplicates.

The difference between Vector and ArrayList in Java? (answer)
Vector is legacy class which was later retrofitted to implement List interface but its synchronized, hence slower, while AraryList is not synchronized and faster.

The difference between HashMap and ArrayList in Java? (answer)
HashMap is backed by hash table data structure while ArrayList is just a dynamic array. You need key and value to use HashMap but you can access elements using index in ArrayList.

Difference between length() of array and size() of ArrayList in Java? (answer)
one returns capacity other returns total number of elements currently present in ArrayList.

What is CopyOnWriteArrayList in Java? (answer)
A concurrent collection which allows multiple threads to read the list, get the elements without any synchronization.

What is difference between synchronized and CopyOnWriteArrayList in Java? (answer)
CopyOnWriteArrayList uses more sophisticated approach to achieve thread-safety. It also doesn't lock the ArrayList during read which synchronize list does.

What are different ways to remove objects from ArrayList in Java? (answer)
There are two ways to remove objects by using remove(object) and by using remove(index), but you should be careful if you are removing objects from ArrayList of integers because due to auto-boxing a call to remove(1) become ambiguous.

Can we declare ArrayList with values in Java? (answer)
Yes you can, see the answer for a simple code example.


That's all on this list of Java ArrayList tutorials. Once you will gone through these tutorials, you will learn almost everything about ArrayList including how it works and how to use the correctly. These how to tutorials are also recepie of many day to day problems Java developers face while doing Java development.


Further Reading
If you are very new to Java and still finding your feet in the langauge then you should check out Head First Java 2nd Edition but if you are someone who already Java and having couple of years of working experience in Java programming then I suggest you to read Effective Java 2nd Edition, that's the bridege between Java developer and a Java expert.


No comments:

Post a Comment