You are on page 1of 13

Collection

Framework
• 3.14.1 CollectionFramework.
• 3.14.2 Interfaces: Collection, List, Set
• 3.14.3 Navigation: Enumeration, Iterator,
ListIterator
• 3.14.4 Classes: LinkedList, ArrayList, Vector,
HashSet

Java Collection Framework

1. Collections are the containers that groups multiple


items in a single unit.[Multivalue container]
2. It provides an architecture to store and manipulate a
group of objects.
3. Using java collections various operations can be
performed on the data like searching, sorting, insertion,
manipulation, deletion etc.
4. Java collection framework provides many interfaces
and classes in order to store.
Hierarchy of Collection Framework
The java.util package contains all
the classes and interfaces for the Collection framework.
Interface
Interfaces are the reference types which are similar to
classes but contains only abstract methods. Methods must
be implemented to the given class.
Interfaces in Java collection framework
• Iterator:
The iterator interface provides the facility of iterating
the elements only in forward direction.
Methods comes under Iterator are
• public boolean hasNext()
• public object next()
• public void remove()
Iterable:
The Iterable interface is the root interface for all the
collection classes.The collection interface along with all
it’s subclasses also implement the Iteratable Interface.
Methods: Iterator<T>iterator()

Collection:
Collection Interface is implemented by all the classes
in the collection framework and declares the methods that
every collection will contain
Methods:
boolean add(object obj)
boolean addAll(object obj)
void clear()
List
Java List is an interface that extends the collection and
contains ordered collection of elements including
duplicate values. It stores elements in an index approach.
List

LinkedLis
ArrayList Vector
t

ArrayList:
ArrayList is the implementation of List interface where
the elements can be dynamically added or removed from the
list.
The size of the List is increased dynamically if the
elements are added more than the initial size.
Syntax to create ArrayList
ArrayList<Type>arraylistNm=new ArrayList<>();
e.g.
ArrayList<String>a1=new ArrayList<>();
Note:
We can not create array lists of primitive data types like
int,float, char etc. Instead, we have to use their corresponding
wrapper class.
Important points of ArrayList

• Implements List interface


• Based on Array Data Structure
• ArrayList is resizable array (Dynamic Arrays)
• Arrays are of fixed size whereas ArrayList size
grows/shrinks on adding/removing elements.
• Random access is possible
• To add elements in ArrayList add() method is used
• E.g.
a1.add(“A”);// add elements at the end of the list
a1.add(3,“A”);// add elements at 4th position
We can simple display the complete ArrayList elements using
System.out.println() method (Not good method)
e.g. System.out.println(a1);
How to change element
To change element value in ArrayList set() method is used.
e.g. a1.set(3,”c”);//change element value of 4th location with
‘c’
To Remove elements from ArrayList remove() method is used
e.g. a1.remove(“B”);// remove element’B’ from List
a1.remove(2); // remove 3rd element from List
To remove all elements, removeAll() method is used (clear()
• To iterate all elements of ArrayList for loop is a
preferred.(iterator() method can also be used.
• E.g for (String str: a1); System.out.println(str);
• To find number of elements in ArrayList, size() method is
used
• To copy all elements of one ArrayList into other use
addAll() method
e.g. A2.addAll(a1); // copy all elements of ArrayList ‘a1’
into ‘a2’.
• To access a ArrayList get() method is used
e.g. String str=a1.get(0); //return element value at location
ArrayList Sorting
• To sort ArrayList elements, sort method of Collection
class is used.
• By Default ascending sorting is done
e.g.
Collections.sort(a1); // sort a1 in ascending order
For Descending order sort Collections.reverseOrder() is used
along with Collection.sort() method.
e.g. Collections.sort(a1,Collections.reverseOrder());
• How to create sublist from ArrayList
subList() method of ArrayList class is used to create a
subList from a given list
e.g. a1.subList(int fromindex,int toindex);// from index is
inclusive and toindex is exclusive.
a1.subList(1,3);
• How to swap two elements of ArrayList
To swap value Collections.swap() method is used
e.g. Collections.swap(a1,1,4); // swapping 2nd element with
5th elements
• IndexOf() and lastIndexOf()
indexOf() method returns the index of particular element
in list.
In case of duplicate values, indexOf() method always
returns first occurrence.
How Clone method works in ArrayList
• clone() method returns a shallow copy of the ArrayList.
• After clonning is done, changes made to original ArrayList
doesn’t reflected in the cloned list
e.g. ArrayList<String> a2=(ArrayList<String>)a1.clone();

You might also like