Skip to content
Related Articles

Related Articles

How to Print a Collection in Java?
  • Difficulty Level : Medium
  • Last Updated : 02 Nov, 2020

A 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- 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 ArrayList
  • Printing a user-defined HashMap

Approach 1: Printing a user-defined ArrayList

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);
    }
}

 
 

Output



Name : John | Roll No : 101
Name : Paul | Roll No : 102
Name : Jack | Roll No : 103
Name : Jose | Roll No : 10

Approach 2: Printing a user-defined HashMap
 

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);
  
        // printing the collection
        for (Map.Entry m : hm.entrySet())
            System.out.println(m.getKey() + " "
                               + m.getValue().toString());
    }
}

 
 

Output
101 | First Name : Mohit | LastName : Singh
102 | First Name : Tarun | LastName : Anand
103 | First Name : Madhu | LastName : Singh
104 | First Name : Rohit | LastName : Ahuja

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :