Create ArrayList of Objects in Java

Java ArrayList of objects

In this tutorial, we will learn how to create ArrayList of objects in Java. We will create a Book class with different properties and use it to create custom book objects when creating an ArrayList of objects.

We will filter these book objects using certain criteria and add the book objects that meet the criteria in a new ArrayList.

The following is the class declaration that we will use to instantiate our custom objects and the fields include title, author, publisher, and bookPrice.

Create a book class with the properties and generate getter methods to retrieve their values when filtering. Generate a toString() method which will enable us to log the different values filtered to the console

Using the add() method to create ArrayList of objects in java

You can simply use add() method to create ArrayList of objects and add it to the ArrayList. This is simplest way to create ArrayList of objects in java.
Here is quick example:

Output:

[Book{title=’Java in action’, author=’mary public’, publisher=’Everest publishers’, bookPrice=600}, Book{title=’Introduction to Java’, author=’mary public’, publisher=’Heavyweight publishers’, bookPrice=100}, Book{title=’Advanced databases’, author=’charles darwin’, publisher=’Longhorn publishers’, bookPrice=600}]

Let’s take another example where we will filter all the books authored by Mary and add the books to a new ArrayList using the add() method.

We will use the Java 8 Stream which provides us with an efficient way to filter our list of objects.

Output:

Books authored by Mary: ================
Book{title=’Java in action’, author=’mary public’, publisher=’Everest publishers’, bookPrice=600}
Book{title=’Introduction to Java’, author=’mary public’, publisher=’Heavyweight publishers’, bookPrice=100}

Using parameterized constructor to create ArrayList of objects in java

The ArrayList class has a constructor that accepts a collection of objects that we will initialize with book objects.

Create a new ArrayList with custom book objects by passing a List containing our book objects to this ArrayList’s constructor.

Call the forEach() method on the ArrayList created to print all the objects that were created during the ArrayList initialization.

Output:

All books: ========================
Book{title=’Java in action’, author=’mary public’, publisher=’Everest publishers’, bookPrice=600}
Book{title=’Introduction to Java’, author=’mary public’, publisher=’Heavyweight publishers’, bookPrice=100}
Book{title=’Advanced databases’, author=’charles darwin’, publisher=’Longhorn publishers’, bookPrice=600}

For the next examples, we will use this list of books to create a new ArrayList of objects by filtering this list and adding the filtered objects to the new list.

Using addAll() method to create ArrayList of objects in java

In this example, we will filter all the books where the price is greater than 100 and add the objects returned to a new ArrayList using the addAll() method.

This method will return a list containing our filtered objects and we use the forEach() method to print the objects in the new ArrayList.

Output:

Books price > 100: ================
Book{title=’Java in action’, author=’mary public’, publisher=’Everest publishers’, bookPrice=600}
Book{title=’Advanced databases’, author=’charles darwin’, publisher=’Longhorn publishers’, bookPrice=600}

Conclusion

In this tutorial, we have learned how to create an ArrayList of objects by using the following approaches: using parameterized constructor during ArrayList initialization, using the add() method, and using the addAll() method.

That’s all about how to create list of objects in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *