Java Program to Print a Collection

JavaCampus InterviewServer Side ProgrammingProgramming

In this article, we will understand how to print a collection. The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.

Below is a demonstration of the same −

Suppose our input is

Run the program

The desired output would be

The Elements of the collection are:
Language : Java | Language_id : 101
Language : Scala | Language_id : 102
Language : Python | Language_id : 103
Language : Mysql | Language_id : 104

Algorithm

Step 1 - START
Step 2 - Declare a collection namely input_list
Step 3 - Define the values.
Step 4 - Create objects namely object_1 , object_2, object_3, object_4 and with each object , add a key value pair to the collection.
Step 5 - Using a for-each loop, display the elements of the collection
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

import java.util.*;
public class Demo {
   String name;
   int id;
   Demo(String s, int n){
      name = s;
      id = n;
   }
   public String toString(){
      return "Language : " + name + " | Language_id : " + id;
   }
   public static void main(String[] args){
      ArrayList<Demo> input_array = new ArrayList<Demo>();
      Demo object_1 = new Demo("Java", 101);
      Demo object_2 = new Demo ("Scala", 102);
      Demo object_3 = new Demo("Python", 103);
      Demo object_4 = new Demo("Mysql", 104);
      input_array.add(object_1);
      input_array.add(object_2);
      input_array.add(object_3);
      input_array.add(object_4);
      System.out.println("The Elements of the collection are: ");
      for (Demo element : input_array)
         System.out.println(element);
   }
}

Output

The Elements of the collection are:
Language : Java | Language_id : 101
Language : Scala | Language_id : 102
Language : Python | Language_id : 103
Language : Mysql | Language_id : 104

Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

import java.util.*;
public class Demo {
   String name;
   int id;
   Demo(String s, int n){
      name = s;
      id = n;
   }
   public String toString(){
      return "Language : " + name + " | Language_id : " + id;
   }
   static void print(ArrayList<Demo> input_array){
      System.out.println("The Elements of the collection are: ");
      for (Demo element : input_array)
         System.out.println(element);
   }
   public static void main(String[] args){
      ArrayList<Demo> input_array = new ArrayList<Demo>();
      Demo object_1 = new Demo("Java", 101);
      Demo object_2 = new Demo("Scala", 102);
      Demo object_3 = new Demo("Python", 103);
      Demo object_4 = new Demo("Mysql", 104);
      input_array.add(object_1);
      input_array.add(object_2);
      input_array.add(object_3);
      input_array.add(object_4);
      print(input_array);
   }
}

Output

The Elements of the collection are:
Language : Java | Language_id : 101
Language : Scala | Language_id : 102
Language : Python | Language_id : 103
Language : Mysql | Language_id : 104
raja
Updated on 30-Mar-2022 06:24:21

Advertisements