Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaHow to Print a Collection in Java?

How to Print a Collection in Java?

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

Approach 1: Printing a user-defined ArrayList

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


 
 

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

  • Create 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());
    }
}


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

 

RELATED ARTICLES

Most Popular

Recent Comments