Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
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.
4
OBJECTIVES
Define a collection.
Describe the collections framework.
Describe the collections hierarchy.
Demonstrate each collection implementation.
5
WHAT IS A COLLECTION?
6
COLLECTIONS FRAMEWORK
7
COLLECTIONS FRAMEWORK INTERFACES
8
COLLECTIONS FRAMEWORK
IMPLEMENTATIONS
9
OPERATIONS
Basic collection operations:-
Check if collection is empty.
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
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
Collection
Implements
extends
SortedMap
Implements
Hashtable HashMap
TreeMap
extends
16
LinkedHashMap
COLLECTION IMPLEMENTATIONS
List:- List of things(classes that implement List)
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[ ]) {
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[ ] ) {
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
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[ ] ) {
lhs.add(new string(one ) );
lhs.add(new string(two ) );
lhs.add(new string(three) );
} 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");
while(iterator.hasNext( )) {
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.*;
LinkedList X By Index No
KEY POINTS
3. Operations
32
THANKS
YOU
33