Você está na página 1de 32

GOVT.

POLYTECHNIC
AURAI
JAVA PROGRAMMING
COLLECTION FRAMEWORK

1
CONTENTS
Introduction
What is Collection
Collections Framework
Collections Hierarchy
Set
List
Map
2
COLLECTION FRAMEWORK
The collections framework define a set of interfaces and their
implementations to manipulate collection.

The collection framework also allows us to store , retrieve, and update


a set of objects.
It reduces programming effort while increasing performance.

It provides an API to work with the data structure, such as lists , tree,
maps, and sets.

3
The collection framework which is contained in the java.util
package is one of javas most powerful sub-systems.

It includes implementations of these interfaces and algorithms


to manipulate them.

Which server as a container for a group of object such as a set of


words in a dictionary or a collection.

4
OBJECTIVES
Define a collection.
Describe the collections framework.
Describe the collections hierarchy.
Demonstrate each collection implementation.

5
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.

6
COLLECTIONS FRAMEWORK

Collections Framework is a unified architecture for


managing collections
Main Parts of Collections Framework
1. 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

7
COLLECTIONS FRAMEWORK INTERFACES

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.

8
COLLECTIONS FRAMEWORK
IMPLEMENTATIONS

Set List Map

HashSet ArrayList HashMap


LinkedHashSet LinkedList LinkedHashMap
TreeSet Vector Hashtable
Tree Map
Note: Hashtable uses a lower-case t

9
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. 10


Remove object from collection

Iterate collection and inspect each object

Each operation has a corresponding method implementation


for each collection type

11
COLLECTIONS CHARACTERISTICS

Ordered :-
Elements are stored and accessed in a specific order.
Sorted :-
Elements are stored and accessed in a sorted order.
Indexed :-
Elements can be accessed using an index.
Unique :-
Collection does not allow duplicates.

12
ITERATOR

The Iterator interface enables us to sequentially traverse


and access the elements contained in a collection .

The elements of a collection can be accessed using the


methods defined by the Iterator interface.

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

13
Method Defined in the Iterator Interface

Method Description

hasNext() :- Return true if the collection contains more then one element.
next() :- Returns the next element form the collection.
remove() :- Remove the current element from the collection.

14
COLLECTIONS HIERARCHY
SET AND LIST
Collection

Implements

Set List

Implements Implements
extends

HashSet
SortedSet
extends
Implements 15

LimkedHashSet TreeSet LinkedList Vector ArrayList


COLLECTIONS HIERARCHY
MAP

Collection

Implements
extends

SortedMap

Implements
Hashtable HashMap
TreeMap
extends
16

LinkedHashMap
COLLECTION IMPLEMENTATIONS
List:- List of things(classes that implement List)

Set:-Unique things(classes that implement set)

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


17
LIST IMPLEMENTATIONS
ARRAY LIST
Import java.util.ArrayList;
One
public class MyArrayList { Two
Three
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) );

}
19
}
LIST IMPLEMENTATIONS
VECTOR
Import java.util.Vector;
1
public class MyVector { 2
3
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) );

}
}
} 20
LIST IMPLEMENTATIONS
LINKEDLIST
Import java.util.LinkedList;
1.0
public class MyLinkedList { 2.0
3 .0
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] );

}
} 21

}
SET

Paul Luke
John

Fred
Peter
Mark

A set cares about uniqueness. It doesnt allow duplicates.

HashSet LinkedHashSet Treeset 22


LIST IMPLEMENTATIONS
HASHSET
Import java.util.*;
d
a
public class MyHashSet {
c
b
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.hashnext( ) ) {
system.out.println( iterator.next( ) );

} 23
}
}
LIST IMPLEMENTATIONS
LINKEDHASHSET
Import java.util.LinkedhashSet;
One
public class MyLinkedHashSet { Two
Three
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] );

} 24
}
}
SET IMPLEMENTATIONS
TREE SET
import java.util.TreeSet;
Jody
public class MyTreeSet {
Philippe
public static void main(String args[ ]) { Reggie
Remiel
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( ));


}
}
25
}
Key: PI Ma Jn ul Le

Value: Paul Mark John Paul Luke

A map cares about unique identifier.

HashMap Hashtable LinkedHashMap TreeMap 26


MAP IMPLEMENTATIONS HASH MAP
import java.util.HashMap;

public class MyHashMap { Name: Jody


ID: 446
public static void main(String args[ Address: Manila
]) {
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"));
}
} 27
MAP IMPLEMENTATIONS
HASH 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}
28
MAP IMPLEMENTATIONS
LINKED HASH MAP
import java.util.*;
public class MyLinkedHashMap {
public static void main(String args[ ])
Jody
{
446
int iNum = 0;
Manila
LinkedHashMap myMap = new
Savings
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( ));
}
}
} 29
MAP IMPLEMENTATIONS
TREE MAP
import java.util.*;

public class MyTreeMap { Printing the VALUES....


public static void main(String args[]) { Manila
446
TreeMap treeMap = new TreeMap( ); Jody
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( ));
}
}
} 30
COLLECTION CLASSES SUMMARY
Class Map Set List Ordered Sorted
HashMap X No No
Hashtable X No No
TreeMap X Sorted By natural order or
custom comparison rules
LinkedHashmap X By insertion order or No
last access order
HashSet X No No
TreeSet X Sorted By natural order or
custom comparison rules
LinkedHashSet X By insertion order or No
last access order
ArrayList X By Index No
Vector X By Index No 31

LinkedList X By Index No
KEY POINTS

Collections Framework contains:


1. Interfaces
2. Implementations

3. Operations

A list cares about the index.


A set cares about uniqueness, it does not allow
duplicates.
A map cares about unique identifiers.

32
THANKS
YOU
33

Você também pode gostar