ΊέΊέί£

ΊέΊέί£Share a Scribd company logo
Java Presentation
αž˜αž‡αŸ’αžˆαž˜αžŽαŸ’αžŒ αž›αž€αžΌαžš αŸ‰αŸ αžŸαž αŸ’αžœ αžœαŸ‚ αžšαŸαž… េ ឌី
Korea Software HRD Center
αž£αŸ’αž“αž€αž”αŸ’αžšαžΉαž€αžΆαžšαŸ„αžšαž›αŸ‹: αžšαžŽαŸ’αžŒ ិត αž‚αžΈαž˜β€‹αžšαŸαžαŸ’αž™αž» αž„
www.kshrd.com.kh
αž£αŸ’αž“αž€αžœαžŽαŸ’αž“αŸ†αžΆ: αžšαŸ„αž€ αŸ„αž„ αžšαŸŠαž»αž“ αŸ‰αŸαŸŠαž»αž„
αžšαŸ„αž€ αžŠαžΆαžšαŸ‰αŸ αžšαŸαž‰αž…αž·αžαž
αžαŸ’αž“αž“ αž€αŸ‹ αž—αŸ’αž“αŸ†αžΆαžšαŸαž‰ αž”αŸ’αž€ αž»αž˜αž‘αžΈ ៀ
αž”αŸ’αžšαž’αžΆαž“αžšαž‘αŸ– Collection
αžŸαž˜αžΆαž‡αŸ’αž·αž€
1. αž›αŸ„αž€ αž αŸŠαž»αŸ† αžœαŸαž“
2. αž›αŸ„αž€ αž‡αžΆαž‚αžΈαž˜αž›αŸ…
3. αž›αŸ„αž€ αžŸαž„ αž‚αž„αŸ’αž‚αžΆ
4. αž›αŸ„αž€ αž‡αžΉαž˜ αž˜αžΈαž“αžΆ
5. αž›αŸ„αž€ αž‚αžΈαž˜ αž”αž“αž αž„
αž˜αžΆαžαž·αž€αžΆ
1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections
2. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ List:Vector, ArrayList, LinkedList
3. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Set: HashSet,TreeSet
4. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Map: TreeMap,HashTable
1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections
 Collection Class αž›αŸ…αž€αž“ αž»αž„αž—αžΆαžŸ Java αž‚αžΊαž‡αžΆ Container
 Collection Class αž‡αžΆαž˜αž½αž™αž”αžŽαŸ’ ុ αŸ†αž“αž“ Class αž“αž·αž„ Interface αž‡αž½αž™αž²αŸ’αž™αž’αž“αž€αžœαžšαž›αžœαžšαž€αž˜αž˜αžœ αž·αž’αžΈαž„αŸ’αž‚αž™αžŸαŸ’αžœαž½αž›
αž› αŸ„αŸ‡αžŸαŸ’αžŸαž™ collection αž“αž“ Object
 αž‡αžΆαž’αž˜αž˜αžαžΆ Object αž˜αž½αž™αžŸαŸ’αžαž„αžŸαŸ’αžαž˜αžΆαž“αž’αžΆαžαž‡αžΆαž›αžŸαŸ’αžšαžΎαž“αžŸαŸ’αŸ‚αž›αžŸαŸ’αžαžΌαžœαž”αžΆαž“ αž€αŸ‹αžšαžΌαž›αžšαž½αž˜αž‚αŸ’αž“αž“ αžŸαŸ’αžαž˜αž½αž™
(Single Unit)αŸ”
 Collections αžŸαŸ’αžαžΌαžœαž”αžΆαž“αž›αžŸαŸ’αž”αžΎαžŸαŸ’αž”αžΆαžœαŸ‹αžœαžΆαž›αŸ‚αžΎαž˜αž”αžΈ αžšαž€αžΆαž‘αž€ αž‘αžΆαž‰αž™αž€ αž›αžšαŸ€αž”αžšαŸ† αž“αž·αž„ αžœαžšαž”αž‘αž·αž“αž“αž“αŸαž™ αŸ”
 αž‡αžΆαž‘αžΌαž›αŸ…αž–αž½αž€αžœαžΆαžαŸ†αžŽαžΆαž„αž²αŸ’αž™ data items αžŸαŸ’αŸ‚αž›αž‡αžΆ form natural group αŸ‚αžΌαžšαž‡αžΆ game poker
hand(a collection of cards),mail folder(a collection of letter) αž‡αžΆαž›αŸ‚αžΎαž˜ αŸ”
1.1 αž›αžαžΎαž’αŸ‚αžΈαž›αŸ…αž‡αžΆ Collections?
1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections
 Collection framework αž‚αžΊαž‡αžΆαž€αžΆαžšαžšαž½αž˜αž”αž‰αŸ’αž… αžΌαž› architecture αžœαŸ†αžšαžΆαž”αŸ‹αž€αžΆαžšαž”αž„αŸ’αž‚αžΆ αž‰ αž“αž·αž„
αž€αžΆαžšαž›αžŸαŸ’αž”αžΎαžŸαŸ’αž”αžΆαžœαŸ‹ collections αŸ”
 αžαžΆαž„αž›αžŸαŸ’αž€αžΆαž˜αž›αž“αŸ„αŸ‡αž‚αžΊαž‡αžΆ Collection frameworkαŸ–
β€’ Interfaces:αžœαžΆαž‚αžΊαž‡αžΆ abstract data types αžŸαŸ’αŸ‚αž›αž”αž„αŸ’αž‚αžΆ αž‰αž–αžΈ Collections αŸ”
Interface αž’αž“αž‰αŸ’αž‰αžΆ αžαž·αž²αŸ’αž™ Collections αž’αžΆαžšαžŸαŸ’αžαžΌαžœαž”αžΆαž“αž›αžŸαŸ’αž”αžΎαžŸαŸ’αž”αžΆαžœαŸ‹αž› αž™αž›αžœαžš ី
αž“αžΌαžœαž€αžΆαžšαž”αž„αŸ’αž‚αžΆ αž‰αž–αžΈαž—αžΆαž–αž›αŸ†αž’αž·αžαžšαž”αžœαŸ‹αž–αž½αž€αžœαžΆ αŸ”
β€’ Implementations: αžœαžΆαž‡αžΆαž€αžΆαžšαž’αž“αžœαžαžαž‡αžΆαž€αŸ‹ αžŸαŸ’αžœαž αž„αž“αžΌαžœ Collections
interfaces αŸ” αžšαŸ†αžŽαžšαžœαŸ†αžαžΆαž“αŸ‹αžšαž”αžœαŸ‹αžœαžΆαž‚αžΊαž’αžΆαžšαž›αžŸαŸ’αž”αžΎαžŸαŸ’αž”αžΆαžœαŸ‹αž› αžΎαž„αžœ αž·αž‰αž›αŸ… data
structure αŸ”
1.1 αž›αžαžΎαž’αŸ‚αžΈαž›αŸ…αž‡αžΆ Collections Framework?
1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections
β€’ Algorithms:αžœαžΆαž‚αžΊαž‡αžΆ methods αžŸαŸ’αŸ‚αž›αž’αž“αžœαžαžαž€αžΆαžšαž‚αžŽαž“αžΆαŸ‚αŸαž˜αžΆαž“
αžŸαŸ’αž”αž›αŸ„αž‡αž“αŸ αŸ‚αžΌαžšαž‡αžΆ αž€αžΆαžšαžŸαŸ’αžŸαžœαžŸαŸ’αž‡αžΆαžœ αž“αž·αž„ αž›αžšαŸ€αž”αžšαŸ† αž›αŸ…αž›αž›αžΎ Object αžŸαŸ’αŸ‚αž›αž’αž“
αžœαžαžαž“αžΌαžœαž›αž›αžΎ collection interfaces αŸ” Algorithms αžŸαŸ’αžαžΌαžœαž”αžΆαž“αž›αž‚αž›αŸ…αžαžΆ
polymorphic αžŸαŸ’αŸ‚αž›αŸ‚αžΌαžšαž‚αŸ’αž“αž“ αž›αŸ…αž“αžΉαž„ methods αžŸαŸ’αŸ‚αž›αž’αžΆαžšαž›αž’αŸ‚αžΎαž”αžΆαž“αž›αžŸαŸ’αžšαžΎαž“αž‘αŸ†
αžšαž„αŸ‹αž›αŸ…αžαžΆαž˜ collections interfaces αŸ” αžšαŸ†αžŽαžšαžœαŸ†αžαžΆαž“αŸ‹αžšαž”αžœαŸ‹αžœαžΆαž‚αžΊαž’αžΆαžšαž›αžŸαŸ’αž”αžΎ
αžŸαŸ’αž”αžΆαžœαŸ‹αž› αžΎαž„αžœ αž·αž‰αž›αŸ… functionality αŸ”
1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections
 Reduces programming effort:αžœαžΆαž”αžΆαž“αž•αŸ’αžαž›αŸ‹αž“αžΌαžœ data structure αž“αž·αž„ algorithms
