Collection can store multiple objects and provides different ways to access and display them. Collections containing user-defined objects can be printed by overriding the toString() method and then iterating through the collection.
- The toString() method can be overridden to provide a readable representation of user-defined objects.
- Collections such as ArrayList and HashMap can be printed using loops.
Approaches To Print a Collection
1. Printing a User-Defined ArrayList
To print objects stored in an ArrayList, override the toString() method in the user-defined class. Then, use a for-each loop to access and print each object.
import java.util.ArrayList;
class Employee {
String name;
int id;
// Constructor
Employee(String name, int id) {
this.name = name;
this.id = id;
}
// Override toString() method
@Override
public String toString() {
return "Employee Name: " + name + " | ID: " + id;
}
}
public class Main {
public static void main(String[] args) {
// Create an ArrayList of Employee objects
ArrayList<Employee> employees = new ArrayList<>();
// Add objects to the ArrayList
employees.add(new Employee("Amit", 201));
employees.add(new Employee("Neha", 202));
employees.add(new Employee("Rahul", 203));
employees.add(new Employee("Priya", 204));
// Print the collection
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
Output
Employee Name: Amit | ID: 201 Employee Name: Neha | ID: 202 Employee Name: Rahul | ID: 203 Employee Name: Priya | ID: 204
Explanation: The Employee class overrides the toString() method to define how each object should be displayed. The objects are stored in an ArrayList, and the for-each loop prints each object. When System.out.println(employee) is executed, Java automatically calls the overridden toString() method.
2. Printing a User-Defined HashMap
A HashMap stores data in key-value pairs. When the value is a user-defined object, the toString() method can be overridden to provide a readable representation of that object. The entrySet() method can then be used to iterate through the map.
import java.util.HashMap;
import java.util.Map;
class Product {
String name;
double price;
// Constructor
Product(String name, double price)
{
this.name = name;
this.price = price;
}
// Override toString() method
@Override public String toString()
{
return "Product: " + name + " | Price: " + price;
}
}
public class Main {
public static void main(String[] args)
{
// Create a HashMap with ID as key
// and Product object as value
HashMap<Integer, Product> products
= new HashMap<>();
// Add key-value pairs
products.put(101, new Product("Laptop", 55000));
products.put(102, new Product("Keyboard", 1500));
products.put(103, new Product("Mouse", 800));
products.put(104, new Product("Monitor", 12000));
// Print the HashMap
for (Map.Entry<Integer, Product> entry :
products.entrySet()) {
System.out.println(entry.getKey() + " "
+ entry.getValue());
}
}
}
Output
101 Product: Laptop | Price: 55000.0 102 Product: Keyboard | Price: 1500.0 103 Product: Mouse | Price: 800.0 104 Product: Monitor | Price: 12000.0
Explanation: The HashMap stores product IDs as keys and Product objects as values. The entrySet() method provides access to each key-value pair. entry.getKey() prints the product ID, while entry.getValue() prints the Product object using its overridden toString() method.
Note: HashMap does not guarantee the order of its elements. Therefore, the output order may vary.