สอนครั้งที่ 5 cpe4235...
TRANSCRIPT
![Page 1: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/1.jpg)
CPE4235 การวิ�เคราะห์และโปรแกรม
เชิ�งวิ�ตถุ�
จิ�ตกร พิ�ทั�กษ์เมธาก�ลJava Web Developer @Geniustree Co.,Ltd“i am programmer.”
![Page 2: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/2.jpg)
การค�นห์าควิามชิอบของต�วิ เอง
บางคร�!งอาจิเร�"มมาจิากการร#�วิ$าต�วิเองไม$ชิอบอะไร
CPE4235
![Page 3: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/3.jpg)
ทับทัวิน
CPE4235
![Page 4: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/4.jpg)
• Annotation• Java Reflection• Wrapper Class• Enum Type• Unit Test
CPE4235
![Page 5: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/5.jpg)
Thread เบ&!องต�นGeneric
Java Collection
CPE4235
5
![Page 6: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/6.jpg)
เน&!อห์า
• Thread• Generic• Java Collection
CPE4235
generic
collection
thread
![Page 7: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/7.jpg)
CPE4235
Thread (เบ&!องต�น)
![Page 8: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/8.jpg)
CPE4235
ก$อนอ&"น
ในทัาง OS (Operating System) Process ก�บ Thread ต$างก�นอย่$างไร?
![Page 9: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/9.jpg)
CPE4235
Process ค&อกระบวินการของสิ่�"งทั*"เราก+าล�ง focusThread ค&องานเล,กๆ ทั*"เราซอย่ย่$อย่ ออกมาจิาก Processบางคร�!งเร*ย่กวิ$า Lightweight process
ใน 1 กระบวินการ เราสิ่ามารถุทั+างานห์ลาย่ๆ อย่$างพิร�อมก�น ห์ร&อม*งานห์ลาย่งานเก�ดข0!นได� เชิ$น
![Page 10: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/10.jpg)
CPE4235
Process การซ�กผ้�า
จิะประกอบไปด�วิย่งานห์ย่�บย่$อย่ ค&อ ซ�กผ้�า อบผ้�า ตากผ้�า เป2นต�น
![Page 11: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/11.jpg)
CPE4235
ในทัางโปรแกรมม�"ง
Thread ห์มาย่ถุ0ง ต�วิจิ�ดการงาน “ ” (ขนาดเล,ก)
ต�วิทั*"ทั+าห์น�าทั*"ในการด+าเน�นงานตามค+าสิ่�"งทั*"ได�ร�บมอบห์มาย่ให์�แล�วิเสิ่ร,จิ
![Page 12: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/12.jpg)
CPE4235
เชิ$น ต�องการให์�โปรแกรมทั+าอะไรสิ่�กอย่$าง ตามทั*"เราต�องการ
อะไรสิ่�กอย่$างน�!น เราขอเร*ย่กวิ$า งาน “ ”ซ0"งงานจิะไม$ค&บห์น�า ถุ�าไม$ม*ใครห์ร&ออะไรเอางานน�!นไปทั+า
อะไรทั*"เอางานไปทั+า น�"นแห์ล$ะ ทั*"เราเร*ย่กวิ$า Thread
![Page 13: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/13.jpg)
CPE4235
![Page 14: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/14.jpg)
CPE4235
ห์ากเราม*งานห์ลาย่งาน ทั*"ต�องการทั+าให์�เสิ่ร,จิโดย่เร,วิทั*"สิ่�ดเราก,ต�องม*คนงานห์ลาย่คนเพิ&"อทั+างานน�!นๆ พิร�อมก�น
ในทัางคอมพิ�วิเตอร เราเร*ย่กระบบการจิ�ดการงานพิร�อมก�น ห์ร&อรองร�บการทั+างานห์ลาย่ๆ อย่$างพิร�อมก�นวิ$า
Multi-Thread System
![Page 15: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/15.jpg)
CPE4235
Java เป2นภาษ์าโปรแกรมทั*"สิ่ามารถุสิ่�"งงานคอมพิ�วิเตอรด�วิย่ระบบ Multi-Thread
![Page 16: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/16.jpg)
CPE4235
โดย่เราสิ่ามารถุสิ่ร�าง Thread ข0!นมาใชิ�งานเองได� 2 แบบค&อ
1. ใชิ� / extends class Thread ห์ร&อ2. Implements interface Runnable
![Page 17: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/17.jpg)
CPE4235
class Thread
Thread t1 = new Thread(); //ห์ร&อ
Thread t1 = new Thread(“Thread1”);
![Page 18: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/18.jpg)
CPE4235
จิากน�!น Override method run() เพิ&"อบอกให์� Thread ทั+าสิ่�"งทั*"ต�องการ
Thread t1 = new Thread("Thread1") {
@Override public void run() {
System.out.println(“running…”); }};
![Page 19: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/19.jpg)
CPE4235
Thread จิะย่�งไม$ทั+างาน จินกวิ$า เราจิะสิ่�"ง start
Thread t1 = new Thread("Thread1") {
@Override public void run() {
System.out.println(“running…”); }};t1.start(); //running…
![Page 20: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/20.jpg)
CPE4235
Implement interface Runnable
public class RunnableImpl implements Runnable {
@Override public void run() { System.out.println(“runnable running…"); }}
![Page 21: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/21.jpg)
CPE4235
start ผ้$าน Thread
Thread t1 = new Thread(new RunnableImpl(), "Thread1") t1.start(); //runnable running…
![Page 22: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/22.jpg)
CPE4235
ห์ร&อเข*ย่นสิ่�!นๆ เป2น
new Thread(new Runnable() {
@Override public void run() { System.out.println(“runnable running…"); }
} , "Thread1") .start(); //runnable running…
![Page 23: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/23.jpg)
CPE4235
เราควิรสิ่ร�าง Thread ด�วิย่วิ�ธ*ไห์น?
![Page 24: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/24.jpg)
CPE4235
จิร�งๆ แล�วิสิ่ร�าง Thread ด�วิย่วิ�ธ*ใดก,ได�
ทั*"ทั+ามา 2 แบบ เพิราะ Java ไม$อน�ญาตให์� extends class ได�ห์ลาย่ต�วิ จิ0งทั+า interface Runnable มาให์�
![Page 25: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/25.jpg)
CPE4235
ข�อแตกต$างของโปรแกรมทั*"ใชิ� Thread ก�บไม$ใชิ� Thread
![Page 26: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/26.jpg)
CPE4235
ไม$ใชิ� Thread
public class Runner {public void run(){
for(int i =0; i<10; i++){
System.out.println(i);}
}}
![Page 27: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/27.jpg)
CPE4235
ไม$ใชิ� Thread
Runner r1 = new Runner();Runner r2 = new Runner();
r1.run();r2.run();
![Page 28: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/28.jpg)
CPE4235
ไม$ใชิ� Thread
จิะทั+างาน (ประมวิลผ้ล) ตามล+าด�บ
r1.run()
r2.run()
![Page 29: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/29.jpg)
CPE4235
ใชิ� Thread
public class Runner implements Runnable{
public void run(){ for(int i =0; i<10; i++){
System.out.print(i+ “ ”);
} }
}
![Page 30: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/30.jpg)
CPE4235
ใชิ� Thread
Runner r1 = new Runner();Runner r2 = new Runner();
new Thread(r1).start();new Thread(r2).start();
![Page 31: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/31.jpg)
CPE4235
ใชิ� Thread
จิะทั+างาน (ประมวิลผ้ล) สิ่ล�บก�น ทั+าให์�ด#เห์ม&อนทั+างานไปพิร�อมก�น
r1.run()
r2.run()
![Page 32: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/32.jpg)
CPE4235
งาน 1 งาน สิ่ามารถุม* Thread ห์ลาย่ต�วิเข�าใชิ�งานพิร�อมก�นได�
งาน
Thread
Thread
Thread
![Page 33: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/33.jpg)
CPE4235
Multi-Thread
Runner rn = new Runner();
new Thread(rn, “t1”).start();new Thread(rn, “t2”).start();new Thread(rn, “t3”).start();new Thread(rn, “t4”).start();new Thread(rn, “t5”).start();
![Page 34: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/34.jpg)
CPE4235
Multi-Thread
0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 0 1 6 7 8 9 0 1 2 3 4 2 3 4 5 6 7 8 9 9 5 6 7 8 9
ผ้ลทั*"ได�ค&อ ทั+างานสิ่ล�บก�นไปมา
![Page 35: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/35.jpg)
CPE4235
แล�วิถุ�าเราต�องการให์�ม�นทั+างาน ทั*ละ Thread ล$ะ จิะทั+าย่�งไง?
![Page 36: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/36.jpg)
CPE4235
แล�วิถุ�าเราต�องการให์�ม�นทั+างาน ทั*ละ Thread ล$ะ จิะทั+าย่�งไง?
ค+าตอบค&อ
Synchronized
![Page 37: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/37.jpg)
CPE4235
ทั+าไมต�อง Synchronized
เน&"อง Java ทั+างานบนระบบ Multi-Threadใน 1 งานจิ0งสิ่ามารถุม* Thread เข�าใชิ�งานพิร�อมก�นได�ห์ลาย่ต�วิ
![Page 38: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/38.jpg)
CPE4235
แต$ก,ม*งานบางอย่$าง ทั*"เราต�องการให์�ม�นทั+างานทั*ละ 1 Thread เทั$าน�!น เพิ&"อการประมวิลผ้ลทั*"ถุ#กต�อง
![Page 39: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/39.jpg)
CPE4235
เชิ$น การเพิ�"ม ลดค$า
ลองค�ดด#วิ$า ถุ�าม* Thread ห์ลาย่ต�วิเข�าไปเพิ�"มลดค$าข�อม#ลพิร�อมก�น จิะเก�ดอะไรข0!น
1 2 1 2 2 3 2 3 (ไม$เป2นไปตามล+าด�บตามทั*"ควิรจิะเป2น)
![Page 40: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/40.jpg)
CPE4235
Synchronized เป2น keyword ห์น0"งในภาษ์า Javaใชิ�สิ่+าห์ร�บ Block Thread ให์�เข�าใชิ�งานได�ทั*ละต�วิเทั$าน�!น
จินกวิ$า Thread ต�วิก$อนห์น�าจิะทั+างานเสิ่ร,จิThread ต�วิถุ�ดไปจิ0งจิะม*สิ่�ทัธ�5เข�าถุ0ง
![Page 41: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/41.jpg)
CPE4235
Synchronized
รอค�วิ ให์�ต�วิก$อนห์น�าทั+าเสิ่ร,จิก$อน
งานThreadThreadThread
![Page 42: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/42.jpg)
CPE4235
Synchronized
public class Runner implements Runnable{
public synchronized void run(){
for(int i =0; i<10; i++){ System.out.print(i+
“ ”);}
}}
![Page 43: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/43.jpg)
CPE4235
Synchronized
Runner rn = new Runner();
new Thread(rn, “t1”).start();new Thread(rn, “t2”).start();new Thread(rn, “t3”).start();new Thread(rn, “t4”).start();new Thread(rn, “t5”).start();
![Page 44: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/44.jpg)
CPE4235
Synchronized
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
สิ่�งเกตวิ$าทั+างานตามล+าด�บ ค&อ 0 – 9 (Thread t1 ทั+า)0 – 9 (Thread t2 ทั+า)0 – 9 (Thread t3 ทั+า)…
![Page 45: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/45.jpg)
CPE4235
Synchronized ม* 2 แบบ ค&อ
• Synchronized method• Synchronized Block
![Page 46: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/46.jpg)
CPE4235
จิากต�วิอย่$างเม&"อก*6ค&อ Synchronized method เพิราะเราเอา keyword synchronized ไปไวิ�ทั*" method ห์ล�ง access modifier
Synchronized Block จิะม* Syntax ด�งน*!
synchronized( ...sync อะไร... ){
}
![Page 47: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/47.jpg)
CPE4235
Synchronized Block
public class Runner implements Runnable{
public void run(){ synchronized(this) {
for(int i =0; i<10; i++){
System.out.print(i+ “ ”);}
}}
}
![Page 48: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/48.jpg)
CPE4235
Synchronized Block
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
ทั+างานตามล+าด�บ เห์ม&อน Synchronized Method0 – 9 (Thread t1 ทั+า)0 – 9 (Thread t2 ทั+า)0 – 9 (Thread t3 ทั+า)…
![Page 49: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/49.jpg)
CPE4235
แล�วิม�นต$างก�นย่�งไง?
![Page 50: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/50.jpg)
CPE4235
Synchronized Method จิะ sync ทั�!งห์มดทั*"อย่#$ใน method
น�!นๆ
Synchronized Block จิะ sync เฉพิาะทั*"อย่#$ใน block เทั$าน�!น นอก block จิะไม$ sync
![Page 51: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/51.jpg)
CPE4235
Generic
![Page 52: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/52.jpg)
CPE4235
ภาษ์า Java เป2นภาษ์า Static Type ห์ร&อ Type Safe Language
ค&อ จิะต�องทั+าการก+าห์นดชิน�ดข�อม#ลให์�ก�บต�วิแปรก$อนทั*"จิะน+าไปใชิ�งานเสิ่มอ
![Page 53: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/53.jpg)
CPE4235
แต$ม�นก,ม*ป8ญห์าบางป8ญห์า ห์ร&อ เห์ต�การณ์บางเห์ต�การณ์ทั*"ข�ดก�บห์ล�กการข�อน*!
![Page 54: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/54.jpg)
CPE4235
ทั�ก class ในภาษ์า Java ม* class java.lang.Object เป2น parent ของ class ทั�!งห์มด
ถุ�าม*การประกาศต�วิแปร Object obj ไวิ� ต�วิแปรน*!สิ่ามารถุรองร�บข�อม#ลของ class ใดๆ ก,ได�
เพิราะทั�ก class ถุ&อเป2น Object
![Page 55: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/55.jpg)
CPE4235
public void add(Object obj){//…
}
add(Object obj) สิ่$ง class อะไรเข�าไปได�บ�า ง ?
![Page 56: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/56.jpg)
CPE4235
public void add(Object obj){//…
}
add(Object obj) สิ่$ง class อะไรเข�าไปได�บ�า ง ?- String- Integer- Float- …
![Page 57: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/57.jpg)
CPE4235
ค��นๆ ม�!ย่ ห์ล�กการข�อน*!
![Page 58: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/58.jpg)
CPE4235
Polymorphism ไง
![Page 59: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/59.jpg)
CPE4235
List list = new ArrayList(); //เก,บข�อม#ลทั*"เป2น Object
list.add(“1”); //String เป2น Reference Data Typelist.add(“2”);
Integer number = (Integer)list.get(0); //Integer เป2น Wrapper class
Compile ผ้$านม�!ย่ ?
![Page 60: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/60.jpg)
CPE4235
List list = new ArrayList();
list.add(“1”);list.add(“2”);
Integer number = (Integer)list.get(0);
Compile ผ้$าน เพิราะ Syntax ไม$ผ้�ด แต$ Run ไม$ผ้$าน เพิราะเก�ด java.lang.ClassCastException
![Page 61: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/61.jpg)
CPE4235
แล�วิเราจิะแก�ป8ญห์าน*!ย่�งไง
![Page 62: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/62.jpg)
CPE4235
Generic
• เป2นค�ณ์สิ่มบ�ต�ห์น0"งทั*"ถุ#กเพิ�"มเข�ามาใน java 5 ห์ร&อ jdk 1.5
• เป2นค�ณ์สิ่มบ�ต�ทั*"เอาไวิ�ใชิ�สิ่+าห์ร�บตรวิจิสิ่อบควิามถุ#กต�องของชิน�ดข�อม#ลในขณ์ะทั*"ทั+าการ Compile โปรแกรม
![Page 63: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/63.jpg)
CPE4235
เราคงจิะเคย่เห์,นสิ่�ญล�กษ์ณ์<> ในภาษ์า java เชิ$น
List<String> list = new ArrayList<>();Map<String, Integer> param = new HashMap<>();
<> ค&อสิ่�ญล�กษ์ณ์ทั*"บ$งบอกวิ$าเป2น Generic
![Page 64: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/64.jpg)
CPE4235
ต�วิอย่$าง
List<String> list = new ArrayList<>();
ห์มาย่ควิามวิ$า list น*!รองร�บการเก,บข�อม#ลทั*"เป2น String เทั$าน�!น
![Page 65: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/65.jpg)
CPE4235
ต�วิอย่$าง
List<String> list = new ArrayList<>();
Generic จิะตรวิจิสิ่อบตอน Compile Timeเราไม$สิ่ามารถุสิ่$งต�วิเลข เข�าไปใน list ได� เพิราะจิะทั+าให์� Compile ไม$ผ้$าน
![Page 66: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/66.jpg)
CPE4235
Generic class เร*ย่กอ*กอย่$างห์น0"งวิ$า Parameterized Class ห์มาย่ถุ0ง class ทั*"สิ่ามารถุม* Parameter ได�
โดย่ Parameter ของ Generic เราจิะเร*ย่กวิ$า Type Parameter
![Page 67: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/67.jpg)
CPE4235
public class FirstGenericClass<T> {
}
ห์ร&อ ม*ได�ห์ลาย่ต�วิ
public class FirstGenericClass<T, K, S, U> {
}
Type Parameter
![Page 68: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/68.jpg)
CPE4235
public class FirstGenericClass<T> {
private T member;
public FirstGenericClass(T member){
this.member = member;}
public T getMember(){ return member; }}
![Page 69: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/69.jpg)
CPE4235
FirstGenericClass<String> gen1;gen1 = new FirstGenericClass<>(“Hello”);
//ถุ�าใสิ่$แบบน*! FirstGenericClass<String> gen1;gen1 = new FirstGenericClass<>(50);
//จิะ Compile ไม$ผ้$าน
![Page 70: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/70.jpg)
CPE4235
FirstGenericClass<String> gen1;gen1 = new FirstGenericClass<>(“Hello”);
System.out.println(gen.getMember().getClass().getName());//java.lang.String
![Page 71: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/71.jpg)
CPE4235
FirstGenericClass<Integer> gen1;gen1 = new FirstGenericClass<>(100);
System.out.println(gen.getMember().getClass().getName());//java.lang.Integer
![Page 72: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/72.jpg)
CPE4235
Type Parameter <T>
จิะม*ก*"ต�วิ ก*" Type ก,ได� แต$ละต�วิ ค�"นด�วิย่สิ่�ญล�กษ์ณ์ , (Comma) เชิ$น <T, S, E>
น�ย่มใชิ�ต�วิอ�กษ์รภาษ์าอ�งกฤษ์ต�วิให์ญ$
![Page 73: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/73.jpg)
CPE4235
Generic Method
ในบางคร�!งเราอาจิต�องการใชิ�งาน Generic ในขอบเขตของ method เทั$าน�!น ไม$ต�องการใชิ�งานใน scope ของ class
สิ่ามารถุใชิ�งานได�ด�งน*!
![Page 74: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/74.jpg)
CPE4235
public class SecondGenericClass {
public static <T> T fromInput(T input) { return input; }
}
ประกาศ <T> ไวิ�ห์น�า method เลย่แทันการประกาศไวิ�ทั*" class
![Page 75: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/75.jpg)
CPE4235
String input 1= SecondGenericClass.fromInput(“Hello”);Integer input2 = SecondGenericClass.fromInput(2);Float input 3= SecondGenericClass.fromInput(5.0F);Long input4 = SecondGenericClass.fromInput(10L);Object input5 = SecondGenericClass.fromInput(new Object());
![Page 76: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/76.jpg)
CPE4235
Bounded Type
เป2นการก+าห์นดขอบเขตของ Type Parameter วิ$าอน�ญาตให์�เป2น Type ใดได�บ�าง
![Page 77: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/77.jpg)
CPE4235
ซ0"งโดย่ปกต�แล�วิการใชิ� <T> จิะไม$ก+าห์นดขอบเขต ค&อสิ่ามารถุใสิ่$ข�อม#ลอะไรเข�ามาใน T ก,ได�
![Page 78: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/78.jpg)
CPE4235
public class SecondGenericClass {
public static <T extends Number> T fromInput(T input) { return input; }
}
T extends Number – T เป2นอะไรก,ได� แต$ต�องเป2น Number
![Page 79: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/79.jpg)
CPE4235
String input 1= SecondGenericClass.fromInput(“Hello”);Integer input2 = SecondGenericClass.fromInput(2);Float input 3= SecondGenericClass.fromInput(5.0F);Long input4 = SecondGenericClass.fromInput(10L);Object input5 = SecondGenericClass.fromInput(new Object());
Compile ไม$ผ้$าน Error 2 ต�วิ เพิราะString ก�บ Object ไม$ใชิ$ Number (ไม$ได� extends Number)
![Page 80: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/80.jpg)
CPE4235
Java Collection
![Page 81: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/81.jpg)
CPE4235
Java Collection
• เป2นเร&"องของการจิ�ดเก,บข�อม#ลเป2นชิ�ด
![Page 82: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/82.jpg)
CPE4235
Java Collection
• เป2นเร&"องของการจิ�ดเก,บข�อม#ลโดย่ใชิ� โครงสิ่ร�างข�อม#ล (Data Structure)
ซ0"งถุ#กออกแบบมาเพิ&"อเพิ�"มประสิ่�ทัธ�ภาพิในการจิ�ดเก,บและการเข�าถุ0ง โครงสิ่ร�างข�อม#ลม*ห์ลาย่แบบ ข0!นอย่#$ก�บวิ$าเราจิะเล&อกใชิ�โครงสิ่ร�างข�อม#ลแบบใด
![Page 83: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/83.jpg)
CPE4235
Collection API ห์ร&อ Collection Interface
เป2น spec ห์ร&อข�อก+าห์นดของ Collection ในแต$ละแบบ วิ$า จิะใชิ� Collection น�!นเก,บข�อม#ลล�กษ์ณ์ะไห์น ม*โครงสิ่ร�างข�อม#ลเป2นแบบใด
![Page 84: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/84.jpg)
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
![Page 85: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/85.jpg)
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
![Page 86: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/86.jpg)
CPE4235
Iterable
เป2น interface สิ่#งสิ่�ดของเร&"องน*! เพิ&"อให์� interface อ&"นๆ extends ไป
เป2น interface ทั*"ใชิ�สิ่+าห์ร�บวินรอบ เพิ&"อด0งข�อม#ลสิ่มาชิ�กของ Collection ออกมาทั*ละต�วิ ด�วิย่ Iterator design pattern
![Page 87: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/87.jpg)
CPE4235
package java.lang;import java.util.Iterator;
public interface Iterable<T> {
Iterator<T> iterator();}
![Page 88: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/88.jpg)
CPE4235
package java.util;
public interface Iterator<E> {
boolean hasNext(); //ม*สิ่มาชิ�กต�วิถุ�ดไปห์ร&อไม$ E next(); //ค&นค$า สิ่มาชิ�กต�วิถุ�ดไป void remove(); //ลบสิ่มาชิ�กป8จิจิ�บ�น}
![Page 89: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/89.jpg)
CPE4235
…Interater itr = collection.iterator();while(itr.hasNext()){
int member = itr.next();if(member < 10){
itr.remove();}
}
![Page 90: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/90.jpg)
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
![Page 91: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/91.jpg)
CPE4235
Collection
• เป2น Core interface ของเร&"อง Collection
• เป2น spec ทั*"คอย่ก+าห์นดวิ$า Collection ควิรม*พิฤต�กรรม ห์ร&อ method อะไรบ�าง
เชิ$น
![Page 92: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/92.jpg)
CPE4235
• size() – ค&นค$าขนาดสิ่มาชิ�กของ Collection
• isEmpty() – ตรวิจิสิ่อบวิ$า Collection น�!นเป2นค$าวิ$างห์ร&อไม$
• contains(Object o) – ตรวิจิสิ่อบวิ$า input น�!นเป2นสิ่มาชิ�กของ Collection ห์ร&อไม$
• iterator() – วินรอบ เพิ&"อด0งค$าสิ่มาชิ�ก ออกมาทั*ละต�วิ
• toArray() – convert สิ่มาชิ�กไปเป2น Array
• add(E e) – เพิ�"มสิ่มาชิ�กลงใน Collection
• remove(Object o) – ลบสิ่มาชิ�กออกจิาก Collection
• ... เป2นต�น
![Page 93: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/93.jpg)
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
![Page 94: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/94.jpg)
CPE4235
List
เป2น interface ห์ร&อ spec ทั*"คอย่ก+าห์นดวิ$า implementation อะไรก,ตามของ interface น*!
จิะม*ล�กษ์ณ์ะ (โครงสิ่ร�าง) การจิ�ดเก,บข�อม#ลเป2นล+าด�บและใชิ� index ในการเข�าถุ0ง (ม*เร&"องของ index มาเก*"ย่วิข�อง)
![Page 95: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/95.jpg)
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
![Page 96: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/96.jpg)
CPE4235
Set
เป2น interface ห์ร&อ spec ทั*"คอย่ก+าห์นดวิ$า implementation อะไรก,ตามของ interface น*!
จิะม*ล�กษ์ณ์ะ (โครงสิ่ร�าง) การจิ�ดเก,บข�อม#ลทั*"ไม$ซ+!าก�น
![Page 97: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/97.jpg)
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
![Page 98: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/98.jpg)
CPE4235
Queue
เป2น interface ห์ร&อ spec ทั*"คอย่ก+าห์นดวิ$า implementation อะไรก,ตามของ interface น*!
จิะม*ล�กษ์ณ์ะ (โครงสิ่ร�าง) การจิ�ดเก,บข�อม#ลเป2นล+าด�บ
![Page 99: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/99.jpg)
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
![Page 100: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/100.jpg)
CPE4235
Map
เป2น interface ห์ร&อ spec ทั*"คอย่ก+าห์นดวิ$า implementation อะไรก,ตามของ interface น*!
จิะม*ล�กษ์ณ์ะ (โครงสิ่ร�าง) การจิ�ดเก,บข�อม#ลทั*"เป2นค#$ของ key/value
![Page 101: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/101.jpg)
CPE4235
สิ่�งเกตวิ$า Map ไม$ได� implements Collection
![Page 102: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/102.jpg)
CPE4235
สิ่ร�ป Collection API (Collection Interface)
List = ล+าด�บ + ม* indexSet = ไม$ซ+!าก�นQueue = ล+าด�บ ก$อนห์ล�งMap = เก,บค#$ key / value
![Page 103: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/103.jpg)
CPE4235
Collection Implementation
![Page 104: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/104.jpg)
CPE4235
Collection implementation
ค&อ class ทั*" implements interface ของ Collection แต$ละแบบ เชิ$น
• List – ArrayList, LinkedList, Vector, Stack
• Set – HashSet, LinkedHashSet • Queue – LinkedList,
PriorityQueue• Map – HashMap, Hashtable,
LinkedHashMap, Properties
![Page 105: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/105.jpg)
CPE4235
List
ArrayList
LinkedList Stack
Collection Interface & Collection Implementation
implements
implements
interface
class classclass
Vector
implements
class
implements
![Page 106: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/106.jpg)
CPE4235
List
ArrayList
LinkedList Stack
Collection Interface & Collection Implementation
implements
implements
interface
class classclass
Vector
implements
class
implements
![Page 107: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/107.jpg)
CPE4235
• ArrayList – จิ�ดเก,บข�อม#ลเป2น Array • ข�อด* ค&อม*ควิามเร,วิในการเข�าถุ0งข�อม#ล
ด�วิย่ index : O(1) • ข�อเสิ่*ย่ ค&อควิามเร,วิในการเพิ�"มข�อม#ลทั*"
ต�องจิอง memory ให์ม$เสิ่มอ (เพิ&"อขย่าย่ขนาด) และควิามเร,วิในการค�นห์าทั*"ต�องไล$ห์าสิ่มาชิ�กทั*ละต�วิ ด�วิย่การ loop : O(n)
![Page 108: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/108.jpg)
CPE4235
List<Integer> list = new ArrayList<>();list.add(2);list.add(5);list.add(8);
System.out.println(list.get(0)); //2 System.out.println(list.get(1)); //5 System.out.println(list.get(2)); //8
![Page 109: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/109.jpg)
CPE4235
List
ArrayList
LinkedList Stack
Collection Interface & Collection Implementation
implements
implements
interface
class classclass
Vector
implements
class
implements
![Page 110: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/110.jpg)
CPE4235
• LinkedList – จิ�ดเก,บข�อม#ลด�วิย่โครงสิ่ร�างข�อม#ล LinkedList• ข�อด* ค&อเพิ�"มข�อม#ล ลบข�อม#ล และจิ�ดเก,บ
ข�อม#ลได�เร,วิ : O(1)• ข�อเสิ่*ย่ ค&อควิามเร,วิในการค�นห์า ทั*"ต�องไล$
ห์าทั*ละต�วิ : O(n)
![Page 111: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/111.jpg)
CPE4235
List<Integer> list = new LinkedList<>();list.add(2);list.add(5);list.add(8);
System.out.println(list.get(0)); //2 System.out.println(list.get(1)); //5 System.out.println(list.get(2)); //8
![Page 112: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/112.jpg)
CPE4235
List
ArrayList
LinkedList Stack
Collection Interface & Collection Implementation
implements
implements
interface
class classclass
Vector
implements
class
implements
![Page 113: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/113.jpg)
CPE4235
• Vector – จิ�ดเก,บข�อม#ลด�วิย่ Array เห์ม&อน ArrayList แต$เป2นSynchronized ค&อจิะอน�ญาตให์� Thread เข�าใชิ�งานได�ทั*ละต�วิเทั$าน�!น Thread ต�วิอ&"นจิะถุ#ก Block จินกวิ$า Thread ต�วิก$อนห์น�าจิะทั+างานเสิ่ร,จิ ถุ0งจิะม*สิ่�ทัธ�5เข�าใชิ�งานได� เพิ&"อควิามถุ#กต�องของข�อม#ล
![Page 114: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/114.jpg)
CPE4235
List
ArrayList
LinkedList Stack
Collection Interface & Collection Implementation
implements
implements
interface
class classclass
Vector
implements
class
implements
![Page 115: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/115.jpg)
CPE4235
• Stack – จิ�ดเก,บข�อม#ลด�วิย่โครงสิ่ร�างข�อม#ล Stack ค&อ First In Last Out (FILO) เข�าก$อนออกห์ล�ง
สิ่มาชิ�กต�วิไห์นทั*"ถุ#กน+าเข�ามาใน stack ก$อนจิะสิ่ามารถุเอาออกได�ทั*ห์ล�ง
![Page 116: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/116.jpg)
CPE4235
Stack<Integer> stk = new Stack<>();stk.add(2);stk.add(5);stk.add(8);
System.out.println(stk.get(1)); //5
System.out.println(stk.pop()); //8 เข�าห์ล�ง ออกก$อนSystem.out.println(stk.pop()); //5System.out.println(stk.pop()); //2 เข�าก$อน ออกห์ล�งสิ่�ด
![Page 117: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/117.jpg)
CPE4235
Set
HashSet
Collection Interface & Collection Implementation
implements
interface
class LinkedHashSet
implements
class
...
![Page 118: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/118.jpg)
CPE4235
Set
HashSet
Collection Interface & Collection Implementation
implements
interface
class LinkedHashSet
implements
class
...
![Page 119: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/119.jpg)
CPE4235
• HashSet – จิ�ดเก,บข�อม#ลด�วิย่วิ�ธ* Set ค&อข�อม#ลห์ร&อสิ่มาชิ�กใน Collection น*!ซ+!าก�นไม$ได� และใชิ� Hashing ในการจิ�ดเก,บและเข�าถุ0งข�อม#ลให์�เร,วิข0!น ล+าด�บการน+าเข�าไม$ม*ผ้ลใน Collection น*! (ไม$เร*ย่งตามล+าด�บ)
จิ+า method hashCode() ได�ม�!ย่ ใชิ�ก�บต�วิน*!แห์ล$ะ
![Page 120: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/120.jpg)
CPE4235
Set<Integer> set = new HashSet<>();set.add(2); //1set.add(4); //2set.add(6); //3set.add(7); //4set.add(2); //5set.add(6); //6 System.out.println(set.size()); //4
![Page 121: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/121.jpg)
CPE4235
for(Integer number : set){System.out.println(number +
“ ”); }
6 2 7 4 //เห์ล&อ 4 ต�วิ + ไม$เป2นไปตามล+าด�บ
![Page 122: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/122.jpg)
CPE4235
Set
HashSet
Collection Interface & Collection Implementation
implements
interface
class LinkedHashSet
implements
class
...
![Page 123: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/123.jpg)
CPE4235
• LinkedHashSet – ม*พิฤต�กรรมเป2น HashSet แต$ม*ล+าด�บการน+าเข�า
เป2นการผ้นวิกควิามสิ่ามารถุของ LinkedList + HashSet
![Page 124: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/124.jpg)
CPE4235
Set<Integer> set = new LinkedHashSet<>();set.add(2); //1set.add(4); //2set.add(6); //3set.add(7); //4set.add(2); //5set.add(6); //6 System.out.println(set.size()); //4
![Page 125: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/125.jpg)
CPE4235
for(Integer number : set){System.out.println(number +
“ ”); }
2 4 6 7 //เห์ล&อ 4 ต�วิ + เป2นไปตามล+าด�บ
![Page 126: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/126.jpg)
CPE4235
Queue
PriorityQueue
Collection Interface & Collection Implementation
implements
interface
class
LinkedList
implements
class
...
![Page 127: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/127.jpg)
CPE4235
Queue
PriorityQueue
Collection Interface & Collection Implementation
implements
interface
class
LinkedList
implements
class
...
![Page 128: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/128.jpg)
CPE4235
• PriorityQueue - เป2น Queue ประเภทัห์น0"ง ทั*"เราสิ่ามารถุเข�าถุ0งสิ่มาชิ�ก ได�ตามล+าด�บควิามสิ่+าค�ญของสิ่มาชิ�กต�วิน�!น
![Page 129: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/129.jpg)
CPE4235
Queue
PriorityQueue
Collection Interface & Collection Implementation
implements
interface
class
LinkedList
implements
class
...
![Page 130: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/130.jpg)
CPE4235
• LinkedList - ก,ถุ&อเป2น Queue ต�วิห์น0"ง เพิราะม*พิฤต�กรรม First In First Out (FIFO)
![Page 131: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/131.jpg)
CPE4235
Queue<Integer> q = new LinkedList<>();q.add(2);q.add(5);q.add(8);
System.out.println(q.poll()); //2 System.out.println(q.poll()); //5 System.out.println(q.poll()); //8
![Page 132: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/132.jpg)
CPE4235
Map
HashMap
HashTable
Properties
Collection Interface & Collection Implementation
implements
implements
interface
class classclass LinkedHashMap
implements
class
implements
![Page 133: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/133.jpg)
CPE4235
Map
HashMap
HashTable
Properties
Collection Interface & Collection Implementation
implements
implements
interface
class classclass LinkedHashMap
implements
class
implements
![Page 134: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/134.jpg)
CPE4235
• HashMap – เป2น Map ประเภทัห์น0"ง จิ�ดเก,บข�อม#ล เป2น key / value ด�วิย่วิ�ธ* Hashing ทั*"ได�จิาก method hashCode() ของสิ่มาชิ�ก
![Page 135: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/135.jpg)
CPE4235
HashMap
เป2น Unsynchronized ค&อย่อมให์� Thread ห์ลาย่ๆ ต�วิเข�าถุ0งข�อม#ลได�โดย่ไม$เก�ดการ Block Thread
ข�อด*ค&อควิามเร,วิในการเข�าถุ0ง สิ่$วินข�อเสิ่*ย่ ค&อเร&"องของควิามถุ#กต�องของข�อม#ล เพิราะอน�ญาตให์� Thread ห์ลาย่ต�วิ access ข�อม#ลได�พิร�อมก�น
![Page 136: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/136.jpg)
CPE4235
Map<String, Integer> map = new HashMap<>();map.put("Monday", 1);map.put("Friday", 5);map.put("Sunday", 7); System.out.println(map.get("Monday")); //1System.out.println(map.get("Friday")); //5System.out.println(map.get("Sunday")); //7
![Page 137: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/137.jpg)
CPE4235
Map
HashMap
HashTable
Properties
Collection Interface & Collection Implementation
implements
implements
interface
class classclass LinkedHashMap
implements
class
implements
![Page 138: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/138.jpg)
CPE4235
• HashTable – คล�าย่ๆ HashMap แต$เป2น Synchronized ค&ออน�ญาตให์� Thread เข�าใชิ�งานได�ทั*ละต�วิเทั$าน�!น
จิะ Block Thread ต�วิป8จิจิ�บ�น จินกวิ$า Thread ต�วิก$อนห์น�าจิะทั+างานเสิ่ร,จิ
![Page 139: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/139.jpg)
CPE4235
Map
HashMap
HashTable
Properties
Collection Interface & Collection Implementation
implements
implements
interface
class classclass LinkedHashMap
implements
class
implements
![Page 140: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/140.jpg)
CPE4235
• LinkedHashMap – เป2น Map ทั*"ม*ล+าด�บการน+าเข�าข�อม#ล ซ0"งโดย่ปกต�แล�วิ Map จิะไม$ม*ล+าด�บการน+าเข�า
![Page 141: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/141.jpg)
CPE4235
Map
HashMap
HashTable
Properties
Collection Interface & Collection Implementation
implements
implements
interface
class classclass LinkedHashMap
implements
class
implements
![Page 142: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/142.jpg)
CPE4235
• Properties – เป2น Map เฉพิาะประเภทัน0งทั*"เอาไวิ�เก,บค$า Key / Value ของ Properties File (.properties) extends มาจิาก HashTable เป2น Synchronized
![Page 143: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/143.jpg)
CPE4235
![Page 144: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/144.jpg)
CPE4235
การเล&อก Collection ไปใชิ�งาน
ข0!นอย่#$ก�บวิ�ตถุ�ประสิ่งคทั*"เราจิะใชิ� และควิามห์มาะสิ่มของการจิ�ดเก,บข�อม#ล โดย่การด#ทั*" Big O วิ$าอ�นไห์นม* Big O น�อย่ทั*"สิ่�ด ในวิ�ตถุ�ประสิ่งคทั*"เราจิะน+าไปใชิ�
![Page 145: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/145.jpg)
CPE4235
เชิ$น
ต�องการจิ�ดเก,บข�อม#ลทั*"ไม$ซ+!า + access เร,วิ ก,ใชิ� HashSetต�องการจิ�ดเก,บข�อม#ลเป2นล+าด�บ + access เร,วิ ก,ใชิ� ArrayListต�องการจิ�ดเก,บข�อม#ลเป2นค#$ key / value + เร,วิ ก,ใชิ� HashMapต�องการจิ�ดเก,บข�อม#ลไม$ซ+!า + เป2นล+าด�บ ก,ใชิ� LinkedHashSet
เป2นต�น
![Page 146: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/146.jpg)
CPE4235 List
Map
Set
Queue
![Page 147: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/147.jpg)
CPE4235
การบ�าน (10 คะแนน)
ให์�ทั+าราย่งานเร&"อง Java Collection ห์ร&อ Thread อย่$างใดอย่$างห์น0"ง
![Page 148: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/148.jpg)
CPE4235
Java Collection
ม*ก*"ประเภทั อะไรบ�าง (มากกวิ$าทั*"ม*อย่#$ใน slide) พิร�อมอธ�บาย่ ประโย่ชิน ข�อด* ข�อเสิ่*ย่ การเล&อกใชิ� Collection วิ$าด#จิากอะไร + ม*ต�วิอย่$าง code ประกอบจิะด*มาก
![Page 149: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/149.jpg)
CPE4235
Thread
อธ�บาย่ควิามเป2นมา ป8ญห์า แนวิค�ด การแก�ป8ญา (synchronized) ต�วิอย่$างการใชิ�งาน + วิงจิรชิ*วิ�ตของ Thread
![Page 150: สอนครั้งที่ 5 cpe4235 (การวิเคราะห์และโปรแกรมเชิงวัตถุ) / 2557](https://reader036.vdocuments.mx/reader036/viewer/2022081504/55c30f7cbb61ebee738b481d/html5/thumbnails/150.jpg)
CPE4235
เน&!อห์าสิ่ร�ปรวิบร�ด อ$านเข�าใจิ ไม$มากจินเก�นไป ห์ร&อน�อย่จินเก�นไป ตามควิามห์มาะสิ่ม (สิ่ร�ป)
สิ่$งวิ�นไห์นด* ? ก$อนสิ่อบกลางภาค