How to Print a Collection in Java? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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: Printing a user-defined ArrayListPrinting a user-defined HashMapApproach 1: Printing a user-defined ArrayListCreate an ArrayList of the user-defined objects and populate the ArrayList.Overrider the toString() method in the user-defined class to print the item of the ArrayList in the desired format.Run a for-loop to print the objects.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); } } OutputName : John | Roll No : 101 Name : Paul | Roll No : 102 Name : Jack | Roll No : 103 Name : Jose | Roll No : 10Approach 2: Printing a user-defined HashMapCreate a hash map with user-defined key and value pair and fill the hash map using put() method.Make sure to Override the toString() method in the user-defined class to print the items in the desired format.Iterate over the hash map using the EntrySet() for loop and print the elements.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()); } } Output101 | First Name : Mohit | LastName : Singh 102 | First Name : Tarun | LastName : Anand 103 | First Name : Madhu | LastName : Singh 104 | First Name : Rohit | LastName : Ahuja Comment More info P Priyank181 Follow Improve Article Tags : Java Java Programs Java-Collections Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like