Collection Framework in java

30
COLLECTION FRAMEWORK www.blog.cpd-india.com Call Us: 011-65164822 CPD TECHNOLOGIES Add:- Block C 9/8, Sector -7, Rohini , Delhi-110085, India www.cpd-india.com

description

Collections Framework is a unified architecture for managing collections, Main Parts of Collections Framework 1. Interfaces :- Core interfaces defining common functionality exhibited by collections 2. Implementations :- Concrete classes of the core interfaces providing data structures 3. Operations :- Methods that perform various operations on collections

Transcript of Collection Framework in java

Page 1: Collection Framework in java

COLLECTION FRAMEWORK

www.blog.cpd-india.com

Call Us: 011-65164822

CPD TECHNOLOGIES Add:- Block C 9/8, Sector -7, Rohini , Delhi-110085, India www.cpd-india.com

Page 2: Collection Framework in java

COLLECTION FRAMEWORK

The collections framework is a unified architecture for representing and manipulating collections, enabling them to be manipulated independently of the details of their representation. It reduces programming effort while increasing performance. It enables interoperability among unrelated APIs, reduces effort in designing and learning new APIs, and fosters software reuse. The framework is based on more than a dozen collection interfaces. It includes implementations of these interfaces and algorithms to manipulate them.

Page 3: Collection Framework in java

CONTENTS

What is Collection? Collections Framework Collections Hierarchy Collections Implementations

SetListMap

Page 4: Collection Framework in java

OBJECTIVES

Define a collection Describe the collections framework Describe the collections hierarchy Demonstrate each collection implementation

Page 5: Collection Framework in java

WHAT IS A COLLECTION?

A Collection (also known as container) is an object that contains a group of objects treated as a single unit.

Any type of objects can be stored, retrieved and manipulated as elements of collections.

Page 6: Collection Framework in java

COLLECTIONS FRAMEWORK

Collections Framework is a unified architecture for managing collections

Main Parts of Collections Framework1. Interfaces

Core interfaces defining common functionality exhibited by collections

1. Implementations Concrete classes of the core interfaces providing data structures

1. Operations Methods that perform various operations on collections

Page 7: Collection Framework in java

COLLECTIONS FRAMEWORKINTERFACES

Core Interface Description

Collection specifies contract that all collections should implement

Set defines functionality for a set of unique elements

SortedSet defines functionality for a set where elements are sorted

List defines functionality for an ordered list of non- unique elements

Map defines functionality for mapping of unique keys to values

SortedMap defines functionality for a map where its keys are sorted

Page 8: Collection Framework in java

COLLECTIONS FRAMEWORK IMPLEMENTATIONS

Set List Map

HashSet ArrayList HashMap

LinkedHashSet LinkedList LinkedHashMap

TreeSet Vector Hashtable

Tree Map

Note: Hashtable uses a lower-case “t”

Page 9: Collection Framework in java

OPERATIONS

Basic collection operations:

Check if collection is empty Check if an object exists in collection. Retrieve an object from collection Add object to collection Remove object from collection Iterate collection and inspect each object

Each operation has a corresponding method implementation for each collection type

Page 10: Collection Framework in java

COLLECTIONS CHARACTERISTICS

OrderedElements are stored and accessed in a specific

order Sorted

Elements are stored and accessed in a sorted order

IndexedElements can be accessed using an index

UniqueCollection does not allow duplicates

Page 11: Collection Framework in java

ITERATOR

An iterator is an object used to mark a position in a collection of data and to move from item to item within the collection

Syntax:Iterator <variable> = <CollectionObject>.iterator();

Page 12: Collection Framework in java

COLLECTIONS HIERARCHYSET AND LIST

HashSet

Collection

SortedSet

ListSet

LinkedHashSet TreeSet LinkedList Vector ArrayList

implements

implements

implements

implements extends

extends

Page 13: Collection Framework in java

COLLECTIONS HIERARCHYMAP

Map

SortedMap

Hashtable

LinkedHashMap

HashMap TreeMap

implements

implementsextends

extends

Page 14: Collection Framework in java

COLLECTION IMPLEMENTATIONS

Set : Unique things (classes that implement Set)

Map : Things with a unique ID (classes that implement Map)

List : Lists of things (classes that implement List)

Page 15: Collection Framework in java

LIST

A List cares about the index.

“Paul”“Paul” “Mark”“Mark” “John”“John” “Paul”“Paul” “Luke”“Luke”value

index 0 1 2 3 4

LinkedListLinkedListVectorVectorArrayListArrayList

Page 16: Collection Framework in java

LIST IMPLEMENTATIONSARRAY LIST

import java.util.ArrayList;

public class MyArrayList {

public static void main(String args[ ]) {

ArrayList alist = new ArrayList( );

alist.add(new String("One"));alist.add(new String("Two"));alist.add(new String("Three"));

System.out.println(alist.get(0));System.out.println(alist.get(1));System.out.println(alist.get(2));

}}

One

Two

Three

Page 17: Collection Framework in java

LIST IMPLEMENTATIONSVECTOR

import java.util.Vector;

public class MyVector {

public static void main(String args[ ]) {

Vector vecky = new Vector( );

vecky.add(new Integer(1));vecky.add(new Integer(2));vecky.add(new Integer(3));

for(int x=0; x<3; x++) { System.out.println(vecky.get(x));}

}}

1

2

3

Page 18: Collection Framework in java

LIST IMPLEMENTATIONSLINKED LIST

import java.util.LinkedList;

