สอนครั้งที่ 5 cpe4235...
TRANSCRIPT
CPE4235 การวิ�เคราะห์และโปรแกรม
เชิ�งวิ�ตถุ�
จิ�ตกร พิ�ทั�กษ์เมธาก�ลJava Web Developer @Geniustree Co.,Ltd“i am programmer.”
การค�นห์าควิามชิอบของต�วิ เอง
บางคร�!งอาจิเร�"มมาจิากการร#�วิ$าต�วิเองไม$ชิอบอะไร
CPE4235
ทับทัวิน
CPE4235
• Annotation• Java Reflection• Wrapper Class• Enum Type• Unit Test
CPE4235
Thread เบ&!องต�นGeneric
Java Collection
CPE4235
5
เน&!อห์า
• Thread• Generic• Java Collection
CPE4235
generic
collection
thread
CPE4235
Thread (เบ&!องต�น)
CPE4235
ก$อนอ&"น
ในทัาง OS (Operating System) Process ก�บ Thread ต$างก�นอย่$างไร?
CPE4235
Process ค&อกระบวินการของสิ่�"งทั*"เราก+าล�ง focusThread ค&องานเล,กๆ ทั*"เราซอย่ย่$อย่ ออกมาจิาก Processบางคร�!งเร*ย่กวิ$า Lightweight process
ใน 1 กระบวินการ เราสิ่ามารถุทั+างานห์ลาย่ๆ อย่$างพิร�อมก�น ห์ร&อม*งานห์ลาย่งานเก�ดข0!นได� เชิ$น
CPE4235
Process การซ�กผ้�า
จิะประกอบไปด�วิย่งานห์ย่�บย่$อย่ ค&อ ซ�กผ้�า อบผ้�า ตากผ้�า เป2นต�น
CPE4235
ในทัางโปรแกรมม�"ง
Thread ห์มาย่ถุ0ง ต�วิจิ�ดการงาน “ ” (ขนาดเล,ก)
ต�วิทั*"ทั+าห์น�าทั*"ในการด+าเน�นงานตามค+าสิ่�"งทั*"ได�ร�บมอบห์มาย่ให์�แล�วิเสิ่ร,จิ
CPE4235
เชิ$น ต�องการให์�โปรแกรมทั+าอะไรสิ่�กอย่$าง ตามทั*"เราต�องการ
อะไรสิ่�กอย่$างน�!น เราขอเร*ย่กวิ$า งาน “ ”ซ0"งงานจิะไม$ค&บห์น�า ถุ�าไม$ม*ใครห์ร&ออะไรเอางานน�!นไปทั+า
อะไรทั*"เอางานไปทั+า น�"นแห์ล$ะ ทั*"เราเร*ย่กวิ$า Thread
CPE4235
CPE4235
ห์ากเราม*งานห์ลาย่งาน ทั*"ต�องการทั+าให์�เสิ่ร,จิโดย่เร,วิทั*"สิ่�ดเราก,ต�องม*คนงานห์ลาย่คนเพิ&"อทั+างานน�!นๆ พิร�อมก�น
ในทัางคอมพิ�วิเตอร เราเร*ย่กระบบการจิ�ดการงานพิร�อมก�น ห์ร&อรองร�บการทั+างานห์ลาย่ๆ อย่$างพิร�อมก�นวิ$า
Multi-Thread System
CPE4235
Java เป2นภาษ์าโปรแกรมทั*"สิ่ามารถุสิ่�"งงานคอมพิ�วิเตอรด�วิย่ระบบ Multi-Thread
CPE4235
โดย่เราสิ่ามารถุสิ่ร�าง Thread ข0!นมาใชิ�งานเองได� 2 แบบค&อ
1. ใชิ� / extends class Thread ห์ร&อ2. Implements interface Runnable
CPE4235
class Thread
Thread t1 = new Thread(); //ห์ร&อ
Thread t1 = new Thread(“Thread1”);
CPE4235
จิากน�!น Override method run() เพิ&"อบอกให์� Thread ทั+าสิ่�"งทั*"ต�องการ
Thread t1 = new Thread("Thread1") {
@Override public void run() {
System.out.println(“running…”); }};
CPE4235
Thread จิะย่�งไม$ทั+างาน จินกวิ$า เราจิะสิ่�"ง start
Thread t1 = new Thread("Thread1") {
@Override public void run() {
System.out.println(“running…”); }};t1.start(); //running…
CPE4235
Implement interface Runnable
public class RunnableImpl implements Runnable {
@Override public void run() { System.out.println(“runnable running…"); }}
CPE4235
start ผ้$าน Thread
Thread t1 = new Thread(new RunnableImpl(), "Thread1") t1.start(); //runnable running…
CPE4235
ห์ร&อเข*ย่นสิ่�!นๆ เป2น
new Thread(new Runnable() {
@Override public void run() { System.out.println(“runnable running…"); }
} , "Thread1") .start(); //runnable running…
CPE4235
เราควิรสิ่ร�าง Thread ด�วิย่วิ�ธ*ไห์น?
CPE4235
จิร�งๆ แล�วิสิ่ร�าง Thread ด�วิย่วิ�ธ*ใดก,ได�
ทั*"ทั+ามา 2 แบบ เพิราะ Java ไม$อน�ญาตให์� extends class ได�ห์ลาย่ต�วิ จิ0งทั+า interface Runnable มาให์�
CPE4235
ข�อแตกต$างของโปรแกรมทั*"ใชิ� Thread ก�บไม$ใชิ� Thread
CPE4235
ไม$ใชิ� Thread
public class Runner {public void run(){
for(int i =0; i<10; i++){
System.out.println(i);}
}}
CPE4235
ไม$ใชิ� Thread
Runner r1 = new Runner();Runner r2 = new Runner();
r1.run();r2.run();
CPE4235
ไม$ใชิ� Thread
จิะทั+างาน (ประมวิลผ้ล) ตามล+าด�บ
r1.run()
r2.run()
CPE4235
ใชิ� Thread
public class Runner implements Runnable{
public void run(){ for(int i =0; i<10; i++){
System.out.print(i+ “ ”);
} }
}
CPE4235
ใชิ� Thread
Runner r1 = new Runner();Runner r2 = new Runner();
new Thread(r1).start();new Thread(r2).start();
CPE4235
ใชิ� Thread
จิะทั+างาน (ประมวิลผ้ล) สิ่ล�บก�น ทั+าให์�ด#เห์ม&อนทั+างานไปพิร�อมก�น
r1.run()
r2.run()
CPE4235
งาน 1 งาน สิ่ามารถุม* Thread ห์ลาย่ต�วิเข�าใชิ�งานพิร�อมก�นได�
งาน
Thread
Thread
Thread
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();
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
ผ้ลทั*"ได�ค&อ ทั+างานสิ่ล�บก�นไปมา
CPE4235
แล�วิถุ�าเราต�องการให์�ม�นทั+างาน ทั*ละ Thread ล$ะ จิะทั+าย่�งไง?
CPE4235
แล�วิถุ�าเราต�องการให์�ม�นทั+างาน ทั*ละ Thread ล$ะ จิะทั+าย่�งไง?
ค+าตอบค&อ
Synchronized
CPE4235
ทั+าไมต�อง Synchronized
เน&"อง Java ทั+างานบนระบบ Multi-Threadใน 1 งานจิ0งสิ่ามารถุม* Thread เข�าใชิ�งานพิร�อมก�นได�ห์ลาย่ต�วิ
CPE4235
แต$ก,ม*งานบางอย่$าง ทั*"เราต�องการให์�ม�นทั+างานทั*ละ 1 Thread เทั$าน�!น เพิ&"อการประมวิลผ้ลทั*"ถุ#กต�อง
CPE4235
เชิ$น การเพิ�"ม ลดค$า
ลองค�ดด#วิ$า ถุ�าม* Thread ห์ลาย่ต�วิเข�าไปเพิ�"มลดค$าข�อม#ลพิร�อมก�น จิะเก�ดอะไรข0!น
1 2 1 2 2 3 2 3 (ไม$เป2นไปตามล+าด�บตามทั*"ควิรจิะเป2น)
CPE4235
Synchronized เป2น keyword ห์น0"งในภาษ์า Javaใชิ�สิ่+าห์ร�บ Block Thread ให์�เข�าใชิ�งานได�ทั*ละต�วิเทั$าน�!น
จินกวิ$า Thread ต�วิก$อนห์น�าจิะทั+างานเสิ่ร,จิThread ต�วิถุ�ดไปจิ0งจิะม*สิ่�ทัธ�5เข�าถุ0ง
CPE4235
Synchronized
รอค�วิ ให์�ต�วิก$อนห์น�าทั+าเสิ่ร,จิก$อน
งานThreadThreadThread
CPE4235
Synchronized
public class Runner implements Runnable{
public synchronized void run(){
for(int i =0; i<10; i++){ System.out.print(i+
“ ”);}
}}
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();
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 ทั+า)…
CPE4235
Synchronized ม* 2 แบบ ค&อ
• Synchronized method• Synchronized Block
CPE4235
จิากต�วิอย่$างเม&"อก*6ค&อ Synchronized method เพิราะเราเอา keyword synchronized ไปไวิ�ทั*" method ห์ล�ง access modifier
Synchronized Block จิะม* Syntax ด�งน*!
synchronized( ...sync อะไร... ){
}
CPE4235
Synchronized Block
public class Runner implements Runnable{
public void run(){ synchronized(this) {
for(int i =0; i<10; i++){
System.out.print(i+ “ ”);}
}}
}
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 ทั+า)…
CPE4235
แล�วิม�นต$างก�นย่�งไง?
CPE4235
Synchronized Method จิะ sync ทั�!งห์มดทั*"อย่#$ใน method
น�!นๆ
Synchronized Block จิะ sync เฉพิาะทั*"อย่#$ใน block เทั$าน�!น นอก block จิะไม$ sync
CPE4235
Generic
CPE4235
ภาษ์า Java เป2นภาษ์า Static Type ห์ร&อ Type Safe Language
ค&อ จิะต�องทั+าการก+าห์นดชิน�ดข�อม#ลให์�ก�บต�วิแปรก$อนทั*"จิะน+าไปใชิ�งานเสิ่มอ
CPE4235
แต$ม�นก,ม*ป8ญห์าบางป8ญห์า ห์ร&อ เห์ต�การณ์บางเห์ต�การณ์ทั*"ข�ดก�บห์ล�กการข�อน*!
CPE4235
ทั�ก class ในภาษ์า Java ม* class java.lang.Object เป2น parent ของ class ทั�!งห์มด
ถุ�าม*การประกาศต�วิแปร Object obj ไวิ� ต�วิแปรน*!สิ่ามารถุรองร�บข�อม#ลของ class ใดๆ ก,ได�
เพิราะทั�ก class ถุ&อเป2น Object
CPE4235
public void add(Object obj){//…
}
add(Object obj) สิ่$ง class อะไรเข�าไปได�บ�า ง ?
CPE4235
public void add(Object obj){//…
}
add(Object obj) สิ่$ง class อะไรเข�าไปได�บ�า ง ?- String- Integer- Float- …
CPE4235
ค��นๆ ม�!ย่ ห์ล�กการข�อน*!
CPE4235
Polymorphism ไง
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 ผ้$านม�!ย่ ?
CPE4235
List list = new ArrayList();
list.add(“1”);list.add(“2”);
Integer number = (Integer)list.get(0);
Compile ผ้$าน เพิราะ Syntax ไม$ผ้�ด แต$ Run ไม$ผ้$าน เพิราะเก�ด java.lang.ClassCastException
CPE4235
แล�วิเราจิะแก�ป8ญห์าน*!ย่�งไง
CPE4235
Generic
• เป2นค�ณ์สิ่มบ�ต�ห์น0"งทั*"ถุ#กเพิ�"มเข�ามาใน java 5 ห์ร&อ jdk 1.5
• เป2นค�ณ์สิ่มบ�ต�ทั*"เอาไวิ�ใชิ�สิ่+าห์ร�บตรวิจิสิ่อบควิามถุ#กต�องของชิน�ดข�อม#ลในขณ์ะทั*"ทั+าการ Compile โปรแกรม
CPE4235
เราคงจิะเคย่เห์,นสิ่�ญล�กษ์ณ์<> ในภาษ์า java เชิ$น
List<String> list = new ArrayList<>();Map<String, Integer> param = new HashMap<>();
<> ค&อสิ่�ญล�กษ์ณ์ทั*"บ$งบอกวิ$าเป2น Generic
CPE4235
ต�วิอย่$าง
List<String> list = new ArrayList<>();
ห์มาย่ควิามวิ$า list น*!รองร�บการเก,บข�อม#ลทั*"เป2น String เทั$าน�!น
CPE4235
ต�วิอย่$าง
List<String> list = new ArrayList<>();
Generic จิะตรวิจิสิ่อบตอน Compile Timeเราไม$สิ่ามารถุสิ่$งต�วิเลข เข�าไปใน list ได� เพิราะจิะทั+าให์� Compile ไม$ผ้$าน
CPE4235
Generic class เร*ย่กอ*กอย่$างห์น0"งวิ$า Parameterized Class ห์มาย่ถุ0ง class ทั*"สิ่ามารถุม* Parameter ได�
โดย่ Parameter ของ Generic เราจิะเร*ย่กวิ$า Type Parameter
CPE4235
public class FirstGenericClass<T> {
}
ห์ร&อ ม*ได�ห์ลาย่ต�วิ
public class FirstGenericClass<T, K, S, U> {
}
Type Parameter
CPE4235
public class FirstGenericClass<T> {
private T member;
public FirstGenericClass(T member){
this.member = member;}
public T getMember(){ return member; }}
CPE4235
FirstGenericClass<String> gen1;gen1 = new FirstGenericClass<>(“Hello”);
//ถุ�าใสิ่$แบบน*! FirstGenericClass<String> gen1;gen1 = new FirstGenericClass<>(50);
//จิะ Compile ไม$ผ้$าน
CPE4235
FirstGenericClass<String> gen1;gen1 = new FirstGenericClass<>(“Hello”);
System.out.println(gen.getMember().getClass().getName());//java.lang.String
CPE4235
FirstGenericClass<Integer> gen1;gen1 = new FirstGenericClass<>(100);
System.out.println(gen.getMember().getClass().getName());//java.lang.Integer
CPE4235
Type Parameter <T>
จิะม*ก*"ต�วิ ก*" Type ก,ได� แต$ละต�วิ ค�"นด�วิย่สิ่�ญล�กษ์ณ์ , (Comma) เชิ$น <T, S, E>
น�ย่มใชิ�ต�วิอ�กษ์รภาษ์าอ�งกฤษ์ต�วิให์ญ$
CPE4235
Generic Method
ในบางคร�!งเราอาจิต�องการใชิ�งาน Generic ในขอบเขตของ method เทั$าน�!น ไม$ต�องการใชิ�งานใน scope ของ class
สิ่ามารถุใชิ�งานได�ด�งน*!
CPE4235
public class SecondGenericClass {
public static <T> T fromInput(T input) { return input; }
}
ประกาศ <T> ไวิ�ห์น�า method เลย่แทันการประกาศไวิ�ทั*" class
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());
CPE4235
Bounded Type
เป2นการก+าห์นดขอบเขตของ Type Parameter วิ$าอน�ญาตให์�เป2น Type ใดได�บ�าง
CPE4235
ซ0"งโดย่ปกต�แล�วิการใชิ� <T> จิะไม$ก+าห์นดขอบเขต ค&อสิ่ามารถุใสิ่$ข�อม#ลอะไรเข�ามาใน T ก,ได�
CPE4235
public class SecondGenericClass {
public static <T extends Number> T fromInput(T input) { return input; }
}
T extends Number – T เป2นอะไรก,ได� แต$ต�องเป2น Number
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)
CPE4235
Java Collection
CPE4235
Java Collection
• เป2นเร&"องของการจิ�ดเก,บข�อม#ลเป2นชิ�ด
CPE4235
Java Collection
• เป2นเร&"องของการจิ�ดเก,บข�อม#ลโดย่ใชิ� โครงสิ่ร�างข�อม#ล (Data Structure)
ซ0"งถุ#กออกแบบมาเพิ&"อเพิ�"มประสิ่�ทัธ�ภาพิในการจิ�ดเก,บและการเข�าถุ0ง โครงสิ่ร�างข�อม#ลม*ห์ลาย่แบบ ข0!นอย่#$ก�บวิ$าเราจิะเล&อกใชิ�โครงสิ่ร�างข�อม#ลแบบใด
CPE4235
Collection API ห์ร&อ Collection Interface
เป2น spec ห์ร&อข�อก+าห์นดของ Collection ในแต$ละแบบ วิ$า จิะใชิ� Collection น�!นเก,บข�อม#ลล�กษ์ณ์ะไห์น ม*โครงสิ่ร�างข�อม#ลเป2นแบบใด
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
CPE4235
Iterable
เป2น interface สิ่#งสิ่�ดของเร&"องน*! เพิ&"อให์� interface อ&"นๆ extends ไป
เป2น interface ทั*"ใชิ�สิ่+าห์ร�บวินรอบ เพิ&"อด0งข�อม#ลสิ่มาชิ�กของ Collection ออกมาทั*ละต�วิ ด�วิย่ Iterator design pattern
CPE4235
package java.lang;import java.util.Iterator;
public interface Iterable<T> {
Iterator<T> iterator();}
CPE4235
package java.util;
public interface Iterator<E> {
boolean hasNext(); //ม*สิ่มาชิ�กต�วิถุ�ดไปห์ร&อไม$ E next(); //ค&นค$า สิ่มาชิ�กต�วิถุ�ดไป void remove(); //ลบสิ่มาชิ�กป8จิจิ�บ�น}
CPE4235
…Interater itr = collection.iterator();while(itr.hasNext()){
int member = itr.next();if(member < 10){
itr.remove();}
}
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
CPE4235
Collection
• เป2น Core interface ของเร&"อง Collection
• เป2น spec ทั*"คอย่ก+าห์นดวิ$า Collection ควิรม*พิฤต�กรรม ห์ร&อ method อะไรบ�าง
เชิ$น
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นต�น
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
CPE4235
List
เป2น interface ห์ร&อ spec ทั*"คอย่ก+าห์นดวิ$า implementation อะไรก,ตามของ interface น*!
จิะม*ล�กษ์ณ์ะ (โครงสิ่ร�าง) การจิ�ดเก,บข�อม#ลเป2นล+าด�บและใชิ� index ในการเข�าถุ0ง (ม*เร&"องของ index มาเก*"ย่วิข�อง)
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
CPE4235
Set
เป2น interface ห์ร&อ spec ทั*"คอย่ก+าห์นดวิ$า implementation อะไรก,ตามของ interface น*!
จิะม*ล�กษ์ณ์ะ (โครงสิ่ร�าง) การจิ�ดเก,บข�อม#ลทั*"ไม$ซ+!าก�น
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
CPE4235
Queue
เป2น interface ห์ร&อ spec ทั*"คอย่ก+าห์นดวิ$า implementation อะไรก,ตามของ interface น*!
จิะม*ล�กษ์ณ์ะ (โครงสิ่ร�าง) การจิ�ดเก,บข�อม#ลเป2นล+าด�บ
CPE4235
Collection
List Set Map
Collection API (Collection Interface)
extends
extends
interface
interface
interface
interface
Iterable
extends
interface
Queue
extends
interface
CPE4235
Map
เป2น interface ห์ร&อ spec ทั*"คอย่ก+าห์นดวิ$า implementation อะไรก,ตามของ interface น*!
จิะม*ล�กษ์ณ์ะ (โครงสิ่ร�าง) การจิ�ดเก,บข�อม#ลทั*"เป2นค#$ของ key/value
CPE4235
สิ่�งเกตวิ$า Map ไม$ได� implements Collection
CPE4235
สิ่ร�ป Collection API (Collection Interface)
List = ล+าด�บ + ม* indexSet = ไม$ซ+!าก�นQueue = ล+าด�บ ก$อนห์ล�งMap = เก,บค#$ key / value
CPE4235
Collection Implementation
CPE4235
Collection implementation
ค&อ class ทั*" implements interface ของ Collection แต$ละแบบ เชิ$น
• List – ArrayList, LinkedList, Vector, Stack
• Set – HashSet, LinkedHashSet • Queue – LinkedList,
PriorityQueue• Map – HashMap, Hashtable,
LinkedHashMap, Properties
CPE4235
List
ArrayList
LinkedList Stack
Collection Interface & Collection Implementation
implements
implements
interface
class classclass
Vector
implements
class
implements
CPE4235
List
ArrayList
LinkedList Stack
Collection Interface & Collection Implementation
implements
implements
interface
class classclass
Vector
implements
class
implements
CPE4235
• ArrayList – จิ�ดเก,บข�อม#ลเป2น Array • ข�อด* ค&อม*ควิามเร,วิในการเข�าถุ0งข�อม#ล
ด�วิย่ index : O(1) • ข�อเสิ่*ย่ ค&อควิามเร,วิในการเพิ�"มข�อม#ลทั*"
ต�องจิอง memory ให์ม$เสิ่มอ (เพิ&"อขย่าย่ขนาด) และควิามเร,วิในการค�นห์าทั*"ต�องไล$ห์าสิ่มาชิ�กทั*ละต�วิ ด�วิย่การ loop : O(n)
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
CPE4235
List
ArrayList
LinkedList Stack
Collection Interface & Collection Implementation
implements
implements
interface
class classclass
Vector
implements
class
implements
CPE4235
• LinkedList – จิ�ดเก,บข�อม#ลด�วิย่โครงสิ่ร�างข�อม#ล LinkedList• ข�อด* ค&อเพิ�"มข�อม#ล ลบข�อม#ล และจิ�ดเก,บ
ข�อม#ลได�เร,วิ : O(1)• ข�อเสิ่*ย่ ค&อควิามเร,วิในการค�นห์า ทั*"ต�องไล$
ห์าทั*ละต�วิ : O(n)
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
CPE4235
List
ArrayList
LinkedList Stack
Collection Interface & Collection Implementation
implements
implements
interface
class classclass
Vector
implements
class
implements
CPE4235
• Vector – จิ�ดเก,บข�อม#ลด�วิย่ Array เห์ม&อน ArrayList แต$เป2นSynchronized ค&อจิะอน�ญาตให์� Thread เข�าใชิ�งานได�ทั*ละต�วิเทั$าน�!น Thread ต�วิอ&"นจิะถุ#ก Block จินกวิ$า Thread ต�วิก$อนห์น�าจิะทั+างานเสิ่ร,จิ ถุ0งจิะม*สิ่�ทัธ�5เข�าใชิ�งานได� เพิ&"อควิามถุ#กต�องของข�อม#ล
CPE4235
List
ArrayList
LinkedList Stack
Collection Interface & Collection Implementation
implements
implements
interface
class classclass
Vector
implements
class
implements
CPE4235
• Stack – จิ�ดเก,บข�อม#ลด�วิย่โครงสิ่ร�างข�อม#ล Stack ค&อ First In Last Out (FILO) เข�าก$อนออกห์ล�ง
สิ่มาชิ�กต�วิไห์นทั*"ถุ#กน+าเข�ามาใน stack ก$อนจิะสิ่ามารถุเอาออกได�ทั*ห์ล�ง
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 เข�าก$อน ออกห์ล�งสิ่�ด
CPE4235
Set
HashSet
Collection Interface & Collection Implementation
implements
interface
class LinkedHashSet
implements
class
...
CPE4235
Set
HashSet
Collection Interface & Collection Implementation
implements
interface
class LinkedHashSet
implements
class
...
CPE4235
• HashSet – จิ�ดเก,บข�อม#ลด�วิย่วิ�ธ* Set ค&อข�อม#ลห์ร&อสิ่มาชิ�กใน Collection น*!ซ+!าก�นไม$ได� และใชิ� Hashing ในการจิ�ดเก,บและเข�าถุ0งข�อม#ลให์�เร,วิข0!น ล+าด�บการน+าเข�าไม$ม*ผ้ลใน Collection น*! (ไม$เร*ย่งตามล+าด�บ)
จิ+า method hashCode() ได�ม�!ย่ ใชิ�ก�บต�วิน*!แห์ล$ะ
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
CPE4235
for(Integer number : set){System.out.println(number +
“ ”); }
6 2 7 4 //เห์ล&อ 4 ต�วิ + ไม$เป2นไปตามล+าด�บ
CPE4235
Set
HashSet
Collection Interface & Collection Implementation
implements
interface
class LinkedHashSet
implements
class
...
CPE4235
• LinkedHashSet – ม*พิฤต�กรรมเป2น HashSet แต$ม*ล+าด�บการน+าเข�า
เป2นการผ้นวิกควิามสิ่ามารถุของ LinkedList + HashSet
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
CPE4235
for(Integer number : set){System.out.println(number +
“ ”); }
2 4 6 7 //เห์ล&อ 4 ต�วิ + เป2นไปตามล+าด�บ
CPE4235
Queue
PriorityQueue
Collection Interface & Collection Implementation
implements
interface
class
LinkedList
implements
class
...
CPE4235
Queue
PriorityQueue
Collection Interface & Collection Implementation
implements
interface
class
LinkedList
implements
class
...
CPE4235
• PriorityQueue - เป2น Queue ประเภทัห์น0"ง ทั*"เราสิ่ามารถุเข�าถุ0งสิ่มาชิ�ก ได�ตามล+าด�บควิามสิ่+าค�ญของสิ่มาชิ�กต�วิน�!น
CPE4235
Queue
PriorityQueue
Collection Interface & Collection Implementation
implements
interface
class
LinkedList
implements
class
...
CPE4235
• LinkedList - ก,ถุ&อเป2น Queue ต�วิห์น0"ง เพิราะม*พิฤต�กรรม First In First Out (FIFO)
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
CPE4235
Map
HashMap
HashTable
Properties
Collection Interface & Collection Implementation
implements
implements
interface
class classclass LinkedHashMap
implements
class
implements
CPE4235
Map
HashMap
HashTable
Properties
Collection Interface & Collection Implementation
implements
implements
interface
class classclass LinkedHashMap
implements
class
implements
CPE4235
• HashMap – เป2น Map ประเภทัห์น0"ง จิ�ดเก,บข�อม#ล เป2น key / value ด�วิย่วิ�ธ* Hashing ทั*"ได�จิาก method hashCode() ของสิ่มาชิ�ก
CPE4235
HashMap
เป2น Unsynchronized ค&อย่อมให์� Thread ห์ลาย่ๆ ต�วิเข�าถุ0งข�อม#ลได�โดย่ไม$เก�ดการ Block Thread
ข�อด*ค&อควิามเร,วิในการเข�าถุ0ง สิ่$วินข�อเสิ่*ย่ ค&อเร&"องของควิามถุ#กต�องของข�อม#ล เพิราะอน�ญาตให์� Thread ห์ลาย่ต�วิ access ข�อม#ลได�พิร�อมก�น
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
CPE4235
Map
HashMap
HashTable
Properties
Collection Interface & Collection Implementation
implements
implements
interface
class classclass LinkedHashMap
implements
class
implements
CPE4235
• HashTable – คล�าย่ๆ HashMap แต$เป2น Synchronized ค&ออน�ญาตให์� Thread เข�าใชิ�งานได�ทั*ละต�วิเทั$าน�!น
จิะ Block Thread ต�วิป8จิจิ�บ�น จินกวิ$า Thread ต�วิก$อนห์น�าจิะทั+างานเสิ่ร,จิ
CPE4235
Map
HashMap
HashTable
Properties
Collection Interface & Collection Implementation
implements
implements
interface
class classclass LinkedHashMap
implements
class
implements
CPE4235
• LinkedHashMap – เป2น Map ทั*"ม*ล+าด�บการน+าเข�าข�อม#ล ซ0"งโดย่ปกต�แล�วิ Map จิะไม$ม*ล+าด�บการน+าเข�า
CPE4235
Map
HashMap
HashTable
Properties
Collection Interface & Collection Implementation
implements
implements
interface
class classclass LinkedHashMap
implements
class
implements
CPE4235
• Properties – เป2น Map เฉพิาะประเภทัน0งทั*"เอาไวิ�เก,บค$า Key / Value ของ Properties File (.properties) extends มาจิาก HashTable เป2น Synchronized
CPE4235
CPE4235
การเล&อก Collection ไปใชิ�งาน
ข0!นอย่#$ก�บวิ�ตถุ�ประสิ่งคทั*"เราจิะใชิ� และควิามห์มาะสิ่มของการจิ�ดเก,บข�อม#ล โดย่การด#ทั*" Big O วิ$าอ�นไห์นม* Big O น�อย่ทั*"สิ่�ด ในวิ�ตถุ�ประสิ่งคทั*"เราจิะน+าไปใชิ�
CPE4235
เชิ$น
ต�องการจิ�ดเก,บข�อม#ลทั*"ไม$ซ+!า + access เร,วิ ก,ใชิ� HashSetต�องการจิ�ดเก,บข�อม#ลเป2นล+าด�บ + access เร,วิ ก,ใชิ� ArrayListต�องการจิ�ดเก,บข�อม#ลเป2นค#$ key / value + เร,วิ ก,ใชิ� HashMapต�องการจิ�ดเก,บข�อม#ลไม$ซ+!า + เป2นล+าด�บ ก,ใชิ� LinkedHashSet
เป2นต�น
CPE4235 List
Map
Set
Queue
CPE4235
การบ�าน (10 คะแนน)
ให์�ทั+าราย่งานเร&"อง Java Collection ห์ร&อ Thread อย่$างใดอย่$างห์น0"ง
CPE4235
Java Collection
ม*ก*"ประเภทั อะไรบ�าง (มากกวิ$าทั*"ม*อย่#$ใน slide) พิร�อมอธ�บาย่ ประโย่ชิน ข�อด* ข�อเสิ่*ย่ การเล&อกใชิ� Collection วิ$าด#จิากอะไร + ม*ต�วิอย่$าง code ประกอบจิะด*มาก
CPE4235
Thread
อธ�บาย่ควิามเป2นมา ป8ญห์า แนวิค�ด การแก�ป8ญา (synchronized) ต�วิอย่$างการใชิ�งาน + วิงจิรชิ*วิ�ตของ Thread
CPE4235
เน&!อห์าสิ่ร�ปรวิบร�ด อ$านเข�าใจิ ไม$มากจินเก�นไป ห์ร&อน�อย่จินเก�นไป ตามควิามห์มาะสิ่ม (สิ่ร�ป)
สิ่$งวิ�นไห์นด* ? ก$อนสิ่อบกลางภาค