αŸ‚αžΌαž›αžšαž“αŸ„αŸ‡αž’αž“αž€αž˜αž·αž“αž…αŸ†αž”αžΆαžšαŸ‹αžαžΆαŸ‚ αž™αžαŸ’αŸ‚αž›αŸ‹ αž“αž·αž„ αž›αžαž αžαž›αŸ…αž›αž›αžΎαž€αž·αžšαž…αž€αžΆαžšαžŸαŸ’αŸ‚αž›αž’αž“αž€αž›αž’αŸ‚αžΎαž›αž“αžΆαŸ„αŸ‡
αž”αžΆαž“αž›αž αžΎαž™ αŸ” αžœαžΆαž”αžΆαž“αž‡αž½αž™αžœαžŸαŸ’αž˜αž½αž›αŸ‚αž›αŸ‹ API αž˜αž½αž™αžšαŸ†αž“αž½αž“αžŸαŸ’αŸ‚αž›αž˜αž·αž“αž‘αžΆαž€αŸ‹αž‘αž„ αž› αž™αž•αŸ’αž
αž›αŸ‹αž²αŸ’αž™αž› αž™αž˜αž·αž“αž…αŸ†αž”αžΆαžšαŸ‹αžœαžšαž›αžœαžš adapter object ឬ αž”αŸ† αžŸαŸ’αž›αž„αž€αžΌαŸ‚αž›αŸ‚αžΎαž˜αž”αžΈ connect
API αž› αžΎαž™ αŸ”
 Increases program speed and quality: αžœαžΆαž”αžΆαž“αž•αŸ’αžαž›αŸ‹αž“αžΌαžœ high-performance
high-quality αŸ‚αž›αŸ‹αž€αžΆαžšαž’αž“αžœαžαžαž“αž“ data structure αž“αž·αž„ algorithms αž²αŸ’αž™αž€αžΆαž“αŸ‹ αžŸαŸ’αžαž›αž’ αŸ”
αž˜αžΆαž“αž“αŸαž™αžαžΆ αž’αž“αž€αžœαžšαž›αžœαžšαž€αž˜αž˜αžœ αž·αž’αžΈαž˜αž·αž“αž”αžΆαžšαŸ‹αž αžαŸ‹αž›αž“αžΏαž™αž›αŸ…αž’αž„αž‚ αž»αž™αžœαžšαž›αžœαžš
structure αž› αž™αžαŸ’αž› αž½αž“αž―αž„ αž›αž αžΎαž™αž›αž™αžΎαž„αž“αžΉαž„αž˜αžΆαž“αž›αž–αž›αž›αžœαŸ„αž›αžŸαŸ’αžšαžΎαž“αž›αŸ‚αžΎαž˜αž”αžΈαž›αž’αŸ‚αžΎαž²αŸ’αž™αž€αž˜αž˜
1.1 αž‚αžŽαžœαž˜αž”αžαžαž·αž“αž“ Collections Framework?
1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections
 Allows interoperability among unrelated APIs: Collection interfaces αž‚αžΊαž‡αžΆ
αž€αžΆαžšαžœαžšαž›αžœαžšαž›αŸ…αž€αž“ αž»αž„αžŸαŸ’αž•αŸ’αž“αž€αžŽαžΆαž˜αž½αž™ αž›αž αžΎαž™ API αž—αžΆαžΆ αž”αŸ‹ collection αž›αŸ…αžœαžœ αž·αž‰αž›αŸ…αž˜αž€ αŸ”
αžŸαŸ’αž”αžœαž·αž“αž›αž”αžΎ network administration API αž•αŸ’αžαž›αŸ‹αž²αŸ’αž™ collection node name
 Increases program speed and quality: αžœαžΆαž”αžΆαž“αž•αŸ’αžαž›αŸ‹αž“αžΌαžœ high-performance
high-quality αŸ‚αž›αŸ‹αž€αžΆαžšαž’αž“αžœαžαžαž“αž“ data structure αž“αž·αž„ algorithms αž²αŸ’αž™αž€αžΆαž“αŸ‹ αžŸαŸ’αžαž›αž’ αŸ”
αž˜αžΆαž“αž“αŸαž™αžαžΆ αž’αž“αž€αžœαžšαž›αžœαžšαž€αž˜αž˜αžœ αž·αž’αžΈαž˜αž·αž“αž”αžΆαžšαŸ‹αž αžαŸ‹αž›αž“αžΏαž™αž›αŸ…αž’αž„αž‚ αž»αž™αžœαžšαž›αžœαžš
structure αž› αž™αžαŸ’αž› αž½αž“αž―αž„ αž›αž αžΎαž™αž›αž™αžΎαž„αž“αžΉαž„αž˜αžΆαž“αž›αž–αž›αž›αžœαŸ„αž›αžŸαŸ’αžšαžΎαž“αž›αŸ‚αžΎαž˜αž”αžΈαž›αž’αŸ‚αžΎαž²αŸ’αž™αž€αž˜αž˜
វវ αž·αž’αžΈαžš αž·αžαžŸαŸ’αžαžŸαŸ’αž”αž›αžœαžΎαžšαž› αžΎαž„ αŸ”
1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections
 Java αž•αŸ’αžαž›αŸ‹αž²αŸ’αž™αž›αŸ…αžŸαŸ’αž”αž›αŸαž‘αž“αž“ Collection αž˜αž½αž™αžšαŸ†αž“αž½αž“αŸ‚αžΌαžšαž‡αžΆαŸ–
