SlideShare 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

What's hot

Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and Annotations
Anuj Singh Rajput
 
Java Generics
Java GenericsJava Generics
Java Generics
Zülfikar Karakaya
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work ppt
Ranjith Alappadan
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Edureka!
 
Java Collections
Java  Collections Java  Collections
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
yugandhar vadlamudi
 
Core java
Core javaCore java
Core java
Sun Technlogies
 
Angular interview questions
Angular interview questionsAngular interview questions
Angular interview questions
Goa App
 
Java Collections
Java CollectionsJava Collections
Java Collectionsparag
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
Minal Maniar
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jsp
sandeep54552
 
Sql joins
Sql joinsSql joins
Sql joins
Berkeley
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
Ehsan Hamzei
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
Hammad Rasheed
 
Java lesson khmer
Java lesson khmerJava lesson khmer
Java lesson khmer
Ul Sovanndy
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
Sanjoy Kumar Roy
 

What's hot (20)

Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and Annotations
 
Java Generics
Java GenericsJava Generics
Java Generics
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work ppt
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Generics
GenericsGenerics
Generics
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
 
Core java
Core javaCore java
Core java
 
Angular interview questions
Angular interview questionsAngular interview questions
Angular interview questions
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jsp
 
Sql joins
Sql joinsSql joins
Sql joins
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Composite Design Pattern
Composite Design PatternComposite Design Pattern
Composite Design Pattern
 
Java lesson khmer
Java lesson khmerJava lesson khmer
Java lesson khmer
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
Spring annotation
Spring annotationSpring annotation
Spring annotation
 

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