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
import java.util.*;
class GFG {
String name;
int rollNo;
GFG(String s, int n)
{
name = s;
rollNo = n;
}
public String toString()
{
return "Name : " + name + " | Roll No : " + rollNo;
}
public static void main(String[] args)
{
ArrayList<GFG> arr = new ArrayList<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 );
arr.add(t1);
arr.add(t2);
arr.add(t3);
arr.add(t4);
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
import java.util.*;
public class GFG {
String firstName;
String lastName;
GFG(String fn, String ln)
{
firstName = fn;
lastName = ln;
}
public String toString()
{
return "| First Name : " + firstName
+ " | LastName : " + lastName;
}
public static void main(String[] args)
{
HashMap<Integer, GFG> hm
= new HashMap<Integer, GFG>();
GFG p1
= new GFG( "Mohit" , "Singh" );
GFG p2
= new GFG( "Tarun" , "Anand" );
GFG p3
= new GFG( "Madhu" , "Singh" );
GFG p4
= new GFG( "Rohit" , "Ahuja" );
hm.put( 101 , p1);
hm.put( 102 , p2);
hm.put( 103 , p3);
hm.put( 104 , p4);
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 May, 2022
Like Article
Save Article