Set:αž˜αž·αž“αž’αžΆαžšαž˜αžΆαž“ element αžœαž‘ αž½αž“αž‚αŸ’αž“αž“ ,αž›αž αžΎαž™ order αž‚αžΊαž˜αž·αž“αž…αŸ†αž”αžΆαžšαŸ‹αžαžΆαž˜
αž›αŸ† αž”αŸ‹αž›αž‘
οƒΌ SortedSet: αŸ‚αžΌαžšαž‡αžΆ Set αž”αžŸαŸ’αž“αž order αž…αŸ†αž”αžΆαžšαŸ‹αžœ αž·αž‰
οƒΌ List: αž’αžΆαžšαž˜αžΆαž“ element αžœαž‘ αž½αž“αž‚αŸ’αž“αž“ αž›αž αžΎαž™ order
αž‚αžΊαžŸαŸ’αžαžΌαžœαžŸαŸ’αžαžαžΆαž˜αž›αŸ† αž”αŸ‹ αŸ”
 Java αž€αŸαž•αŸ’αžαž›αŸ‹αž²αŸ’αž™αž“αžΌαžœ Collection-like αž˜αž½αž™αžšαŸ†αž“αž½αž“αŸ‚αžΌαžšαž‡αžΆαŸ–
οƒΌ Map: β€œdictionary” αžŸαŸ’αŸ‚αž› associates key
αž“αž·αž„ value αž”αžŸαŸ’αž“αž order αž‚αžΊαž˜αž·αž“αž…αŸ†αž”αžΆαžšαŸ‹αžαžΆαž˜αž›αŸ† αž”αŸ‹αž›αž‘
1.1 αžŸαŸ’αž”αž›αŸαž‘αž“αž“ Collections?
1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections
1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections
11
1. List Interface
12
οƒ˜Generate result តអម insertion order.
οƒ˜αžœαžΆ Support duplicacy
οƒ˜αžœαžΆ Support indexing αž˜αžΆαž“αž“αŸαž™αžαžΆαž™αž™αžΎαž„αž’αžΆαž… insert element តអម index αž‡αžΆαž€αŸ‹αž›αžΆαž€αŸ‹αž”αžΆαž“αŸ”
οƒ˜αžœαžΆαž’αžΆαž… synchronized αž“αž·αž„αž˜αž·αž“ synchronized αž”αžΆαž“αž‘αžΆαŸ†αž„αž–αžΈαžš.
1. List Interface
A List cares about the index.
β€œPaul” β€œMark” β€œJohn” β€œPaul” β€œLuke”value
index 0 1 2 3 4
LinkedListVectorArrayList
List
ArrayLi
st
LinkedLi
st
Vecto
r
Fast iteration and fast
random access
Good for adding
elements to the
ends, i.e. stacks
and queues
It's like a
slower
ArrayList,
but it has
synchronize
d methods
1. 1 Vector
15
οƒ˜ αžœαžΆαžαŸ’αžšαžΌαžœαž”αžΆαž“αž™αžαŸ’αžšαžΎαžαžΆαžΆαŸ†αž„αž–αžΈ Java versoin 1.0 αž˜αž€αŸ”
οƒ˜ Generate result តអម insertion order.
οƒ˜ វអ support duplicacy.
οƒ˜ វអ Increase capacity size αž–αžΈαžšαžŠαž„αž“αž“ capacity αž™αžŠαžΎαž˜αžœαžΆ
οƒ˜ αžœαžΆαžαŸ’αžšαžΌαžœαž”αžΆαž“αž™αŸαžŸαŸ’αž‚αžΆ αž›αŸ‹αžαžΆαž‡αžΆ legacy class(old class) .
οƒ˜ αžœαžΆαž˜αžΆαž“αž›αž€αžαžŽαŸˆ synchronized ឬ thread-safe.
οƒ˜ αž™αŸ„αž™αžŸαŸ’αž‚αžšαžœαžΆαž˜αžΆαž“αž›αž€αžαžŽαŸˆ synchronization αžœαžΆαž™αŸ’αžœαžΎαž™αž’αžΆαž™ Process slow αžŠαžΌαž™αž…αž“αŸαŸ‡αž™αŸαž˜αž·αž“αžŸαžΌαžœαž”αžΆαž“αž™αžαŸ’αžšαžΎαžœαžΆαž™αŸαŸ”
οƒ˜ αžœαžΆαž˜αžΆαž“αž›αž€αžαžŽαŸˆ dynami
16
Public static void main(String arg[]){
Vectorv=newvector();
For(int i=0; i<=10; i++){
v.addelement(i);}
System.out.print(al); //0,1,2……..10;
Enumeration e=v.element();
While(itr.hasNextElement()){
Integer in=(Integer)itr.nextElement();
If(in%2==0)
System.out.println(in); //0,2,4,6,8,10
}
System.out.print(al); //0,1,2……..10
}
1. 1 Vector
1. Arraylist
17
β€’ αž™αžαŸ’αžšαžΎαž›αž€αžαžŽ Dynamic Array αž™αžŠαžΎαž˜αž”αžΈstore element(αž•αŸ’αž‘αž»αž€αž’αžΆαžšαž»)αŸ”
β€’ αž’αžΆαž…αž•αŸ’αž‘αž»αž€ Duplicate element(αž’αžΆαžšαž»αžŸαž‘αžΌαž“)αŸ”
β€’ Maintaininsertion order (αžαžΆαž˜αž›αžΆαŸ†αŸ„αžšαŸ‹αž›αžΆαŸ†αž™αŸ„αž™)αŸ”
β€’ Non synchronized
β€’ αž’αž“αž»αž‰αŸ’αž‰αžΆ αžšαž€αžΆαžš Random access αž™αžαŸ’αžšαŸαŸ‡αžœαžΆαž™αŸ’αžœαžΎαž€αžΆαžšαž‡αžΆαž›αž€αžαžŽ indexαŸ”
β€’ αž™αž™αžΎαž„αž’αžΆαž… Iterate element αž“αž“collection αž€αž“αž»αž„ Java αžαžΆαž˜αŸ’αžšαž™αžšαŸ€αžšαŸαžΉ: Iterator interface & For-each Loop
18
β€’ Syntax:
Arraylist<String>al=newArraylist<String>();
1. Arraylist
19
Public static void main(String arg[]){
ArrayList al=new ArrayList();
For(int i=0; i<=10; i++){
al.add(i);}
System.out.print(al); //0,1,2……..10;
Iterator itr=al.iterator();
While(itr.hasNext()){
Integerin=(Integer) itr.next();
If(in%2==0)
System.out.println(in); //0,2,4,6,8,10
Else
itr.remove();
}
System.out.print(al); //0,2,4,6,8,10
}
1. Arraylist
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());
}
}
}
1. Arraylist
21
import java.util.*;
class TestCollection2{
public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
for(String obj:al)
System.out.println(obj);
}
}
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.
1. 3 Linked List
23
β€’ αžœαžΆαžαŸ’αžšαžΌαžœαž”αžΆαž“αž™αžαŸ’αžšαžΎαžαžΆαžΆαŸ†αž„αž–αžΈJava version 1.2αž˜αž€αŸ”
β€’ αž™αžαŸ’αžšαžΎαž›αž€αžαžŽDoubly linkedlist αž™αžŠαžΎαž˜αž”αžΈ store elementαŸ”
β€’ αž’αžΆαž…αž•αŸ’αž‘αž»αž€ Duplicate element(αž’αžΆαžšαž»αžŸαž‘αžΌαž“)αŸ”
β€’ Maintaininsertion order (αžαžΆαž˜αž›αžΆαŸ†αŸ„αžšαŸ‹αž›αžΆαŸ†αž™αŸ„αž™)αŸ”
β€’ Non synchronizedαŸ”
β€’ αž’αžΆαž…αž™αžαŸ’αžšαžΎαž‡αžΆList αž›αž€αžαžŽ stackqueueαŸ”
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
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.
2. Set Interface
26
οƒ˜ αžœαžΆαž˜αžΆαž“αžαžΆαžΆαŸ†αž„αž–αžΈ Java 1.2
οƒ˜ αžœαžΆαž˜αž·αž“αž’αž“αž»αž‰αŸ’αž‰αžΆ αžšαž™αž’αžΆαž™αž˜αžΆαž“ Dupplicate αž™αŸ αžšαž»αž“αŸ’αž“αŸ‚αž™αžšαžΎαž™αž™αžΎαž„αž™αž’αžΆαž™αž˜αžΆαž“αžšαž“αž˜αŸƒ Dupplicate αž€αŸαžœαžΆαž˜αž·αž“αž˜αžΆαž“ Error αž™αž€αžΎαžšαž™ αžΎαž„αž“αŸ’αžŠαžš
οƒ˜ αžœαžΆαž˜αž·αž“ support indexing αž™αŸαŸ” αž˜αžΆαž“αž“αŸαž™αžαžΆ αžœαžΆαž˜αž·αž“αž˜αžΆαž“αž€αžΆαžš insert αž“αž·αž„ delete elements តអម index αžŽαžΆαž˜αž½αž™αž™ αžΎαž™αŸ”
Hierarchy of HashSet class:
27
2. Set Interface
A Set cares about uniqueness, it doesn’t allow duplicates.
β€œPaul”
β€œMark”
β€œJohn” β€œLuke”
β€œFredβ€β€œPeter”
TreeSetLinkedHashSetHashSet
Set
HashSet
LinkedHashSet
TreeSet
Fast access, assures
no duplicates,
provides no ordering
No duplicates;
iterates by insertion
order
No
duplicates;
iterates in
sorted order
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 αž“αŸ’αžšαžšαž»αž™αžŽαžΆαžŽ αŸαŸ‡αŸ”
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
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.
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
3. Map Interface
34
οƒ˜ αžœαžΆαž˜αžΆαž“αžαžΆαžΆαŸ†αž„αž–αžΈ Java 1.2αž˜αž€αŸ”
οƒ˜ αžœαžΆαž™αŸ’αžœαžΎαž€αžΆαžšαž›αž€αžαžŽαŸˆαž‡αžΆ key-value paired techniques(Hashing techniques).
οƒ˜ αžœαžΆαž˜αž·αž“αž’αžΆαž…αž˜αžΆαž“ duplicate αž”αžΆαž“αž™αŸαžšαž»αž“αŸ’αž“αŸ‚αžœαžΆαž’αžΆαž…αž˜αžΆαž“ duplicate values.
οƒ˜ αžœαžΆαž˜αž·αž“αž˜αžΆαž“αž›αž€αžαžŽαŸˆsynchronized αž™αŸαŸ”
3. Map Interface
35
Map
A Map cares about unique identifiers.
β€œPaul” β€œMark” β€œJohn” β€œPaul” β€œLuke”
key
value
β€œPl” β€œMa” β€œJn” β€œul” β€œLe”
LinkedHashMap TreeMapHashtableHashMap
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
3. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ TreeMap & HashTable
 αž‚αžΊαž‡αžΆ class αžŸαŸ’αŸ‚αž› implements αž›αžšαž‰αž–αžΈ NavigableMap interface αž“αž·αž„αžœextends αž›αžšαž‰αž–αžΈαžœ
