Collections Api - Java

21
-BY DIMPY CHUGH (1833) DRISHTI BHALLA(1838) COLLECTIONS API

Transcript of Collections Api - Java

- B Y

D I M P Y C H U G H ( 1 8 3 3 )

D R I S H T I B H A L L A ( 1 8 3 8 )

COLLECTIONS API

COLLECTION API

Collection in java is a framework that provides an architecture to store and manipulate the group of objects.

Java Collection Framework is a collection of many interfaces( Set, List, Queue,Map) and classes (ArrayList, Vector,LinkedList,PriorityQueue,HashSet,TreeSet,TreeMap etc).

All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections.

METHODS OF COLLECTION INTERFACE

No. Method Description

1 public boolean add(Object element) is used to insert an element in this

collection.

2 Object get(int index) Returns the element at the specified position in the collection..

3 public boolean addAll(Collection c) is used to insert the specified collection

elements in the invoking collection.

4 public boolean remove(Object element) is used to delete an element from this

collection.

5 public int size() return the total number of elements in the

collection.

6 public void clear() removes the total no of element from the

collection.

7 public boolean contains(Object element) is used to search an element.

8 public boolean containsAll(Collection c) is used to search the specified collection in

this collection.

9 public Iterator iterator() returns an iterator.

10 public boolean isEmpty() checks if collection is empty.

11 public boolean equals(Object element) matches two collection.

INTERFACES

LIST- List of thingsAllows duplicates

QUEUE- Things in FIFO manner

Allows duplicates & null values

SET- Unique Things

Allows null values

MAP – Things with unique key

Allows duplicate values

TRAVERSING TECHNIQUES

1) Using Iterator Interface

Iterator Interface is used to traverse the elements in forward direction only. It has 2 main functions:

public boolean hasNext()- It returns true if iterator has more elements.

public object next()- It returns the element and moves the cursor pointer to the next element.

For Example:

Iterator it=list.iterator();

while(it.hasNext())

{

System.out.println( " Element :" + it.next());

}

2) Using Enumeration Interface Enumeration provides two methods to enumerate through the elements. public boolean hasMoreElements()- returns true if there

are more elements to enumerate through otherwise it returns false.

public object nextElement()- returns the next element in enumeration.

For Example: Enumeration e=v1.elements(); while(e.hasMoreElements()) { System.out.println(e.nextElement()); }

3) Using ListIterator Inteface

ListIterator Interface is used to traverse the element in backward and forward direction. It has 4 main functions: public boolean hasNext()- Returns true if this list iterator has

more elements when traversing the list in the forward direction public Object next()- Returns the next element in the list and

advances the cursor position. public boolean hasPrevious()- Returns true if this list iterator

has more elements when traversing the list in the reverse direction. public Object previous()- Returns the previous element in the

list and moves the cursor position backwards. Example for Traversing backwards ListIterator itr=al.listIterator(); while(itr.hasPrevious()) { System.out.println(itr.previous()); }

ARRAYLIST

ArrayList is an inbuilt class that implements the List interface.

It uses dynamic array for storing the elements.

It maintains insertion order.

It takes NULL, TRUE, FALSE as elements of the list.

In ArrayList, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.

Elements of ArrayList can be traversed using Iterator .

Creating a new ArrayList

ArrayList al=new ArrayList();

VECTORS

Vector implements the List Interface.

It is similar to ArrayList as it also contains growable array data structure, but with two differences:

I. Vector is synchronized- This means if one thread is working on a Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on vector at a time.

II. Vector give poor performance- Operations on elements gives poor performance as they are thread-safe, the thread which works on Vector gets a lock on it which makes other thread wait till the lock is released.

It maintains the insertion order.

Allows duplicate elements in the list.

Vector can be traversed by Enumerator or Iterator.

Creating a new Vector

Vector v1=new Vector();

Additional Functions of Vector class

1) int capacity()- Returns the current capacity of this vector.

