COLLECTION:
Collections in java is a framework that provides an architecture to store and manipulate the group of objects.
All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections.Java Collection simply means a single unit of objects.
Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).
Methods of Collection interface:
There are many methods declared in the Collection interface. They are as follows:
No.
|
Method
|
Description
|
1
|
public boolean add(Object element)
|
is used to insert an element in this collection.
|
2
|
public boolean addAll(Collection c)
|
is used to insert the specified collection elements in the invoking collection.
|
3
|
public boolean remove(Object element)
|
is used to delete an element from this collection.
|
4
|
public boolean removeAll(Collection c)
|
is used to delete all the elements of specified collection from the invoking collection.
|
5
|
public boolean retainAll(Collection c)
|
is used to delete all the elements of invoking collection except the specified collection.
|
6
|
public int size()
|
return the total number of elements in the collection.
|
7
|
public void clear()
|
removes the total no of element from the collection.
|
8
|
public boolean contains(Object element)
|
is used to search an element.
|
9
|
public boolean containsAll(Collection c)
|
is used to search the specified collection in this collection.
|
10
|
public Iterator iterator()
|
returns an iterator.
|
11
|
public Object[] toArray()
|
converts collection into array.
|
12
|
public boolean isEmpty()
|
checks if collection is empty.
|
13
|
public boolean equals(Object element)
|
matches two collection.
|
14
|
public int hashCode()
|
returns the hashcode number for collection.
|
LIST INTERFACE:
ARRAY LIST:
o Java ArrayList class can contain duplicate elements.
o Java ArrayList class maintains insertion order.
o Java ArrayList class is non synchronized.
o Java ArrayList allows random access because array works at the index basis.
o In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.
i. Java ArrayList class extends AbstractList class which implements List interface. The List interface extends Collection and Iterable interfaces in hierarchical order.
ArrayList class declaration
Declaration --> java.util.ArrayList class.
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
Constructors of Java ArrayList:
Constructor
|
Description
|
ArrayList()
|
It is used to build an empty array list.
|
ArrayList(Collection c)
|
It is used to build an array list that is initialized with the elements of the collection c.
|
ArrayList(int capacity)
|
It is used to build an array list that has the specified initial capacity.
|
Methods of Java ArrayList:
Method
|
Description
|
void add(int index, Object element)
|
It is used to insert the specified element at the specified position index in a list.
|
boolean addAll(Collection c)
|
It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
|
void clear()
|
It is used to remove all of the elements from this list.
|
int lastIndexOf(Object o)
|
It is used to return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
|
Object[] toArray()
|
It is used to return an array containing all of the elements in this list in the correct order.
|
Object[] toArray(Object[] a)
|
It is used to return an array containing all of the elements in this list in the correct order.
|
boolean add(Object o)
|
It is used to append the specified element to the end of a list.
|
boolean addAll(int index, Collection c)
|
It is used to insert all of the elements in the specified collection into this list, starting at the specified position.
|
Object clone()
|
It is used to return a shallow copy of an ArrayList.
|
int indexOf(Object o)
|
It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
|
void trimToSize()
|
It is used to trim the capacity of this ArrayList instance to be the list's current size.
|
Java Generic Collection:
Syntax:
ArrayList<String> al=new ArrayList<String>();
Example:
An ArrayList example where we are adding books to list and printing all the books.
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class ArrayListExample {
public static void main(String[] args) {
//Creating list of Books
List<Book> list=new ArrayList<Book>();
//Creating Books
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
//Adding Books to list
list.add(b1);
list.add(b2);
list.add(b3);
//Traversing list
for(Book b:list){
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
}
}
}
JAVA LINKED LIST CLASS
o Java LinkedList class can contain duplicate elements.
o Java LinkedList class maintains insertion order.
o Java LinkedList class is non synchronized.
o In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.
o Java LinkedList class can be used as list, stack or queue.
class declaration:
declaration --> java.util.LinkedList class.
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E >, Deque<E>, Cloneable, Serializable
Constructors of Java LinkedList:
Constructor
|
Description
|
LinkedList()
|
It is used to construct an empty list.
|
LinkedList(Collection c)
|
It is used to construct a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
|
Methods of Java LinkedList:
Method
|
Description
|
void add(int index, Object element)
|
It is used to insert the specified element at the specified position index in a list.
|
void addFirst(Object o)
|
It is used to insert the given element at the beginning of a list.
|
void addLast(Object o)
|
It is used to append the given element to the end of a list.
|
int size()
|
It is used to return the number of elements in a list
|
boolean add(Object o)
|
It is used to append the specified element to the end of a list.
|
boolean contains(Object o)
|
It is used to return true if the list contains a specified element.
|
boolean remove(Object o)
|
It is used to remove the first occurence of the specified element in a list.
|
Object getFirst()
|
It is used to return the first element in a list.
|
Object getLast()
|
It is used to return the last element in a list.
|
int indexOf(Object o)
|
It is used to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element.
|
int lastIndexOf(Object o)
|
It is used to return the index in a list of the last occurrence of the specified element, or -1 if the list does not contain any element.
|
java LinkedList Example: Book
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class LinkedListExample {
public static void main(String[] args) {
//Creating list of Books
List<Book> list=new LinkedList<Book>();
//Creating Books
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
//Adding Books to list
list.add(b1);
list.add(b2);
list.add(b3);
//Traversing list
for(Book b:list){
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
}
}
}
JAVA LIST INTERFACE
Method
|
Description
|
void add(int index,Object element)
|
It is used to insert element into the invoking list at the index passed in the index.
|
boolean addAll(int index,Collection c)
|
It is used to insert all elements of c into the invoking list at the index passed in the index.
|
object get(int index)
|
It is used to return the object stored at the specified index within the invoking collection.
|
object set(int index,Object element)
|
It is used to assign element to the location specified by index within the invoking list.
|
object remove(int index)
|
It is used to remove the element at position index from the invoking list and return the deleted element.
|
ListIterator listIterator()
|
It is used to return an iterator to the start of the invoking list.
|
ListIterator listIterator(int index)
|
It is used to return an iterator to the invoking list that begins at the specified index.
|
Example:
import java.util.*;
public class ListExample{
public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();
al.add("Amit");
al.add("Vijay");
al.add("Kumar");
al.add(1,"Sachin");
System.out.println("Element at 2nd position: "+al.get(2));
for(String s:al){
System.out.println(s);
}
}
} Output:
Element at 2nd position: Vijay
Amit
Sachin
Vijay
Kumar
Java ListIterator Interface
Methods of Java ListIterator Interface:
Method
|
Description
|
boolean hasNext()
|
This method return true if the list iterator has more elements when traversing the list in the forward direction.
|
Object next()
|
This method return the next element in the list and advances the cursor position.
|
boolean hasPrevious()
|
This method return true if this list iterator has more elements when traversing the list in the reverse direction.
|
Object previous()
|
This method return the previous element in the list and moves the cursor position backwards.
|
Example:
import java.util.*;
public class TestCollection8{
public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();
al.add("Amit");
al.add("Vijay");
al.add("Kumar");
al.add(1,"Sachin");
System.out.println("element at 2nd position: "+al.get(2));
ListIterator<String> itr=al.listIterator();
System.out.println("traversing elements in forward direction...");
while(itr.hasNext()){
System.out.println(itr.next());
}
System.out.println("traversing elements in backward direction...");
while(itr.hasPrevious()){
System.out.println(itr.previous());
}
` }
}
Output:
element at 2nd position: Vijay
traversing elements in forward direction...
Amit
Sachin
Vijay
Kumar
traversing elements in backward direction...
Kumar
Vijay
Sachin
Amit
HASH SET
Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface.
The important points about Java HashSet class are:
o HashSet stores the elements by using a mechanism called hashing( generating a unique that represents a value).
o HashSet contains unique elements only.
The HashSet class extends AbstractSet class which implements Set interface. The Set interface inherits Collection and Iterable interfaces in hierarchical order
Constructors of Java HashSet class:
Constructor
|
Description
|
HashSet()
|
It is used to construct a default HashSet.
|
HashSet(Collection c)
|
It is used to initialize the hash set by using the elements of the collection c.
|
HashSet(int capacity)
|
It is used to initialize the capacity of the hash set to the given integer value capacity. The capacity grows automatically as elements are added to the HashSet.
|
Methods of Java HashSet class:
Method
|
Description
|
void clear()
|
It is used to remove all of the elements from this set.
|
boolean contains(Object o)
|
It is used to return true if this set contains the specified element.
|
boolean add(Object o)
|
It is used to adds the specified element to this set if it is not already present.
|
boolean isEmpty()
|
It is used to return true if this set contains no elements.
|
boolean remove(Object o)
|
It is used to remove the specified element from this set if it is present.
|
Object clone()
|
It is used to return a shallow copy of this HashSet instance: the elements themselves are not cloned.
|
Iterator iterator()
|
It is used to return an iterator over the elements in this set.
|
int size()
|
It is used to return the number of elements in this set.
|
Example:
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class HashSetExample {
public static void main(String[] args) {
HashSet<Book> set=new HashSet<Book>();
//Creating Books
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
//Adding Books to HashSet
set.add(b1);
set.add(b2);
set.add(b3);
//Traversing HashSet
for(Book b:set){
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
}
}
}
TREE SET
o Contains unique elements only like HashSet.
o Access and retrieval times are quiet fast.
o Maintains ascending order.
Constructors of Java TreeSet class
Constructor
|
Description
|
TreeSet()
|
It is used to construct an empty tree set that will be sorted in an ascending order according to the natural order of the tree set.
|
TreeSet(Collection c)
|
It is used to build a new tree set that contains the elements of the collection c.
|
TreeSet(Comparator comp)
|
It is used to construct an empty tree set that will be sorted according to given comparator.
|
TreeSet(SortedSet ss)
|
It is used to build a TreeSet that contains the elements of the given SortedSet.
|
Methods of Java TreeSet class
Method
|
Description
|
boolean addAll(Collection c)
|
It is used to add all of the elements in the specified collection to this set.
|
boolean contains(Object o)
|
It is used to return true if this set contains the specified element.
|
boolean isEmpty()
|
It is used to return true if this set contains no elements.
|
boolean remove(Object o)
|
It is used to remove the specified element from this set if it is present.
|
void add(Object o)
|
It is used to add the specified element to this set if it is not already present.
|
void clear()
|
It is used to remove all of the elements from this set.
|
Object clone()
|
It is used to return a shallow copy of this TreeSet instance.
|
Object first()
|
It is used to return the first (lowest) element currently in this sorted set.
|
Object last()
|
It is used to return the last (highest) element currently in this sorted set.
|
int size()
|
It is used to return the number of elements in this set.
|
Example:
import java.util.*;
class Book implements Comparable<Book>{
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
public int compareTo(Book b) {
if(id>b.id){
return 1;
}else if(id<b.id){
return -1;
}else{
return 0;
}
}
}
public class TreeSetExample {
public static void main(String[] args) {
Set<Book> set=new TreeSet<Book>();
//Creating Books
Book b1=new Book(121,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(233,"Operating System","Galvin","Wiley",6);
Book b3=new Book(101,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
//Adding Books to TreeSet
set.add(b1);
set.add(b2);
set.add(b3);
//Traversing TreeSet
for(Book b:set){
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
}
}
}