AbstractMap class
 αžœαžΆαž•αŸ’αž‘ αž»αž€αž“αžΌαžœ value αž› αž™αžŸαŸ’αž•αŸ’αž’αž€αž›αž›αžΎ key αžšαž”αžœαŸ‹αžœαžΆ
 αžœαžΆαž˜αž·αž“αž’αžΆαžšαž‚αŸ’αž“αž˜ αž“ keyαž›αž‘ αžŸαŸ’αžαž’αžΆαžšαž‚αŸ’αž“αž˜ αž“ Value αž”αžΆαž“
 αžœαžΆαž•αŸ’αž‘ αž»αž€αž”αžΆαž“αžŸαŸ’αž unique elements αž”αž›αžŽαžΆαžŽ αŸ„αŸ‡
 αžœαžΆαžαž›αžŸαŸ’αž˜αŸ€αž”αž‡αžΆ ascending
3.1. TreeMap
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 αžŸαŸ’αŸ‚αžšαŸ”
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.
3. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ TreeMap & HashTable
 HashTable implements αž–αžΈ Map Interface αž“αž·αž„αžœextends αž–αžΈ Dictionary
Class
 αžœαžΆαž•αŸ’αž‘ αž»αž€αž“αžΌαžœ value αž› αž™αž–αžΉαž„αžŸαŸ’αž•αŸ’αž’αž€αž›αž›αžΎ key
 αžœαžΆαž•αŸ’αž‘ αž»αž€αžŸαŸ’αžαžœunique elements
 αžœαžΆαŸ‚αžΌαžšαž“αžΉαž„ HashMap αžŸαŸ’αŸ‚αžšαžŸαŸ’αžαžαŸ’αžœαžŸαŸ’αžαž„αŸ‹αžœαžΆαž‚αžΊ synchronized
 αžœαžΆαž˜αž·αž“αžαž›αžŸαŸ’αž˜αŸ€αž”αŸ‚αžΌαžšαž›αŸ…αž“αžΉαž„ TreeMap αž›αž‘
