Table of Contents
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package org.arpit.java2blog; import java.math.BigDecimal; class Book{ private String title; private String author; private String publisher; private BigDecimal bookPrice; public Book(String title, String author, String publisher, BigDecimal bookPrice) { this.title = title; this.author = author; this.publisher = publisher; this.bookPrice = bookPrice; } public String getTitle() { return title; } public String getAuthor() { return author; } public String getPublisher() { return publisher; } public Long getBookPrice() { return Long.valueOf(String.valueOf(bookPrice)); } @Override public String toString() { return "Book{" + "title='" + title + '\'' + ", author='" + author + '\'' + ", publisher='" + publisher + '\'' + ", bookPrice=" + bookPrice + '}'; } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class ArrayListListOfObjects { public static void main(String[] args) { // Declaring ArrayList List<Book> listOfBooks =new ArrayList<>(); Book javaInAction = new Book("Java in action", "mary public", "Everest publishers", new BigDecimal("600")); Book introductionToJava = new Book("Introduction to Java", "mary public", "Heavyweight publishers", new BigDecimal("100")); Book advancedDatabases = new Book("Advanced databases", "charles darwin", "Longhorn publishers", new BigDecimal("600")); // Adding objects to ArrayList listOfBooks.add(javaInAction); listOfBooks.add(introductionToJava); listOfBooks.add(advancedDatabases); // Printing the ArrayList System.out.println(listOfBooks); } } |
Output:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ArrayListOfObjects { public static void main(String[] args) { //using add() method System.out.println("Books authored by Mary: ================"); ArrayList<Book> booksForMary = new ArrayList<>(); List<Book> maryPublicBooks = books.stream() .filter(book -> book.getAuthor().equals("mary public")) .collect(Collectors.toList()); for (Book book : maryPublicBooks) { booksForMary.add(book); } booksForMary.forEach(System.out::println); } } |
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}
Further reading:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; public class ArrayListOfObjects { public static void main(String[] args) { //using parameterized constructor ArrayList<Book> books = new ArrayList<>(List.of( new Book( "Java in action", "mary public", "Everest publishers", new BigDecimal("600")), new Book( "Introduction to Java", "mary public", "Heavyweight publishers", new BigDecimal("100")), new Book( "Advanced databases", "charles darwin", "Longhorn publishers", new BigDecimal("600")))); System.out.println("All books: ========================"); books.forEach(System.out::println); } } |
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}
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.Java2Blog; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ArrayListOfObjects { public static void main(String[] args) { //using addAll() method System.out.println("Books price > 100: ================"); ArrayList<Book> expensiveBooks = new ArrayList<>(); List<Book> bookList = books.stream() .filter(book -> book.getBookPrice() > 100) .collect(Collectors.toList()); expensiveBooks.addAll(bookList); expensiveBooks.forEach(System.out::println); } } |
Output:
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.