
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print a Collection in Java
In this article, we will understand how to print a collection in Java. 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.
Problem Statement
Write a program in Java to print a Collection. Below is a demonstration of the same ?
Input
Run the program
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
Different approaches to print a collection in Java
Following are the different approaches to print a collection in Java ?
Using the main method
Following are the steps to print a collection in Java ?
- First we will import all the necessary classes from java.util package.
- Intialize the public class which is the Demo class.
- Create a Demo class with attributes name (String) and id (int) and initialize the Demo class attributes using a constructor.
- And use the toString() method to print name and id in a formatted way.
- Inside the main method, create an ArrayList of Demo objects.
- Add several Demo objects to the ArrayList.
- Use a for-each loop to iterate through and print each element of the ArrayList.
Example
Here, we bind all the operations together under the ?main' method ?
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
Using encapsulation
Following are the steps to print a collection in Java using encapsulation ?
- Import necessary classes from java.util package.
- Create a Demo class with attributes name (String) and id (int).
- Initialize the Demo class attributes using a constructor.
- And we will use toString() method to print name and id in a formatted way.
- Define a static method named print to encapsulate the logic of printing the collection.
- Inside the main method, create an ArrayList of Demo objects.
- Add several Demo objects to the ArrayList.
- Call the print method to print all elements of the collection.
Example
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