Module 2 Collection Framework
Module 2 Collection Framework
Syllabus
Thoughts on Collections.
Collections Overview
All the operations that you perform on a data such as searching, sorting,
Collections.
for the fundamental collections (dynamic arrays, linked lists, trees, and
hash tables) are highly efficient. You seldom, if ever, need to code one of
Third, extending and /or adapting a collection had to be easy. Toward this
interfaces.
TreeSet) of these interfaces are provided that you may use as-is.
o addition of generics,
o autoboxing/unboxing,and
which could result in run time mismatch. Hence Generics was introduced
through which you can explicitly state the type of object being stored.
for-each version of for loop can also be used for traversing each element
of a collection.
But this can only be used if we don't want to modify the contents of a
for-each loop can cycle through any collection of object that implements
Iterable interface.
Output :a b c d
Collection Interfaces
Interface Description
Collection Hierarchy
All these Interfaces give several methods which are defined by collections
Collection
Methods Description
boolean addAll( Collection C ) collection. Returns true if the element were added.
boolean remove( Object obj ) if the element was removed. Otherwise, returns
false.
boolean contains( Object obj ) collection or not. Returns true if obj is an element
array.
its own.
Methods Description
Object get( int index ) Returns object stored at the specified index
2. It doesn't define any method of its own. It has two sub interfaces,
in an ascending order.
2. There are couple of new and interesting methods added by this interface.
Methods Description
mentioned there.
standard Stacks.
Some of the classes provide full implementations that can be used as it is.
Class Description
Note:
1. To use any Collection class in your program, you need to import it in your
2. Whenever you print any Collection class, it gets printed inside the square
brackets [ ].
ArrayList class
Simple arrays have fixed size i.e it can store fixed number of elements.
But, sometimes you may not know beforehand about the number of
interface.
2. ArrayList supports dynamic array that can grow as needed. ArrayList has
three constructors.
3. ArrayLists are created with an initial size. When this size is exceeded,
order.
import java.util.*
class Test
{
public static void main(String[] args)
{
ArrayList< String> al = new ArrayList< String>();
al.add("ab");
al.add("bc");
al.add("cd");
system.out.println(al);
}
}
Output :
[ab,bc,cd]
al.add(1,"A2");
al1.retainAll(al2);
System.out.println("Contents of al1 using reatain all: " + al1);
al1.removeAll(al2);
System.out.println("Contents of al1: " + al1);
Output
the ArrayList.
Following are some reasons for why you can need to obtain an array from
your ArrayList:
int sum = 0;
}
}
Output
Sum is: 10
In the above mentioned example we are storing only string object in ArrayList
collection. But You can store any type of object, including object of class that
class Student
{
int rollno;
String name;
int age;
Student(int rollno,String name,int age)
{
this.rollno=rollno;
this.name=name;
this.age=age;
}
}
//creating arraylist
ArrayList<Student> al=new ArrayList<Student>();
al.add(s1); //adding Student class object
al.add(s2);
al.add(s3);
System.out.println(al);
LinkedList class
interfaces.
occurred.
ll.addLast("Z");
ll.addFirst("A");
ll.add(1, "A2");
ll.removeFirst();
ll.removeLast();
Output:
ArrayList and LinkedList are the Collection classes, and both of them
dynamic array that grows or shrinks in size as the elements are added or
DoublyLinked List.
Both the classes are used to store the elements in the list, but the major
structure.
On the other hand, the LinkedList does not allow random access as it does
not have indexes to access elements directly, it has to traverse the list
AbstractSequentialList.
faster in LinkedList.
HashSet class
3. It creates a collection that uses hash table for storage. Hash table
unique value, called its hash code. The hash code is then used as the index
import java.util.*;
public class HashSetDemo
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
HashSet< String> hs = new HashSet< String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
hs.add("F");
System.out.println(hs);
System.out.println(hs1);
}
Output:
[D, E, F, A, B, C]
[4, 5, 677, 10]
LinkedHashSet Class
import java.util.*;
public class LinkedHashSetDemo
{
public static void main(String[] args)
{
LinkedHashSet< String> hs = new LinkedHashSet< String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
LinkedHashSet< Integer> hs1 = new LinkedHashSet< Integer>();
hs1.add(10);
hs1.add(677);
hs1.add(5);
hs1.add(4);
hs1.add(4);
System.out.println(hs1);
}
}
Output :
[B, A, D, E, C, F]
[10, 677, 5, 4]
Questions
1. What is collection in java? Mention some of the recent changes to
Collections.
2. List and give brief description on Java Collection Interfaces and Classes.
add and remove elements with the specified Index, get and set methods.