public class MyLinkedList {

public static void main(String args[ ]) {

LinkedList link = new LinkedList( );

link.add(new Double(2.0));link.addLast(new Double(3.0));link.addFirst(new Double(1.0));

Object array[ ] = link.toArray( );

for(int x=0; x<3; x++) { System.out.println(array[x]);}

}}

1.02.03.0

Page 19: Collection Framework in java

SET

A Set cares about uniqueness, it doesn’t allow duplicates.

“Paul”“Paul”

“Mark”“Mark”

“John”“John” “Luke”“Luke”

“Fred”“Fred”“Peter”“Peter”

TreeSetTreeSetLinkedHashSetLinkedHashSetHashSetHashSet

Page 20: Collection Framework in java

SET IMPLEMENTATIONSHASH SET

import java.util.*;

public class MyHashSet {

public static void main(String args[ ]) {

HashSet hash = new HashSet( );

hash.add("a");hash.add("b");hash.add("c");hash.add("d");

Iterator iterator = hash.iterator( );

while(iterator.hasNext( )) { System.out.println(iterator.next( ));}

}}

dacb

Page 21: Collection Framework in java

SET IMPLEMENTATIONSLINKED HASH SET

import java.util.LinkedHashSet;

public class MyLinkedHashSet {

public static void main(String args[ ]) {

LinkedHashSet lhs = new LinkedHashSet();

lhs.add(new String("One"));lhs.add(new String("Two"));lhs.add(new String("Three"));

Object array[] = lhs.toArray( );

for(int x=0; x<3; x++) { System.out.println(array[x]);}

}}

One

Two

Three

Page 22: Collection Framework in java

SET IMPLEMENTATIONSTREE SET

import java.util.TreeSet;import java.util.Iterator;

public class MyTreeSet {

public static void main(String args[ ]) {

TreeSet tree = new TreeSet();

tree.add("Jody");tree.add("Remiel");tree.add("Reggie");tree.add("Philippe");

Iterator iterator = tree.iterator( );

while(iterator.hasNext( )) { System.out.println(iterator.next( ).toString( ));}

}}

Jody

Philippe

Reggie

Remiel

Page 23: Collection Framework in java

MAP

A Map cares about unique identifiers.

“Paul”“Paul” “Mark”“Mark” “John”“John” “Paul”“Paul” “Luke”“Luke”

key

value

“Pl” “Ma” “Jn” “ul” “Le”

LinkedHashMapLinkedHashMap TreeMapTreeMapHashtableHashtableHashMapHashMap

Page 24: Collection Framework in java

MAP IMPLEMENTATIONSHASH MAP

import java.util.HashMap;

public class MyHashMap {

public static void main(String args[ ]) {

HashMap map = new HashMap( );map.put("name", "Jody");map.put("id", new Integer(446));map.put("address", "Manila");

System.out.println("Name: " + map.get("name"));System.out.println("ID: " + map.get("id"));System.out.println("Address: " + map.get("address"));

}}

Name: Jody

ID: 446

Address: Manila

Page 25: Collection Framework in java

MAP IMPLEMENTATIONSHASH TABLE

import java.util.Hashtable;

public class MyHashtable {

public static void main(String args[ ]) {

Hashtable table = new Hashtable( );table.put("name", "Jody");table.put("id", new Integer(1001));table.put("address", new String("Manila"));

System.out.println("Table of Contents:" + table);

}}

Table of Contents:

{address=Manila, name=Jody, id=1001}

Page 26: Collection Framework in java

MAP IMPLEMENTATIONSLINKED HASH MAP

import java.util.*;

public class MyLinkedHashMap {

public static void main(String args[ ]) {int iNum = 0;LinkedHashMap myMap = new LinkedHashMap( );

myMap.put("name", "Jody");myMap.put("id", new Integer(446));myMap.put("address", "Manila");myMap.put("type", "Savings");

Collection values = myMap.values( );Iterator iterator = values.iterator( );

while(iterator.hasNext()) { System.out.println(iterator.next( ));}

}}

Jody

446

Manila

Savings

Page 27: Collection Framework in java

MAP IMPLEMENTATIONSTREE MAP

import java.util.*;

public class MyTreeMap {

public static void main(String args[]) {

TreeMap treeMap = new TreeMap( );

treeMap.put("name", "Jody");treeMap.put("id", new Integer(446));treeMap.put("address", "Manila");

Collection values = treeMap.values()Iterator iterator = values.iterator( );System.out.println("Printing the VALUES....");

while (iterator.hasNext()) { System.out.println(iterator.next( ));}

}}

Printing the VALUES....

Manila

446

Jody

Page 28: Collection Framework in java

COLLECTION CLASSES SUMMARY

NoBy indexXLinkedList

NoBy indexXVector

NoBy indexXArrayList

NoBy insertion order or last access order

XLinkedHashSet

By natural order or custom comparison rules

SortedXTreeSet

NoNoXHashSet

NoBy insertion order or last access order

XLinkedHashMap

By natural order or custom comparison rules

SortedXTreeMap

NoNoXHashtable

NoNoXHashMap

SortedOrderedListSetMapClass

Page 29: Collection Framework in java

KEY POINTS Collections Framework contains:

1. Interfaces 2. Implementations3. Operations

A list cares about the index. A set cares about uniqueness, it does not allow

duplicates. A map cares about unique identifiers.

Page 30: Collection Framework in java

THANK YOU

CPD TECHNOLOGIES Block C 9/8, Sector -7, Rohini, Delhi-110085, India

Landmark: Near Rohini East Metro Station, Opposite Metro Pillar No-397Telephone: 011-65164822Mobile: +91- 8860352748Email: [email protected]