Collections
Collections
Arrays can hold primitives as well as Collections don’t work with primitives ,
objects they only can hold objects
Good performance but poor memory Poor performance but good memory
utilization utilization
java.util.ArrayList
java.util.LinkedList
java.util.Vector
java.util.Stack
The “ArrayList” class
1. ArrayList implements the List interface
• Type UnSafe
AND
• Type Safe
Type UnSafe ArrayList
• Type UnSafe ArrayList can be created as shown below
• For ex:
obj.add(“Amit”);
obj.add(25);
obj.add(true);
• The < > is called diamond operator in Java and was introduced from Java
7 onwards .
• It tells the compiler to only allow programmer to add String values in the
ArrayList.
• Any other type of value cannot be added in the ArrayList and if we try to
do so , the compiler will generate syntax error
• For ex:
obj.add(“Amit”); // Correct
obj.add(25); // Wrong
obj.add(true); // Wrong
Inserting Elements In ArrayList
• To insert an element in the ArrayList , we have to call the
method add( )
• Prototype:
• For Ex:
ArrayList <String> cities = new ArrayList<>();
cities.add(“Bhopal”);
cities.add(0, “Indore”);
Retrieving Elements Of ArrayList
To retrieve an element from the ArrayList , we have to
call the method get( )
Prototype:
public Object get(int index)
String s=cities.get(0);
String p=cities.get(1);
System.out.println(s); // will show Indore
System.out.println(p); // will show Bhopal
Checking size of ArrayList
• Size of an ArrayList means total number of elements
currently present in it.
For Ex:
int n = cities.size();
import java.util.*;
list.add("Apple");
list.add("Banana");
list.add("Grapes");
System.out.println(list);
} }
Exercise 1
• WAP to store names of first four months in the ArrayList
and print them back .
Retrieving Item From ArrayList Using
Enhanced for
We can traverse an ArrayList also using enhanced
for loop
For Ex:
boolean found=cities.contains(“Bhopal”);
Another Way Of Searching An Element
In ArrayList
We also can use indexOf() method of ArrayList in Java to
find out index of a particular object.
1. Now print the modified list and if the fruit name is not
found then print the message Fruit not found
COLLECTIONS
(An easy way to manage
Objects)
Introduction To Custom ArrayList
• What is a custom ArrayList ?
• For ex:
• ArrayList<Emp> empList=new ArrayList<>();
Creating A Custom ArrayList
• How do we add objects in a custom ArrayList ?
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.
• No initial capacity
cities.add(2,”New York”);
•Example
System.out.println(cities.get(1));// Paris
Getting Element From The LinkedList
•Although LinkedList provides us a get( ) method , but
when we use it to access a particular element then it
internally traverses the complete list upto the element
required .
• Prototype:
}
}
Output:
[Amit, Sumit]
Now Again Guess The Output ?
class Student class HashSetDemo{
{ public static void main(String[] args) {
private String name; HashSet <Student> hs;
public Student(String name) hs=new HashSet<Student>();
Student s1=new Student("Amit");
{
Student s2=new Student("Sumit");
this.name=name; Student s3=new Student("Amit");
} hs.add(s1);
hs.add(s2);
public String toString() hs.add(s3);
{ System.out.println(hs);
return name; }
} }
Output:
[Amit , Sumit , Amit]
}
Why Duplicates Were Not Removed
?
• This is because when we add a new object to HashSet , then
java searches it in the hash table using it’s hash code .
•Now , if we write :
TreeSet<Book> ts=new TreeSet<Book>( );
Book b1=new Book("Let Us C","Kanetkar",350);
Book b2=new Book("Java Certification","Kathy",650);
Book b3=new Book("Mastering C++","Venugopal",500);
ts.add(b1);
ts.add(b2);
ts.add(b3);
•Then java will throw a ClassCastException.
Why Did This Happen ?
• This is because for any object which we add to the
TreeSet created using default constructor , then 2
conditions must be compulsorily satisfied:
1. The objects added must be homogeneous
2. The objects must be comparable i.e. the class must implement
the java.lang.Comparable interface.