3.3 HashTable
3. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ TreeMap & HashTable
3.3 Methods αžšαž”αžœαŸ‹ HashTable
Method Desciption
void clear() remove key value αž‘αžΆαŸ†αž„αž’αžœαŸ‹αž–αž·αžœhashtable
Object Clone() return a shallow copy αž–αžΈ hashtable αž›αž“αŸ„αŸ‡
boolean contains(Object
value)
Test αž›αž”αžΎαžœkey map αž›αŸ…αž€αžΆαž“αŸ‹specified value
boolean isEmpty() Test αž›αž”αžΎ hashtable maps αž‚αŸ’αž“αž˜ αž“ keyαž›αŸ…αž€αžΆαž“αŸ‹value
Enumeration keys() Return αž“αžΌαžœαž€αžΆαžšαžšαžΆαž”αŸ‹αž›αŸ’αž˜ αŸ„αŸ‡αž“αž“ keys αžŸαŸ’αŸ‚αž›αž˜αžΆαž“αž€αž“ αž»αž„
hashtable
Object put(object
key,object value)
Maps αž“αžœ specified key αž›αŸ…αž€αžΆαž“αŸ‹specified value
3. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ TreeMap & HashTable
3.3 Methods αžšαž”αžœαŸ‹ HashTable
Method Desciption
void rehash() αžαž›αž˜αŸƒαžΎαž„αž‘αŸ†αž αŸ†αž“αž“ hashtableαž“αž·αž„αžαž›αž˜αŸƒαžΎαž„ keys αž‘αžΆαŸ†αž„αž’αžœαŸ‹
វអ
object remove(object key) remove keyαž“αž·αž„αžœvalue αžšαž”αžœαŸ‹ key αž›αž“αžΆαŸ„αŸ‡
int size() Return αžšαŸ†αž“αž½αž“ key-value αžŸαŸ’αŸ‚αž›αž€αŸ†αž–αž„ map
String toString() Return string αžŸαŸ’αŸ‚αž›αžœαž˜αž˜αžΌαž›
Boolean
key)
Test αž›αž”αžΎ specified object αž‚αžΊαž‡αžΆ keyαž€αž“ αž»αž„ hashtable
Boolean
containValue(object value)
Test αž›αž”αžΎ specified object αž‚αžΊαž‡αžΆ valueαž€αž“ αž»αž„ hashtableαŸ”
return false αž›αž–αž›αž˜αž·αž“αž˜αžΆαž“value αžŽαžΆαž›αžœαž˜ ើរ
3. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ TreeMap & HashTable
3.3 Methods αžšαž”αžœαŸ‹ HashTable
Method Desciption
Enumeration elements() Return αž“αžΌαžœαž€αžΆαžšαžšαžΆαž”αŸ‹αž›αŸ’αž˜ αŸ„αŸ‡αž“αž“ value αžŸαŸ’αŸ‚αž›αž˜αžΆαž“αž€αž“ αž»αž„
hashtable
object get(object key) remove keyαž“αž·αž„αžœvalue αžšαž”αžœαŸ‹ key αž›αž“αžΆαŸ„αŸ‡
αž”αŸ’αžšαž—αŸ’αŸαž―αž€αžŸαžΆ
45
β€’ http://tutorials.jenkov.com/java-collections/index.html
β€’ http://www.javatpoint.com/Map-interface-in-collection-
framework
β€’ http://www.javamadesoeasy.com/2015/04/arraylist-in-
java.html
β€’ http://www.javamadesoeasy.com/2015/04/arraylist-in-
java.html
β€’ http://hackersstudy.tistory.com/26
β€’ http://www.javaperspective.com/collections.html
β€’ https://developer.apple.com/library/ios/documentation/Swift/
Conceptual/Swift_Programming_Language/CollectionTypes
.html
β€’ https://docs.oracle.com/javase/tutorial/collections/intro/
β€’ http://www.javatpoint.com/collections-in-java
សូមធរេុណ!!!
46

More Related Content