2) int indexOf(Object elem)- Searches for the first occurrence of the given argument, testing for equality using the equals method and -1 if the vector does not contain this element.

LINKED LISTS

LinkedList implements the List interface.

Java LinkedList class uses doubly linked list to store the elements.

It can contain duplicate elements.

Maintains insertion order.

It is non synchronized.

Java LinkedList class can be used as list, stack or queue.

Linkedlist have same functions as that of collection interface.

Creating a new LinkedList

LinkedList list=new LinkedList();

PRIORITY QUEUE

PriorityQueue implements the Queue interface. Elements are added in any random order. Allows duplicate elements in the queue. It does not permit null elements. Head of the Queue is the least item in the order. A PriorityQueue is traversed using Iterator. Creating a new PriorityQueue PriorityQueue p1=new PriorityQueue(); Additional Functions of PriorityQueue 1) Object peek()- This method retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. 2) Object poll()- This method retrieves and removes the head of this queue, or returns null if this queue is empty.

HASHSET

HashSet implements the Set interface. It uses hashtable to store the elements.

No insertion order is maintained.

It permits null element.

HashSet is traversed by Iterator.

It contains unique elements only. But in case new objects are formed i.e. memory locations are different , the hashset cannot avoid duplicacy.

Hashset have same functions as that of collection interface.

Creating a new HashSet

HashSet set=new HashSet();

LINKED-HASHSET

LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted.

It contains unique elements only like HashSet. It extends HashSet class and implements Set interface.

It is traversed by Iterator.

The functions of LinkedHashset are same as Hashset.

Creating a new LinkedHashSet LinkedHashSet set=new LinkedHashSet();

TREESET

It contains unique elements only like HashSet and implements Set interface .

Elements are stored in sorted, ascending order.

Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly.

Creating a new TreeSet

TreeSet set=new TreeSet();

Additional Functions of TreeSet

1) Object first()-Returns the first (lowest) element currently in this sorted set.

2) Object last()-Returns the last (highest) element currently in this sorted set.

3) SortedSet subSet(Object fromElement, Object toElement)-Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

HASHMAP

HashMap maintains key and value pairs and are denoted as HashMap<Key, Value> .

It implements the Map interface.

It can not take duplicate keys but can have duplicate values.

It allows null key and null values.

It maintains no order.

It is mandatory to specify both key and value. Specifying key and keeping value empty or vice-versa gives compile time error.

HashMap is traversed by Iterator.

It is non-synchronised.

Creating a new HashMap

HashMap map=new HashMap();

Additional Functions of HashMap

1. Set entrySet()- Returns a collection view of the mappings contained in this map.

2. Object put(Object key, Object value)- Associates the specified value with the specified key in this map.

3. Object remove(Object key)- Removes the mapping for this key from this map if present.

4. boolean containsKey(Object key)- Returns true if this map contains a mapping for the specified key.

LINKEDHASHMAP

LinkedHashMap maintains a linked list of the entries in the map, in the order in which they were inserted.

It extends HashMap class and implements the Map interface .

It is same as HashMap instead maintains insertion order of keys.

It is traversed by Iterator. LinkedHashMap has same functions as that of

HashMap. Creating a new LinkedHashMap LinkedHashMap map=new LinkedHashMap();

TREEMAP

TreeMap class implements the Map interface by using a tree.

It can not take duplicate keys but can have duplicate values.

It cannot have null key but can have multiple null values.

A TreeMap provides an efficient means of storing key/value pairs in sorted order, and allows rapid retrieval.

TreeMap has same functions as that of HashMap.

Creating a new TreeMap

TreeMap map=new TreeMap();

HASHTABLE

HashTable implements the Map interface .

Like HashMap, Hashtable stores key-value pairs. When using a Hashtable, we specify an object that is used as a key, and the value that we want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

It contains only unique elements.

Any non-null object can be used as a key or as a value.

Unlike HashMap, It is synchronized.It is thread-safe and can be shared with many threads.

Creating a new HashTable

HashTable map=new HashTable();

THANKYOU!!