12. 1. List Interface
12
οGenerate result ααΆα insertion order.
οααΆ Support duplicacy
οααΆ Support indexing ααΆααααααΆαααΎαα’αΆα insert element ααΆα index ααΆααααΆααααΆαα
οααΆα’αΆα synchronized αα·ααα·α synchronized ααΆαααΆααααΈα.
13. 1. List Interface
A List cares about the index.
βPaulβ βMarkβ βJohnβ βPaulβ βLukeβvalue
index 0 1 2 3 4
LinkedListVectorArrayList
20. 1. Arraylist
20
Two ways to iterate the elements of collection in java
β’ By Iterator interface.
β’ By for-each loop.
import java.util.*;
class TestCollection1{
public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();//creating arraylist
al.add("Ravi");//adding object in arraylist
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator itr=al.iterator();//getting Iterator from arraylist to traverse eleme
nts
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
22. 1. 2 Vector VS Arraylist
22
ArrayList Vector
1) ArrayList is not synchronized. Vector is synchronized.
2) ArrayList increments 50% of current array size if number of
element exceeds from its capacity.
Vector increments 100% means doubles the array size if total
number of element exceeds than its capacity.
3) ArrayList is not a legacy class, it is introduced in JDK 1.2. Vector is a legacy class.
4) ArrayList is fast because it is non-synchronized. Vector is slow because it is synchronized i.e. in multithreading
environment, it will hold the other threads in runnable or non-
runnable state until current thread releases the lock of object.
5) ArrayList uses Iterator interface to traverse the elements. Vector uses Enumeration interface to traverse the elements. But
it can use Iterator also.
23. 1. 3 Linked List
23
β’ ααΆααααΌαααΆααααααΎααΆαΆααααΈJava version 1.2ααα
β’ αααααΎααααDoubly linkedlist αααΎαααΈ store elementα
β’ α’αΆα αααα»α Duplicate element(ααΆαα»αααΌα)α
β’ Maintaininsertion order (ααΆαααΆααααααΆαααα)α
β’ Non synchronizedα
β’ α’αΆα αααααΎααΆList αααα stackqueueα
24. 24
import java.util.*;
public class TestCollection7{
public static void main(String args[]){
LinkedList<String> al=new LinkedList<String>()
;
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Ravi
Vijay
Ravi
Ajay
25. 1. 4 Linked List VS ArrayList
25
ArrayList LinkedList
1) ArrayList internally uses dynamic array to store the
elements.
LinkedList internally uses doubly linked list to store the
elements.
2) Manipulation with ArrayList is slow because it internally uses
array. If any element is removed from the array, all the bits are
shifted in memory.
Manipulation with LinkedList is faster than ArrayList because it
uses doubly linked list so no bit shifting is required in memory.
3) ArrayList class can act as a list only because it implements
List only.
LinkedList class can act as a list and queue both because it
implements List and Deque interfaces.
4) ArrayList is better for storing and accessing data. LinkedList is better for manipulating data.
26. 2. Set Interface
26
ο ααΆααΆαααΆαΆααααΈ Java 1.2
ο ααΆαα·αα’αα»ααααΆ ααα’αΆαααΆα Dupplicate αα αα»αααααααΎαααΎααα’αΆαααΆααααα Dupplicate ααααΆαα·αααΆα Error αααΎαα αΎααααα
ο ααΆαα·α support indexing ααα ααΆααααααΆ ααΆαα·αααΆαααΆα insert αα·α delete elements ααΆα index ααΆαα½αα αΎαα
28. 2. Set Interface
A Set cares about uniqueness, it doesnβt allow duplicates.
βPaulβ
βMarkβ
βJohnβ βLukeβ
βFredββPeterβ
TreeSetLinkedHashSetHashSet
30. 2.1 HashSet class
30
ο ααΆααααΌαααΆααααααΎααΆαΆααααΈ Java Version 1.2 αααααα
ο ααΆαα·α Support Dupplicate ααα
ο It doesn't guarantee any order for generating result even it does not guarantee that the order will remain constant over
time.
ο ααΆα’αα»ααααΆ ααα’αΆαααΆα null ααΆ elementα ααΆααααααΆααΆαα·αααΆα NullPointerException αααΎαα αΎααααααααΆα element ααΆ Null
ο ααΆααααΌαααΆαααΌα Hashtable class αααΎαααΈ holding elements ααααααΆα
ο ααΆαα·αααΆααααααααΆ synchronized ααααααααΎαα’αΆα ααααΎαα’αΆαααΆ synchronized ααΆααα ααΆααααααΌαααΆαα
ο αααααΎααΌα hashtable αααΎαααΈ store ααΌα elements ααααααΆα
ο ααΆ extends αα αααΈ AbstractSet class αα·α implements αα αααΈ Set interface.
ο αααα»αααα unique elements ααααα»αααΆα ααα
31. HashSet class (continue)
31
import java.util.*;
class TestCollection9{
public static void main(String args[]){
HashSet<String> al=new HashSet<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Vijay
Ravi
Ajay
32. 2.2 TreeSet class
32
οααΆααααΌαααΆααααααΎααΆαΆααααΈ Java version 1.2 ααα
οααΆαα·α support ααΌα duplicacy ααα
οααΆ generate ααΌαααΆα Order ααααααααΆααααα ascending ααααααΎαααα’αΆα αα’αΆαααΆ generate ααΆα descending ααΆαααααα
οααΆαααα»αααα unique elements ααΌα HashSet ααααα
οααΆ implements αα αααΈ NavigableSet interface αααα extends ααΈ SortedSet interface.
33. 2.2 TreeSet class (α)
33
import java.util.*;
class TestCollection11{
public static void main(String args[]){
TreeSet<String> al=new TreeSet<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Ajay
Ravi
Vijay
36. Map
A Map cares about unique identifiers.
βPaulβ βMarkβ βJohnβ βPaulβ βLukeβ
key
value
βPlβ βMaβ βJnβ βulβ βLeβ
LinkedHashMap TreeMapHashtableHashMap
37. Map World
Map
Hashtab
le
LinkedHashM
ap
Treemap
Like a slower HashMap (as
with Vector, due to its
synchronized methods). No
null values or null keys
allowed
Faster iterations;
iterates by insertion
order or last accessed;
allows one null key,
many null values
A sorted
map
HashMap
Fastest updates
(key/values);
allows one null
key, many
null values
38. 3. αααα ααααα’αααΈ TreeMap & HashTable
ο± ααΊααΆ class αααα implements αααααΈ NavigableMap interface αα·ααextends αααααΈα
AbstractMap class
ο± ααΆααα α»αααΌα value α αααααα’ααααΎ key ααααααΆ
ο± ααΆαα·αα’αΆααααα α keyαα αααα’αΆααααα α Value ααΆα
ο± ααΆααα α»αααΆαααα unique elements ααααΆα αα
ο± ααΆαααααααααΆ ascending
3.1. TreeMap
39. 3. αααα ααααα’αααΈ TreeMap & HashTable
3.1.1 Constructor αααα TreeMap
ο±TreeMap(): ααΆ constructor αααααααααΎαααΎαααΈαααααΎα empty tree map αααααααααααααααα
ascending ααΆα key value ααααααΆα
ο±TreeMap(Comparator comp): αααααΎαααΎαααΈαααααΎα empty tree-based map αααααααααααααΆα
Comparator compα
ο±TreeMap(Map m): constructor ααααααΊ ααααααααααα αα’αΆα tree map ααΆαα½αααΉαααΆα m
ααααααΆααΉααααααααααααα ascending ααΆα key value ααααααΆα
ο±TreeMap(SortedMap sm): ααΆααααααααααα αα’αΆα tree map ααΆαα½αααΉαααΆαααΈ SortedMap
sm ααααααΆαααααααααΌαααΉα sm ααααα
40. 3. αααα ααααα’αααΈ TreeMap & HashTable
3.2 Methods αααα TreeMap
Method Desciption
void Clear() remove all mapping from this treemap
Object Clone() return a shallow copy αααααTreeMap instance
Comparator comparator() return comparator αααααααααΎαααΎαααΈααααααα
Boolean containsKey
(object key)
Return true αααΎαtreemap ααααααΆαααα α»αααΌα specified key
αααααmapping
Boolean
containsValue(object
Return true αααΎ treemap αααα map αα ααΆαα keyαα½αα¬
αααααΎαααααααΆα specified valueαααα
Set entrySet() Return a set view of the mapping contained in this
map.
AΒ collectionΒ β sometimes called a container β is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers). If you have used the Java programming language β or just about any other programming language β you are already familiar with collections.
AΒ collectionΒ β sometimes called a container β is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers). If you have used the Java programming language β or just about any other programming language β you are already familiar with collections.
AΒ collectionΒ β sometimes called a container β is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers). If you have used the Java programming language β or just about any other programming language β you are already familiar with collections.
AΒ collectionΒ β sometimes called a container β is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers). If you have used the Java programming language β or just about any other programming language β you are already familiar with collections.
AΒ collectionΒ β sometimes called a container β is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers). If you have used the Java programming language β or just about any other programming language β you are already familiar with collections.
Java supplies several types of Collection:
Set: cannot contain duplicate elements, order is not important
SortedSet: like a Set, but order is important
List: may contain duplicate elements, order is important
Java also supplies some βcollection-likeβ things:
Map: a βdictionaryβ that associates keys with values, order is not important
SortedMap: like a Map, but order is important
While you can get all the details from the Java API, you are expected to learn (i.e. memorize):
The signatures of the βmost importantβ methods in each interface
The most important implementations of each interface
Java supplies several types of Collection:
Set: cannot contain duplicate elements, order is not important
SortedSet: like a Set, but order is important
List: may contain duplicate elements, order is important
Java also supplies some βcollection-likeβ things:
Map: a βdictionaryβ that associates keys with values, order is not important
SortedMap: like a Map, but order is important
While you can get all the details from the Java API, you are expected to learn (i.e. memorize):
The signatures of the βmost importantβ methods in each interface
The most important implementations of each interface