Share ArrayList Between Classes in Java with Code

Learn how an ArrayList can be shared between different classes in a Java program. In other words, how to pass Arraylist from one class to another in java

We’ll create an ArrayList in the main() method and will share it to the two different classes i.e. the Read and the Write class.

The Write class will add elements to an ArrayList and another Read class will read elements from the same ArrayList.

Logic is simple.

Create a list using an java.util.ArrayList in the main() of the Sample class. Pass the same ArrayList to constructor of both Read and Write class constructor and initialize the list fields of both class. Perform operations add or remove on that list.

NOTE: If two objects are performing read and write operation on same shared list, then we need to synchronize the list to avoid exception. So, we will be Synchronizing the ArrayList using Collection.synchronizdList.

Code Example to Share ArrayList between classes

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/*
 * This class will add elements to 
 * a shared list that is created with an Arraylist.
 */

class Write {

	private List<String> list;

	// Receive a shared list in constructor
	Write(List<String> list) {
		this.list = list;
	}

	// add items
	public void addItem(String item) {

		this.list.add(item);
	}
}

/*
 * Read class will read elements form a shared list
 */
class Read {

	private List<String> list;

	// Receive shared list in constructor
	Read(List<String> list) {
		this.list = list;
	}

	// Read the items
	public void readItem() {

		for (String item : list) {
			System.out.println(item.toString());
		}

	}

}

// Test class.

public class Sample{
	
	public static void main(String[] args) {

		// Use synchronized list to avoid exception
		List<String> list = Collections
				.synchronizedList(new ArrayList<String>());

		Write w = new Write(list);
		Read r = new Read(list);

		w.addItem("Apple");
		w.addItem("Mange");

		r.readItem();
	}
}

Sharing ArrayList of user defined objects from one class to Another in java

We’ll learn how to import ArrayList of objects from one class to another class in Java

In this example, we’ll create a Book class which objects will be stored into an ArrayList.

We’ll have two more classes: MainBookStore , and NewBookStore.

We’ll create multiple objects of the Book class and store into the ArrayList of the MainBookStore.

And then, get the array list of book objects from the MainBookStore and assign to the ArrayList of the NewBookStore.

And then, we’ll print the name of the book and id into the NewBookStore.

CODE Example:

import java.util.ArrayList;

class Book {

	String name;
	int id;

	public Book(String name, int id) {
		this.name = name;
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public int getId() {
		return id;
	}
}

class MainBookStore {
	private ArrayList<Book> books;

	public ArrayList<Book> getAllBooks() {
		return books;
	}

	public MainBookStore() {
		books = new ArrayList<Book>();
	}

	public void add(Book object) {
		books.add(object);
	}
}

//Create a new book store and import all the 
//book list from main book store.
class NewBookStore {

	private ArrayList<Book> books;

	public void getAllBooksFromMainStore(ArrayList<Book> list) {
		// first syntax
		this.books = list;
		// Second syntax
		// this.books = (ArrayList<Book>) list.clone();

	}

	public void printBooks() {
		for (Book b : this.books) {
			System.out.println(b.getName() + " " + b.getId());
		}
	}
}

public class BookStoreOwner {

	public static void main(String[] args) {
		// Add book objects to the ArrayList of the storage class
		MainBookStore s = new MainBookStore();
		s.add(new Book("OOP Concepts Booster", 1));
		s.add(new Book("IT Jobs Made Easy for Freshers", 2));
		s.add(new Book("Head First Design Patterns", 3));

		// Create object of new book store
		NewBookStore bs = new NewBookStore();
		// Get book list to the new Store from main store;
		bs.getAllBooksFromMainStore(s.getAllBooks());
		bs.printBooks();

	}
}

OUTPUT:

OOP Concepts Booster 1
IT Jobs Made Easy for Freshers 2
Head First Design Patterns 3

Related Posts