You are on page 1of 16

COLLECTIONS

(An easy way to manage


Objects)
Introduction To Custom ArrayList
• What is a custom ArrayList ?

• A custom ArrayList is an ArrayList which can hold


objects of programmer defined classes .

• For example , suppose we have a class called Emp and


we want to store Emp objects in the ArrayList .

• Then such an ArrayList will be called custom ArrayList.


Creating A Custom ArrayList
• How do we create a custom ArrayList ?

• To create a custom ArrayList , we use the following


syntax:
• List <name of our class> refName= new ArrayList< >( );

• For ex:
• List<Emp> empList=new ArrayList<>();
Creating A Custom ArrayList
• How do we add objects in a custom ArrayList ?

• To add objects of our class in a custom ArrayList , we


use the same syntax as before , i.e. by calling the method
add()
For ex:
List<Emp> empList=new ArrayList<>();
Emp e=new Emp(21, “Ravi”,50000.0);
Emp f=new Emp(25, “Sumit”, 40000.0);
empList.add(e);
empList.add(f);
Exercise 4
Create a class called Emp with following instance members:
1. age
2. name
3. sal

Provide appropriate constructor to initialize Emp object. Then


create a driver class called UseEmp which maintains a list of
employees . Now do the following operations on this list:
4. Add 4 Emp objects
5. Display Emp records
6. Remove an Emp object from the list
7. Sort the List
Point To Remember
• If we are adding objects of our own class in ArrayList ,
then we must always override the equals( ) method
inherited from the super class Object.

• This is because whenever we will call the method


remove( ) on the ArrayList object , it internally calls the
equals( ) method of our class .

• This also happens when we call the methods indexOf( )


or contains( )
Point To Remember
• Now if we do not override this method in our class then
the equals() method of Object class will get called and as
we know the equals () method of Object class compares
memory addresses of 2 objects .

• So even if objects are having same data member values


then also equals() method of Object class will return
false

• Thus the methods remove( ) , indexOf( ) and contains( )


will fail to find our object in the list.
How To Sort The ArrayList ?
• The Java language provides us predefined sorting
functions/methods to sort the elements of collections like
ArrayList.

• Thus we do not have to write our own logic for sorting


these collections.
How To Sort The ArrayList ?
• This is done using a class called “Collections” in the
package java.util which contains a static method called
sort( ) which can sort an ArrayList

• The prototype of the method is:


• public static void sort(List L)

• This method accepts a List as argument and sorts it’s


elements in natural order .
What Is Natural Order?
• Natural order means the default sorting order which is
as follows:

• If the List consists of String elements, it will be sorted


into alphabetical order.

• If it contains Integers , it will be sorted in numeric order.

• If it consists of Date elements, it will be sorted into


chronological order.
Example
List<String> months=new ArrayList<>();
List<Integer> days=new ArrayList<>();
months.add("January");
months.add("February");
months.add("March");
months.add("April");
days.add(31);
days.add(28);
days.add(31);
days.add(30);
Example
System.out.println("Before sorting:");
System.out.println(months);
Collections.sort(months);
System.out.println("After sorting:");
System.out.println(months);
System.out.println("Before sorting:");
System.out.println(days);
Collections.sort(days);
System.out.println("After sorting:");
System.out.println(days);
Output
Before sorting:
[January, February, March, April]
After sorting:
[April, February, January, March]

Before sorting:
[31, 28, 31, 30]
After sorting:
[28, 30, 31, 31]
How To Sort Custom ArrayList ?
• But when we call the sort( ) method of Collections class
and pass it our Emp list then it will generate an error.

• Can you guess why ?

• This is because we have not defined any sorting order for


our Emp objects !!!
Solution
• To solve this problem we will have to supply the
information to Collections class about how to sort the
Emp list.

• This is done by implementing an interface called


Comparable and overriding it’s method called
compareTo( ) which has the following prototype:

• public int compareTo(Object)


Summary Of Benefits
1. Maintains the insertion order of elements

2. Can grow dynamically

3. Elements can be added or removed from a particular


location

4. Provides methods to manipulate stored objects

You might also like