Collection

  • 1. Java Presentation αž˜αž‡αŸ’αžˆαž˜αžŽαŸ’αžŒ αž›αž€αžΌαžš αŸ‰αŸ αžŸαž αŸ’αžœ αžœαŸ‚ αžšαŸαž… េ ឌី Korea Software HRD Center αž£αŸ’αž“αž€αž”αŸ’αžšαžΉαž€αžΆαžšαŸ„αžšαž›αŸ‹: αžšαžŽαŸ’αžŒ ិត αž‚αžΈαž˜β€‹αžšαŸαžαŸ’αž™αž» αž„ www.kshrd.com.kh αž£αŸ’αž“αž€αžœαžŽαŸ’αž“αŸ†αžΆ: αžšαŸ„αž€ αŸ„αž„ αžšαŸŠαž»αž“ αŸ‰αŸαŸŠαž»αž„ αžšαŸ„αž€ αžŠαžΆαžšαŸ‰αŸ αžšαŸαž‰αž…αž·αžαž
  • 2. αžαŸ’αž“αž“ αž€αŸ‹ αž—αŸ’αž“αŸ†αžΆαžšαŸαž‰ αž”αŸ’αž€ αž»αž˜αž‘αžΈ ៀ αž”αŸ’αžšαž’αžΆαž“αžšαž‘αŸ– Collection αžŸαž˜αžΆαž‡αŸ’αž·αž€ 1. αž›αŸ„αž€ αž αŸŠαž»αŸ† αžœαŸαž“ 2. αž›αŸ„αž€ αž‡αžΆαž‚αžΈαž˜αž›αŸ… 3. αž›αŸ„αž€ αžŸαž„ αž‚αž„αŸ’αž‚αžΆ 4. αž›αŸ„αž€ αž‡αžΉαž˜ αž˜αžΈαž“αžΆ 5. αž›αŸ„αž€ αž‚αžΈαž˜ αž”αž“αž αž„
  • 3. αž˜αžΆαžαž·αž€αžΆ 1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections 2. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ List:Vector, ArrayList, LinkedList 3. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Set: HashSet,TreeSet 4. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Map: TreeMap,HashTable
  • 4. 1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections  Collection Class αž›αŸ…αž€αž“ αž»αž„αž—αžΆαžŸ Java αž‚αžΊαž‡αžΆ Container  Collection Class αž‡αžΆαž˜αž½αž™αž”αžŽαŸ’ ុ αŸ†αž“αž“ Class αž“αž·αž„ Interface αž‡αž½αž™αž²αŸ’αž™αž’αž“αž€αžœαžšαž›αžœαžšαž€αž˜αž˜αžœ αž·αž’αžΈαž„αŸ’αž‚αž™αžŸαŸ’αžœαž½αž› αž› αŸ„αŸ‡αžŸαŸ’αžŸαž™ collection αž“αž“ Object  αž‡αžΆαž’αž˜αž˜αžαžΆ Object αž˜αž½αž™αžŸαŸ’αžαž„αžŸαŸ’αžαž˜αžΆαž“αž’αžΆαžαž‡αžΆαž›αžŸαŸ’αžšαžΎαž“αžŸαŸ’αŸ‚αž›αžŸαŸ’αžαžΌαžœαž”αžΆαž“ αž€αŸ‹αžšαžΌαž›αžšαž½αž˜αž‚αŸ’αž“αž“ αžŸαŸ’αžαž˜αž½αž™ (Single Unit)αŸ”  Collections αžŸαŸ’αžαžΌαžœαž”αžΆαž“αž›αžŸαŸ’αž”αžΎαžŸαŸ’αž”αžΆαžœαŸ‹αžœαžΆαž›αŸ‚αžΎαž˜αž”αžΈ αžšαž€αžΆαž‘αž€ αž‘αžΆαž‰αž™αž€ αž›αžšαŸ€αž”αžšαŸ† αž“αž·αž„ αžœαžšαž”αž‘αž·αž“αž“αž“αŸαž™ αŸ”  αž‡αžΆαž‘αžΌαž›αŸ…αž–αž½αž€αžœαžΆαžαŸ†αžŽαžΆαž„αž²αŸ’αž™ data items αžŸαŸ’αŸ‚αž›αž‡αžΆ form natural group αŸ‚αžΌαžšαž‡αžΆ game poker hand(a collection of cards),mail folder(a collection of letter) αž‡αžΆαž›αŸ‚αžΎαž˜ αŸ” 1.1 αž›αžαžΎαž’αŸ‚αžΈαž›αŸ…αž‡αžΆ Collections?
  • 5. 1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections  Collection framework αž‚αžΊαž‡αžΆαž€αžΆαžšαžšαž½αž˜αž”αž‰αŸ’αž… αžΌαž› architecture αžœαŸ†αžšαžΆαž”αŸ‹αž€αžΆαžšαž”αž„αŸ’αž‚αžΆ αž‰ αž“αž·αž„ αž€αžΆαžšαž›αžŸαŸ’αž”αžΎαžŸαŸ’αž”αžΆαžœαŸ‹ collections αŸ”  αžαžΆαž„αž›αžŸαŸ’αž€αžΆαž˜αž›αž“αŸ„αŸ‡αž‚αžΊαž‡αžΆ Collection frameworkαŸ– β€’ Interfaces:αžœαžΆαž‚αžΊαž‡αžΆ abstract data types αžŸαŸ’αŸ‚αž›αž”αž„αŸ’αž‚αžΆ αž‰αž–αžΈ Collections αŸ” Interface αž’αž“αž‰αŸ’αž‰αžΆ αžαž·αž²αŸ’αž™ Collections αž’αžΆαžšαžŸαŸ’αžαžΌαžœαž”αžΆαž“αž›αžŸαŸ’αž”αžΎαžŸαŸ’αž”αžΆαžœαŸ‹αž› αž™αž›αžœαžš ី αž“αžΌαžœαž€αžΆαžšαž”αž„αŸ’αž‚αžΆ αž‰αž–αžΈαž—αžΆαž–αž›αŸ†αž’αž·αžαžšαž”αžœαŸ‹αž–αž½αž€αžœαžΆ αŸ” β€’ Implementations: αžœαžΆαž‡αžΆαž€αžΆαžšαž’αž“αžœαžαžαž‡αžΆαž€αŸ‹ αžŸαŸ’αžœαž αž„αž“αžΌαžœ Collections interfaces αŸ” αžšαŸ†αžŽαžšαžœαŸ†αžαžΆαž“αŸ‹αžšαž”αžœαŸ‹αžœαžΆαž‚αžΊαž’αžΆαžšαž›αžŸαŸ’αž”αžΎαžŸαŸ’αž”αžΆαžœαŸ‹αž› αžΎαž„αžœ αž·αž‰αž›αŸ… data structure αŸ” 1.1 αž›αžαžΎαž’αŸ‚αžΈαž›αŸ…αž‡αžΆ Collections Framework?
  • 6. 1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections β€’ Algorithms:αžœαžΆαž‚αžΊαž‡αžΆ methods αžŸαŸ’αŸ‚αž›αž’αž“αžœαžαžαž€αžΆαžšαž‚αžŽαž“αžΆαŸ‚αŸαž˜αžΆαž“ αžŸαŸ’αž”αž›αŸ„αž‡αž“αŸ αŸ‚αžΌαžšαž‡αžΆ αž€αžΆαžšαžŸαŸ’αžŸαžœαžŸαŸ’αž‡αžΆαžœ αž“αž·αž„ αž›αžšαŸ€αž”αžšαŸ† αž›αŸ…αž›αž›αžΎ Object αžŸαŸ’αŸ‚αž›αž’αž“ αžœαžαžαž“αžΌαžœαž›αž›αžΎ collection interfaces αŸ” Algorithms αžŸαŸ’αžαžΌαžœαž”αžΆαž“αž›αž‚αž›αŸ…αžαžΆ polymorphic αžŸαŸ’αŸ‚αž›αŸ‚αžΌαžšαž‚αŸ’αž“αž“ αž›αŸ…αž“αžΉαž„ methods αžŸαŸ’αŸ‚αž›αž’αžΆαžšαž›αž’αŸ‚αžΎαž”αžΆαž“αž›αžŸαŸ’αžšαžΎαž“αž‘αŸ† αžšαž„αŸ‹αž›αŸ…αžαžΆαž˜ collections interfaces αŸ” αžšαŸ†αžŽαžšαžœαŸ†αžαžΆαž“αŸ‹αžšαž”αžœαŸ‹αžœαžΆαž‚αžΊαž’αžΆαžšαž›αžŸαŸ’αž”αžΎ αžŸαŸ’αž”αžΆαžœαŸ‹αž› αžΎαž„αžœ αž·αž‰αž›αŸ… functionality αŸ”
  • 7. 1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections  Reduces programming effort:αžœαžΆαž”αžΆαž“αž•αŸ’αžαž›αŸ‹αž“αžΌαžœ data structure αž“αž·αž„ algorithms αŸ‚αžΌαž›αžšαž“αŸ„αŸ‡αž’αž“αž€αž˜αž·αž“αž…αŸ†αž”αžΆαžšαŸ‹αžαžΆαŸ‚ αž™αžαŸ’αŸ‚αž›αŸ‹ αž“αž·αž„ αž›αžαž αžαž›αŸ…αž›αž›αžΎαž€αž·αžšαž…αž€αžΆαžšαžŸαŸ’αŸ‚αž›αž’αž“αž€αž›αž’αŸ‚αžΎαž›αž“αžΆαŸ„αŸ‡ αž”αžΆαž“αž›αž αžΎαž™ αŸ” αžœαžΆαž”αžΆαž“αž‡αž½αž™αžœαžŸαŸ’αž˜αž½αž›αŸ‚αž›αŸ‹ API αž˜αž½αž™αžšαŸ†αž“αž½αž“αžŸαŸ’αŸ‚αž›αž˜αž·αž“αž‘αžΆαž€αŸ‹αž‘αž„ αž› αž™αž•αŸ’αž αž›αŸ‹αž²αŸ’αž™αž› αž™αž˜αž·αž“αž…αŸ†αž”αžΆαžšαŸ‹αžœαžšαž›αžœαžš adapter object ឬ αž”αŸ† αžŸαŸ’αž›αž„αž€αžΌαŸ‚αž›αŸ‚αžΎαž˜αž”αžΈ connect API αž› αžΎαž™ αŸ”  Increases program speed and quality: αžœαžΆαž”αžΆαž“αž•αŸ’αžαž›αŸ‹αž“αžΌαžœ high-performance high-quality αŸ‚αž›αŸ‹αž€αžΆαžšαž’αž“αžœαžαžαž“αž“ data structure αž“αž·αž„ algorithms αž²αŸ’αž™αž€αžΆαž“αŸ‹ αžŸαŸ’αžαž›αž’ αŸ” αž˜αžΆαž“αž“αŸαž™αžαžΆ αž’αž“αž€αžœαžšαž›αžœαžšαž€αž˜αž˜αžœ αž·αž’αžΈαž˜αž·αž“αž”αžΆαžšαŸ‹αž αžαŸ‹αž›αž“αžΏαž™αž›αŸ…αž’αž„αž‚ αž»αž™αžœαžšαž›αžœαžš structure αž› αž™αžαŸ’αž› αž½αž“αž―αž„ αž›αž αžΎαž™αž›αž™αžΎαž„αž“αžΉαž„αž˜αžΆαž“αž›αž–αž›αž›αžœαŸ„αž›αžŸαŸ’αžšαžΎαž“αž›αŸ‚αžΎαž˜αž”αžΈαž›αž’αŸ‚αžΎαž²αŸ’αž™αž€αž˜αž˜ 1.1 αž‚αžŽαžœαž˜αž”αžαžαž·αž“αž“ Collections Framework?
  • 8. 1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections  Allows interoperability among unrelated APIs: Collection interfaces αž‚αžΊαž‡αžΆ αž€αžΆαžšαžœαžšαž›αžœαžšαž›αŸ…αž€αž“ αž»αž„αžŸαŸ’αž•αŸ’αž“αž€αžŽαžΆαž˜αž½αž™ αž›αž αžΎαž™ API αž—αžΆαžΆ αž”αŸ‹ collection αž›αŸ…αžœαžœ αž·αž‰αž›αŸ…αž˜αž€ αŸ” αžŸαŸ’αž”αžœαž·αž“αž›αž”αžΎ network administration API αž•αŸ’αžαž›αŸ‹αž²αŸ’αž™ collection node name  Increases program speed and quality: αžœαžΆαž”αžΆαž“αž•αŸ’αžαž›αŸ‹αž“αžΌαžœ high-performance high-quality αŸ‚αž›αŸ‹αž€αžΆαžšαž’αž“αžœαžαžαž“αž“ data structure αž“αž·αž„ algorithms αž²αŸ’αž™αž€αžΆαž“αŸ‹ αžŸαŸ’αžαž›αž’ αŸ” αž˜αžΆαž“αž“αŸαž™αžαžΆ αž’αž“αž€αžœαžšαž›αžœαžšαž€αž˜αž˜αžœ αž·αž’αžΈαž˜αž·αž“αž”αžΆαžšαŸ‹αž αžαŸ‹αž›αž“αžΏαž™αž›αŸ…αž’αž„αž‚ αž»αž™αžœαžšαž›αžœαžš structure αž› αž™αžαŸ’αž› αž½αž“αž―αž„ αž›αž αžΎαž™αž›αž™αžΎαž„αž“αžΉαž„αž˜αžΆαž“αž›αž–αž›αž›αžœαŸ„αž›αžŸαŸ’αžšαžΎαž“αž›αŸ‚αžΎαž˜αž”αžΈαž›αž’αŸ‚αžΎαž²αŸ’αž™αž€αž˜αž˜ វវ αž·αž’αžΈαžš αž·αžαžŸαŸ’αžαžŸαŸ’αž”αž›αžœαžΎαžšαž› αžΎαž„ αŸ”
  • 9. 1. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ Collections  Java αž•αŸ’αžαž›αŸ‹αž²αŸ’αž™αž›αŸ…αžŸαŸ’αž”αž›αŸαž‘αž“αž“ Collection αž˜αž½αž™αžšαŸ†αž“αž½αž“αŸ‚αžΌαžšαž‡αžΆαŸ– Set:αž˜αž·αž“αž’αžΆαžšαž˜αžΆαž“ element αžœαž‘ αž½αž“αž‚αŸ’αž“αž“ ,αž›αž αžΎαž™ order αž‚αžΊαž˜αž·αž“αž…αŸ†αž”αžΆαžšαŸ‹αžαžΆαž˜ αž›αŸ† αž”αŸ‹αž›αž‘ οƒΌ SortedSet: αŸ‚αžΌαžšαž‡αžΆ Set αž”αžŸαŸ’αž“αž order αž…αŸ†αž”αžΆαžšαŸ‹αžœ αž·αž‰ οƒΌ List: αž’αžΆαžšαž˜αžΆαž“ element αžœαž‘ αž½αž“αž‚αŸ’αž“αž“ αž›αž αžΎαž™ order αž‚αžΊαžŸαŸ’αžαžΌαžœαžŸαŸ’αžαžαžΆαž˜αž›αŸ† αž”αŸ‹ αŸ”  Java αž€αŸαž•αŸ’αžαž›αŸ‹αž²αŸ’αž™αž“αžΌαžœ Collection-like αž˜αž½αž™αžšαŸ†αž“αž½αž“αŸ‚αžΌαžšαž‡αžΆαŸ– οƒΌ Map: β€œdictionary” αžŸαŸ’αŸ‚αž› associates key αž“αž·αž„ value αž”αžŸαŸ’αž“αž order αž‚αžΊαž˜αž·αž“αž…αŸ†αž”αžΆαžšαŸ‹αžαžΆαž˜αž›αŸ† αž”αŸ‹αž›αž‘ 1.1 αžŸαŸ’αž”αž›αŸαž‘αž“αž“ Collections?
  • 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
  • 14. List ArrayLi st LinkedLi st Vecto r Fast iteration and fast random access Good for adding elements to the ends, i.e. stacks and queues It's like a slower ArrayList, but it has synchronize d methods
  • 15. 1. 1 Vector 15 οƒ˜ αžœαžΆαžαŸ’αžšαžΌαžœαž”αžΆαž“αž™αžαŸ’αžšαžΎαžαžΆαžΆαŸ†αž„αž–αžΈ Java versoin 1.0 αž˜αž€αŸ” οƒ˜ Generate result តអម insertion order. οƒ˜ វអ support duplicacy. οƒ˜ វអ Increase capacity size αž–αžΈαžšαžŠαž„αž“αž“ capacity αž™αžŠαžΎαž˜αžœαžΆ οƒ˜ αžœαžΆαžαŸ’αžšαžΌαžœαž”αžΆαž“αž™αŸαžŸαŸ’αž‚αžΆ αž›αŸ‹αžαžΆαž‡αžΆ legacy class(old class) . οƒ˜ αžœαžΆαž˜αžΆαž“αž›αž€αžαžŽαŸˆ synchronized ឬ thread-safe. οƒ˜ αž™αŸ„αž™αžŸαŸ’αž‚αžšαžœαžΆαž˜αžΆαž“αž›αž€αžαžŽαŸˆ synchronization αžœαžΆαž™αŸ’αžœαžΎαž™αž’αžΆαž™ Process slow αžŠαžΌαž™αž…αž“αŸαŸ‡αž™αŸαž˜αž·αž“αžŸαžΌαžœαž”αžΆαž“αž™αžαŸ’αžšαžΎαžœαžΆαž™αŸαŸ” οƒ˜ αžœαžΆαž˜αžΆαž“αž›αž€αžαžŽαŸˆ dynami
  • 16. 16 Public static void main(String arg[]){ Vectorv=newvector(); For(int i=0; i<=10; i++){ v.addelement(i);} System.out.print(al); //0,1,2……..10; Enumeration e=v.element(); While(itr.hasNextElement()){ Integer in=(Integer)itr.nextElement(); If(in%2==0) System.out.println(in); //0,2,4,6,8,10 } System.out.print(al); //0,1,2……..10 } 1. 1 Vector
  • 17. 1. Arraylist 17 β€’ αž™αžαŸ’αžšαžΎαž›αž€αžαžŽ Dynamic Array αž™αžŠαžΎαž˜αž”αžΈstore element(αž•αŸ’αž‘αž»αž€αž’αžΆαžšαž»)αŸ” β€’ αž’αžΆαž…αž•αŸ’αž‘αž»αž€ Duplicate element(αž’αžΆαžšαž»αžŸαž‘αžΌαž“)αŸ” β€’ Maintaininsertion order (αžαžΆαž˜αž›αžΆαŸ†αŸ„αžšαŸ‹αž›αžΆαŸ†αž™αŸ„αž™)αŸ” β€’ Non synchronized β€’ αž’αž“αž»αž‰αŸ’αž‰αžΆ αžšαž€αžΆαžš Random access αž™αžαŸ’αžšαŸαŸ‡αžœαžΆαž™αŸ’αžœαžΎαž€αžΆαžšαž‡αžΆαž›αž€αžαžŽ indexαŸ” β€’ αž™αž™αžΎαž„αž’αžΆαž… Iterate element αž“αž“collection αž€αž“αž»αž„ Java αžαžΆαž˜αŸ’αžšαž™αžšαŸ€αžšαŸαžΉ: Iterator interface & For-each Loop
  • 19. 19 Public static void main(String arg[]){ ArrayList al=new ArrayList(); For(int i=0; i<=10; i++){ al.add(i);} System.out.print(al); //0,1,2……..10; Iterator itr=al.iterator(); While(itr.hasNext()){ Integerin=(Integer) itr.next(); If(in%2==0) System.out.println(in); //0,2,4,6,8,10 Else itr.remove(); } System.out.print(al); //0,2,4,6,8,10 } 1. Arraylist
  • 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()); } } }
  • 21. 1. Arraylist 21 import java.util.*; class TestCollection2{ public static void main(String args[]){ ArrayList<String> al=new ArrayList<String>(); al.add("Ravi"); al.add("Vijay"); al.add("Ravi"); al.add("Ajay"); for(String obj:al) System.out.println(obj); } }
  • 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 αžŽαžΆαž˜αž½αž™αž™ αžΎαž™αŸ”
  • 27. Hierarchy of HashSet class: 27
  • 28. 2. Set Interface A Set cares about uniqueness, it doesn’t allow duplicates. β€œPaul” β€œMark” β€œJohn” β€œLuke” β€œFredβ€β€œPeter” TreeSetLinkedHashSetHashSet
  • 29. Set HashSet LinkedHashSet TreeSet Fast access, assures no duplicates, provides no ordering No duplicates; iterates by insertion order No duplicates; iterates in sorted order
  • 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
  • 34. 3. Map Interface 34 οƒ˜ αžœαžΆαž˜αžΆαž“αžαžΆαžΆαŸ†αž„αž–αžΈ Java 1.2αž˜αž€αŸ” οƒ˜ αžœαžΆαž™αŸ’αžœαžΎαž€αžΆαžšαž›αž€αžαžŽαŸˆαž‡αžΆ key-value paired techniques(Hashing techniques). οƒ˜ αžœαžΆαž˜αž·αž“αž’αžΆαž…αž˜αžΆαž“ duplicate αž”αžΆαž“αž™αŸαžšαž»αž“αŸ’αž“αŸ‚αžœαžΆαž’αžΆαž…αž˜αžΆαž“ duplicate values. οƒ˜ αžœαžΆαž˜αž·αž“αž˜αžΆαž“αž›αž€αžαžŽαŸˆsynchronized αž™αŸαŸ”
  • 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.
  • 41. 3. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ TreeMap & HashTable  HashTable implements αž–αžΈ Map Interface αž“αž·αž„αžœextends αž–αžΈ Dictionary Class  αžœαžΆαž•αŸ’αž‘ αž»αž€αž“αžΌαžœ value αž› αž™αž–αžΉαž„αžŸαŸ’αž•αŸ’αž’αž€αž›αž›αžΎ key  αžœαžΆαž•αŸ’αž‘ αž»αž€αžŸαŸ’αžαžœunique elements  αžœαžΆαŸ‚αžΌαžšαž“αžΉαž„ HashMap αžŸαŸ’αŸ‚αžšαžŸαŸ’αžαžαŸ’αžœαžŸαŸ’αžαž„αŸ‹αžœαžΆαž‚αžΊ synchronized  αžœαžΆαž˜αž·αž“αžαž›αžŸαŸ’αž˜αŸ€αž”αŸ‚αžΌαžšαž›αŸ…αž“αžΉαž„ TreeMap αž›αž‘ 3.3 HashTable
  • 42. 3. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ TreeMap & HashTable 3.3 Methods αžšαž”αžœαŸ‹ HashTable Method Desciption void clear() remove key value αž‘αžΆαŸ†αž„αž’αžœαŸ‹αž–αž·αžœhashtable Object Clone() return a shallow copy αž–αžΈ hashtable αž›αž“αŸ„αŸ‡ boolean contains(Object value) Test αž›αž”αžΎαžœkey map αž›αŸ…αž€αžΆαž“αŸ‹specified value boolean isEmpty() Test αž›αž”αžΎ hashtable maps αž‚αŸ’αž“αž˜ αž“ keyαž›αŸ…αž€αžΆαž“αŸ‹value Enumeration keys() Return αž“αžΌαžœαž€αžΆαžšαžšαžΆαž”αŸ‹αž›αŸ’αž˜ αŸ„αŸ‡αž“αž“ keys αžŸαŸ’αŸ‚αž›αž˜αžΆαž“αž€αž“ αž»αž„ hashtable Object put(object key,object value) Maps αž“αžœ specified key αž›αŸ…αž€αžΆαž“αŸ‹specified value
  • 43. 3. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ TreeMap & HashTable 3.3 Methods αžšαž”αžœαŸ‹ HashTable Method Desciption void rehash() αžαž›αž˜αŸƒαžΎαž„αž‘αŸ†αž αŸ†αž“αž“ hashtableαž“αž·αž„αžαž›αž˜αŸƒαžΎαž„ keys αž‘αžΆαŸ†αž„αž’αžœαŸ‹ វអ object remove(object key) remove keyαž“αž·αž„αžœvalue αžšαž”αžœαŸ‹ key αž›αž“αžΆαŸ„αŸ‡ int size() Return αžšαŸ†αž“αž½αž“ key-value αžŸαŸ’αŸ‚αž›αž€αŸ†αž–αž„ map String toString() Return string αžŸαŸ’αŸ‚αž›αžœαž˜αž˜αžΌαž› Boolean key) Test αž›αž”αžΎ specified object αž‚αžΊαž‡αžΆ keyαž€αž“ αž»αž„ hashtable Boolean containValue(object value) Test αž›αž”αžΎ specified object αž‚αžΊαž‡αžΆ valueαž€αž“ αž»αž„ hashtableαŸ” return false αž›αž–αž›αž˜αž·αž“αž˜αžΆαž“value αžŽαžΆαž›αžœαž˜ ើរ
  • 44. 3. αžŸαŸ’αžœαŸ‚ αž„αž™αž›αŸ‹αž’αŸ†αž–αžΈ TreeMap & HashTable 3.3 Methods αžšαž”αžœαŸ‹ HashTable Method Desciption Enumeration elements() Return αž“αžΌαžœαž€αžΆαžšαžšαžΆαž”αŸ‹αž›αŸ’αž˜ αŸ„αŸ‡αž“αž“ value αžŸαŸ’αŸ‚αž›αž˜αžΆαž“αž€αž“ αž»αž„ hashtable object get(object key) remove keyαž“αž·αž„αžœvalue αžšαž”αžœαŸ‹ key αž›αž“αžΆαŸ„αŸ‡
  • 45. αž”αŸ’αžšαž—αŸ’αŸαž―αž€αžŸαžΆ 45 β€’ http://tutorials.jenkov.com/java-collections/index.html β€’ http://www.javatpoint.com/Map-interface-in-collection- framework β€’ http://www.javamadesoeasy.com/2015/04/arraylist-in- java.html β€’ http://www.javamadesoeasy.com/2015/04/arraylist-in- java.html β€’ http://hackersstudy.tistory.com/26 β€’ http://www.javaperspective.com/collections.html β€’ https://developer.apple.com/library/ios/documentation/Swift/ Conceptual/Swift_Programming_Language/CollectionTypes .html β€’ https://docs.oracle.com/javase/tutorial/collections/intro/ β€’ http://www.javatpoint.com/collections-in-java

Editor's Notes

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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
  7. 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
  8. 13
  9. 28
  10. 36