Advertisement



< Prev
Next >


User-Defined Objects in Collections


Collection classes are used to store built-in objects such as Integer, String, Character etc. But the potential of collection classes isn't just restricted to the storage of built-in objects. Collections classes can store any similar type of objects, be it objects of a built-in class or objects of an user-defined class. This feature of collection classes is very useful when we are making an application which requires us to store many objects of user-defined classes and not just objects of built-in classes.


Advertisement




Let's understand this by an example -:


In this example, we have created a Customer class with three instance variables - name, balance and ID, together they make up an object of Customer class. We are going to use ArrayList collection class to store objects of this Customer class.

import java.util.*;

class Customer
{
String name;
int balance;
int id;


//Costructor 
Customer(String s, int i, int j)
{
name=s;
balance=i;
id=j;
}


//toString() method is overridden to give a meaningful String representaion of each object.
public String toString() 
{
return "|Name : "+ name + "|Balance : "+ balance + "|ID : " + id + "|\n";
}


public static void main(String... ar)
{
ArrayList<Customer> arr= new ArrayList<Customer>(); //ArrayList will contain a collection of Customer's objects.

//Creating Customer objects.
Customer customer1 = new Customer("Jay", 1000, 2);
Customer customer2 = new Customer("Shane", 7000 3);
Customer customer3 = new Customer("Ricky", 5000, 1);
Customer customer4 = new Customer("Tom", 3000, 6);
Customer customer5 = new Customer("Mick", 6000, 4);

//Storing objects in an ArrayList collection class.
arr.add(customer1);
arr.add(customer2);
arr.add(customer3);
arr.add(customer4);
arr.add(customer5);

for(Customer c : arr)
System.out.println(c);

}

}



Output is :

|Name : Jay|Balance : 1000$|ID : 2|

|Name : Shane|Balance : 7000|ID : 3|

|Name : Ricky|Balance : 5000|ID : 1|

|Name : Tom|Balance : 3000|ID : 6|

|Name : Mick|Balance : 6000|ID : 4|



Program Analysis


  • In this code above, we have overridden toString() method to give a meaningful String representation of each object of Customer class. This method is inherited by all the classes from the main "Object" class.
  • ArrayList<Customer>was declared to hold a collection of Customer objects.
  • We have declared and initialized 5 objects of Customer class and have stored them in an ArrayList(arr), by calling its add(Object o) method.
  • Finally, for-each loop iterates over the objects in ArrayList, and display eachs object(one-by-one).



Please share this article -





< Prev
Next >
< Iterator v/s ListIterator
Java Maps >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement