Last Updated : 23 Jul, 2025
Collection is a set of objects that hold references to other objects in the program. In doing the same we already have studied a data structure- HashMap which internally works out the same way. So we come up with one of the approaches to print a Collection in java that is through HashMap. Now a step further we encounter- a collection framework.
The toString method is inherited by all the Classes in Java in order to print any collection in Java by overriding the toString method. After overriding, we can iterate through the collection using a for-each loop to print all the objects of the collection
Collections in java can be printed through 2 approaches which are:
Example
Java
// Java Program to print an arraylist of an
// user-defined collection
import java.util.*;
class GFG {
String name;
int rollNo;
// constructor of class GFG
GFG(String s, int n)
{
name = s;
rollNo = n;
}
// over-riding the toString method
// to print the collection
public String toString()
{
return "Name : " + name + " | Roll No : " + rollNo;
}
// Driver Main Method
public static void main(String[] args)
{
// creating an arraylist of user-defined collection
ArrayList<GFG> arr = new ArrayList<GFG>();
// creating objects of class GFG
GFG t1 = new GFG("John", 101);
GFG t2 = new GFG("Paul", 102);
GFG t3 = new GFG("Jack", 103);
GFG t4 = new GFG("Jose", 104);
// adding objects to arraylist
arr.add(t1);
arr.add(t2);
arr.add(t3);
arr.add(t4);
// printing the collection
for (GFG c : arr)
System.out.println(c);
}
}
Name : John | Roll No : 101 Name : Paul | Roll No : 102 Name : Jack | Roll No : 103 Name : Jose | Roll No : 10Approach 2: Printing a user-defined HashMap
Example
Java
// Java program printing ArrayList
// of an user-defined collection
// Importing Classes/Files
import java.util.*;
public class GFG {
String firstName;
String lastName;
// Constructor
GFG(String fn, String ln)
{
firstName = fn;
lastName = ln;
}
// Function- toString()
public String toString()
{
// Over-riding the toString method to print the
// collection
return "| First Name : " + firstName
+ " | LastName : " + lastName;
}
// Driver Main Method
public static void main(String[] args)
{
// Creating a hashmap with key as ID and
// value as user defined class
HashMap<Integer, GFG> hm
= new HashMap<Integer, GFG>();
// creating objects
GFG p1
= new GFG("Mohit", "Singh");
GFG p2
= new GFG("Tarun", "Anand");
GFG p3
= new GFG("Madhu", "Singh");
GFG p4
= new GFG("Rohit", "Ahuja");
// adding mappings
hm.put(101, p1);
hm.put(102, p2);
hm.put(103, p3);
hm.put(104, p4);
// Iterating HashMap through for loop and printing the collection
for (Map.Entry m : hm.entrySet())
System.out.println(m.getKey() + " "
+ m.getValue().toString());
}
}
101 | First Name : Mohit | LastName : Singh 102 | First Name : Tarun | LastName : Anand 103 | First Name : Madhu | LastName : Singh 104 | First Name : Rohit | LastName : Ahuja
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4