Hierarchy of Collection Framework

6
7/25/2019 Hierarchy of Collection Framework http://slidepdf.com/reader/full/hierarchy-of-collection-framework 1/6 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, ist, !ueue, "e#ue etc.$ and classes (Arrayist, %ector, inkedist, &riority!ueue, 'ashSet, inked'ashSet, reeSet etc$. What is Collection in java Collection represents a single unit of objects i.e. a group. What is framework in java provides readymade architecture. represents set of classes and interface. is optional. What is Collectionframework Collection framework represents a unified architecture for storing and manipulating group of objects. )t has* +. )nterfaces and its implementations i.e. classes . Algorithm Do You Know ? -hat are the two ways to iterate the elements of a collection -hat is the difference between Arrayist and inkedist classes in collection framework -hat is the difference between Arrayist and %ector classes in collection framework

Transcript of Hierarchy of Collection Framework

Page 1: Hierarchy of Collection Framework

7/25/2019 Hierarchy of Collection Framework

http://slidepdf.com/reader/full/hierarchy-of-collection-framework 1/6

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, ist, !ueue, "e#ue etc.$ and classes (Arrayist, %ector, inkedist,

&riority!ueue, 'ashSet, inked'ashSet, reeSet etc$.

What is Collection in java

Collection represents a single unit of objects i.e. a group.

What is framework in java

• provides readymade architecture.

• represents set of classes and interface.

• is optional.

What is Collection framework

Collection framework represents a unified architecture for storing and manipulating group of 

objects. )t has*

+. )nterfaces and its implementations i.e. classes

. Algorithm

Do You Know ?

• -hat are the two ways to iterate the elements of a collection

• -hat is the difference between Arrayist and inkedist classes in collection

framework

• -hat is the difference between Arrayist and %ector classes in collection framework

Page 2: Hierarchy of Collection Framework

7/25/2019 Hierarchy of Collection Framework

http://slidepdf.com/reader/full/hierarchy-of-collection-framework 2/6

• -hat is the difference between 'ashSet and 'ash/ap classes in collection

framework

• -hat is the difference between 'ash/ap and 'ashtable class

• -hat is the difference between )terator and 0numeration interface in collection

framework

• 'ow can we sort the elements of an object. -hat is the difference between

Comparable and Comparator interfaces

• -hat does the hashcode($ method

li>What is the difference between java collection and java collections ?

Hierarchy of Collection Framework

et us see the hierarchy of collection framework.he java.util package contains all the

classes and interfaces for Collection framework.

Page 3: Hierarchy of Collection Framework

7/25/2019 Hierarchy of Collection Framework

http://slidepdf.com/reader/full/hierarchy-of-collection-framework 3/6

Methods of Collection interface

here are many methods declared in the Collection interface. hey are as follows*

No. Method Description

+ public boolean add(1bject element$ is used to insert an element in this collection.

public boolean addAll(Collection c$ is used to insert the specified collection elemen

collection.

2 public boolean remove(1bject

element$

is used to delete an element from this collection

Page 4: Hierarchy of Collection Framework

7/25/2019 Hierarchy of Collection Framework

http://slidepdf.com/reader/full/hierarchy-of-collection-framework 4/6

3 public boolean removeAll(Collection

c$

is used to delete all the elements of specified co

collection.

4 public boolean retainAll(Collection c$ is used to delete all the elements of invoking co

specified collection.

6 public int si7e($ return the total number of elements in the colle

8 public void clear($ removes the total no of element from the collec

9 public boolean contains(1bject

element$

is used to search an element.

: public boolean containsAll(Collection

c$

is used to search the specified collection in this

+; public )terator iterator($ returns an iterator.

++ public 1bject<= toArray($ converts collection into array.

+ public boolean is0mpty($ checks if collection is empty.

+2 public boolean e#uals(1bject

element$

matches two collection.

+3 public int hashCode($ returns the hashcode number for collection.

Iterator interface

)terator interface provides the facility of iterating the elements in forward direction only.

Methods of Iterator interface

here are only three methods in the )terator interface. hey are*

+. public boolean hasNext() it returns true if iterator has more elements.

. public object next() it returns the element and moves the cursor pointer to thene5t element.

2. public void remove() it removes the last elements returned by the iterator. )t israrely used.

Page 5: Hierarchy of Collection Framework

7/25/2019 Hierarchy of Collection Framework

http://slidepdf.com/reader/full/hierarchy-of-collection-framework 5/6

>ollowing e5ample how to print a collection by using

t/ap.keySet($,t/ap.values($ and t/ap.first?ey($ methods of Java @til class .

package com.gitam.prasad;

import java.util.TreeMap;

public class TreeExample{

  public static void main(String[] args) {  System.out .println(Tree Map Example!"n);  TreeMap tMap # new TreeMap();  tMap.put($% Sunday);  tMap.put(&% Monday);  tMap.put('% Tuesday);  tMap.put(% ednesday);  tMap.put(*% T+ursday);  tMap.put(,% -riday);  tMap.put(% Saturday);  System.out .println(/eys o0 tree map1  

2 tMap.3eySet());  System.out .println(4alues o0 tree map1  

2 tMap.values());  System.out .println(/ey1 * value1  2 tMap.get(*)2 "n);  System.out .println(-irst 3ey1  2 tMap.5rst/ey()

2 4alue1  2 tMap.get(tMap.5rst/ey()) 2 "n);

  System.out .println(6ast 3ey1  2 tMap.last/ey()2 4alue1 2 tMap.get(tMap.last/ey()) 2 "n);

  System.out .println(7emoving 5rst data1  2 tMap.remove(tMap.5rst/ey()));

  System.out .println(8o9 t+e tree map /eys1  2 tMap.3eySet());

  System.out .println(8o9 t+e tree map contain1  2 tMap.values() 2 "n);

  System.out .println(7emoving last data1  2 tMap.remove(tMap.last/ey()));

  System.out .println(8o9 t+e tree map /eys1  2 tMap.3eySet());

  System.out .println(8o9 t+e tree map contain1  2 tMap.values());

  ::

Page 6: Hierarchy of Collection Framework

7/25/2019 Hierarchy of Collection Framework

http://slidepdf.com/reader/full/hierarchy-of-collection-framework 6/6