
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Print a Collection
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
- Related Articles
- Java Program to Reverse a Collection
- Golang program to print the inverted hash collection
- Java Program to Print a String
- Java Program to Compare Elements in a Collection
- Golang program to iterate hash collection and print only values
- Java program to print a Fibonacci series
- Java program to print a prime number
- Java Program to Use Different Types of a Collection
- Java Program to Shuffle the Elements of a Collection
- Java Program to Convert Collection into Array
- Java Program to Convert Array into Collection
- Java program to print a given pattern. (stars)
- Java Program to Print an Integer
- Java program to print the transpose of a matrix
- Java program to print unique values from a list

Advertisements