Friday, August 23, 2024

Java ArrayList Tutorials and Examples for Beginners (with Java ArrayList Cheat Sheet)

Hello guys, if you want to learn ArrayList in-depth and looking for a complete guide on ArrayList then you have come to the right place. Earlier, I have shared the best Java collection courses and in this article, I am going to share tutorials and examples to learn and master ArrayList in Java. In the last 10 years, 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 at the request of many of my readers, who asked in the 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 that 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 a newbie and Effective Java 2nd Edition if you know Java but wants to become a Java expert.


What will you learn in this Java ArrayList tutorial?

In this list, you will find two kinds of Java ArrayList tutorials, first, how to do something with ArrayList like 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. 

The second type of tutorial is for concept building which is based on different properties of ArrayList like what is the difference between Vector and ArrayList etc. These are also the type of questions you will often see in a phone round of Java Interviews. So, those will help you to do well there as well.

Also read till the end, I have a Java ArrayList cheat sheet for you. 


How to do ArrayList Tutorials?

Below is one of the most comprehensive how-to guides for Java ArrayList. You will learn almost everything about ArrayList by going through this list. These are also solutions to many common requirements Java developers face in their day-to-day development work, of course, related to ArrayList.

How to initialize ArrayList in one line? (the trick)
Java doesn't support collection literals similar to an 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 an anti-pattern in Java because it creates an anonymous class every time you use it to initialize the ArrayList.

How to Remove Duplicates from ArrayList in Java? (solution)
Since ArrayList is a 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 ArrayList in Java in 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 elements 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 using 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 retrieve 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 help other than some debugging purpose. If you want to get all elements from ArrayList as comma-separated String then there is no method in Java. This tutorial will teach you how to convert an ArrayList to a 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 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 situations, for example, you can use the remove() method of java.util.ArrayList to take out objects from ArrayList but if you do so while iterating 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 tasks in day-to-day programming is converting Arraylist to array and vice-versa. No matter how carefully you design your software, you will find that doing quite often, many times due to using third-party libraries, which accept other types. I really hope Java auto-boxing should convert the array to ArrayList and hopefully, it might do it in the 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 methods to create wrapper objects e.g a read-only wrapper of ArrayList, you can use the 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 contain more details. This tutorial contains some basic examples of using the ArrayList class in Java.

How to sort ArrayList in descending order in Java? (answer)
This is the second part of the 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.



ArrayList based Java Interview Questions?

Here are some Java Interview Questions which are 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 the ArrayList class:

What is the difference between an array and ArrayList in Java? (answer)
You cannot change the size of the array once created but ArrayList can re-size itself. Also for the same number of elements, the 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 finding yourself adding new elements or removing old elements then use LinkedList.

The difference between ArrayList and HashSet in Java? (answer)
ArrayList is an ordered collection and allows 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 a legacy class that was later retrofitted to implement the List interface but it's synchronized, hence slower, while ArrayList 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 the total number of elements currently present in ArrayList.

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

What is the difference between synchronized and CopyOnWriteArrayList in Java? (answer)
CopyOnWriteArrayList uses a more sophisticated approach to achieve thread safety. It also doesn't lock the ArrayList during the reading which the synchronized 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.


Java ArrayList Cheat Sheet

And, here is the Java ArrayList cheat sheet I was talking about. This cheat sheet provides a quick reference for common ArrayList operations in Java, including how to create an ArrayList, add and remove elements, access elements, iterate through the list, and use some utility methods.


Java ArrayList CheatSheet


That's all on this list of Java ArrayList tutorials. I have given you all the information you will need to start using ArrayList in Java and you can also read the code which uses ArrayList in Java. I have also shared a Java ArrayList cheat sheet for quick reference. 

Other Java Collection tutorials you may like

Once you will go through these tutorials, you will learn almost everything about ArrayList including how it works and how to use them correctly. These how-to tutorials are also recipes of many day-to-day problems Java developers face while doing Java development.

2 comments:

  1. Hey can you solve this pattern plz

    * * * *
    * * *
    * * * *
    * * *
    * * * *
    * * *
    * * * *

    ReplyDelete
  2. 1
    9 2
    10 8 3
    14 11 7 4
    15 13 12 6 5

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.