Comparator Containers Iterator Factory Pattern Trove 89 210

  • Slides: 22
Download presentation
 הקדמה Comparator Containers שימושיים Iterator Factory Pattern Trove הטמעה 89 -210 תכנות מתקדם

הקדמה Comparator Containers שימושיים Iterator Factory Pattern Trove הטמעה 89 -210 תכנות מתקדם - תרגול 3 תכנות מתקדם 89 -210 תרגול מספר 3 תשע"א 2010 -2011 Containers ב Java אליהו חלסצ'י

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 Comparator : • כל מחלקה יכולה לממש את הממשקים הבאים interface Comparator { int compare(Object o 1, Object o 2); } interface Comparable { int compare. To(Object o) ; } containers ה , • ע"פ המימוש באחד מממשקים אלו מחליטים כיצד למיין בפנים את העצמים השמורים

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה Robot r 1=new RV

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה Robot r 1=new RV 1(); Robot r 2=new RV 1(); Robot r 3=new RV 1(); r 1. set. Name("aaabc"); r 2. set. Name("aabc"); r 3. set. Name("abc"); 3 תרגול - תכנות מתקדם 89 -210 Comparator : פלט aaabc abc : • דוגמא public class RV 1 implements Robot , Comparable<Robot>{ … private String name; public String get. Name() { return name; } public int compare. To(Robot arg 0) { return name. compare. To(arg 0. get. Name()); } Array. List<Robot> ar=new Array. List<Robot>(); ar. add(r 3); } ar. add(r 2); ar. add(r 1); Collections. sort(ar); for(int i=0; i<ar. size(); i++){ System. out. println(ar. get(i). get. Name()); } : מהירה ויציבה merge sort אופטימיזציה של : מיון מהיר (quick sort תמיד מובטח )בניגוד ל nlog(n). עובד מהר יותר על רשימות כמעט ממוינות : מיון יציב . לא ממיינת עצמים שווים ( )דוג' דואר שממוין לפי תאריך

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 שימושיים Containers : Collections • מתודות שימושיות ל – – – – – boolean add(Objetc o) boolean add(Collection c) void clear() boolean contains(Object o) boolean is. Empty() Iterator iterator() boolean remove(Object o) boolean remove. All(Collection c) int size() Object[] to. Array()

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 שימושיים Containers : Map • מתודות שימושיות ל – – – Object put(Object key, Object value) void put. All(Map t) Object get(Object key) void clear() boolean contains. Key(Object key) boolean contains. Value(Object value) boolean is. Empty() Object remove(Object key) int size() Set entry. Set() Set key. Set() Collection values()

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 Iterator : • דוגמאות Array. List<Robot> ar=new Array. List<Robot> (); ar. add(r 3); ar. add(r 2); ar. add(r 1); for(int i=0; i<ar. size(); i++){ System. out. println(ar. get(i). get. Name()); } for(Robot r : ar){ System. out. println(r. get. Name()); } Iterator<Robot> it=ar. iterator(); while (it. has. Next()){ System. out. println(it. next(). get. Name()); }

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 Iterator Hash. Set hs<Robot>=new Hash. Set<Robot> (); hs. add(r 3); hs. add(r 2); hs. add(r 1); : • דוגמאות Hash. Set – Iterator<Robot> it=hs. iterator(); while (it. has. Next()){ System. out. println(it. next(). get. Name ()); } String key; Hash. Map<String, Robot> hm=new Hash. Map<String, Robot> (); hm. put(r 3. get. Name(), r 3); hm. put(r 2. get. Name(), r 2); hm. put(r 1. get. Name(), r 1); Iterator<String> it=hm. key. Set(). iterator(); while (it. has. Next()){ key=it. next(); System. out. println(hm. get(key). get. Name ()); } Hash. Map –

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 Iterator Hash. Map • דרכים נוספות לעבור על for(Entry<String, Robot> e : hm. entry. Set()) System. out. println(e. get. Key()+", "+e. get. Value()); for(String k : hm. key. Set()) System. out. println(k+", "+hm. get(k)); for(Robot r : hm. values()) System. out. println(r);

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 Factory Pattern private Hash. Map<String, Robot. Fac> robot. Factory; import java. util. Hash. Map; public class Robot. Factory { public Robot. Factory(){ robot. Factory=new Hash. Map<String, Robot. Fac> (); robot. Factory. put("RV 1", new RV 1 Fac()); private interface Robot. Fac{ robot. Factory. put("RV 2", new RV 2 Fac()); public Robot create(); robot. Factory. put("Aibo", new Aibo. Fac()); } } public Robot create. Robot(String type){ private class RV 1 Fac implements Robot. Fac{ return robot. Factory. get(type). create(); public Robot create(){ return new RV 1(); } } private class RV 2 Fac implements Robot. Fac{ public Robot create(){ return new RV 2(); } } private class Aibo. Fac implements Robot. Fac{ public Robot create(){ return new Aibo(); } } . create ניצור ממשק עם הפקודה . 1

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 Factory Pattern private Hash. Map<String, Robot. Fac> robot. Factory; import java. util. Hash. Map; public class Robot. Factory { public Robot. Factory(){ robot. Factory=new Hash. Map<String, Robot. Fac> (); robot. Factory. put("RV 1", new RV 1 Fac()); private interface Robot. Fac{ robot. Factory. put("RV 2", new RV 2 Fac()); public Robot create(); robot. Factory. put("Aibo", new Aibo. Fac()); } } public Robot create. Robot(String type){ private class RV 1 Fac implements Robot. Fac{ return robot. Factory. get(type). create(); public Robot create(){ return new RV 1(); } } private class RV 2 Fac implements Robot. Fac{ public Robot create(){ return new RV 2(); } } private class Aibo. Fac implements Robot. Fac{ public Robot create(){ return new Aibo(); } } . create ניצור ממשק עם הפקודה . נממש את הממשק ע"י מחלקות עבור כל אחד מסוגי האובייקטים הרצויים . 1. 2

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 Factory Pattern private Hash. Map<String, Robot. Fac> robot. Factory; import java. util. Hash. Map; public class Robot. Factory { public Robot. Factory(){ robot. Factory=new Hash. Map<String, Robot. Fac> (); robot. Factory. put("RV 1", new RV 1 Fac()); private interface Robot. Fac{ robot. Factory. put("RV 2", new RV 2 Fac()); public Robot create(); robot. Factory. put("Aibo", new Aibo. Fac()); } } public Robot create. Robot(String type){ private class RV 1 Fac implements Robot. Fac{ return robot. Factory. get(type). create(); public Robot create(){ return new RV 1(); } } private class RV 2 Fac implements Robot. Fac{ public Robot create(){ return new RV 2(); } } private class Aibo. Fac implements Robot. Fac{ public Robot create(){ return new Aibo(); } } . create ניצור ממשק עם הפקודה . נממש את הממשק ע"י מחלקות עבור כל אחד מסוגי האובייקטים הרצויים כשהמפתח הוא השם דרכו , עבור המחלקות הנ"ל Hash. Map ניצור . המשתמש בוחר ליצור את האובייקט הרצוי . 1. 2. 3

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 Factory Pattern private Hash. Map<String, Robot. Fac> robot. Factory; import java. util. Hash. Map; public class Robot. Factory { public Robot. Factory(){ robot. Factory=new Hash. Map<String, Robot. Fac> (); robot. Factory. put("RV 1", new RV 1 Fac()); private interface Robot. Fac{ robot. Factory. put("RV 2", new RV 2 Fac()); public Robot create(); robot. Factory. put("Aibo", new Aibo. Fac()); } } public Robot create. Robot(String type){ private class RV 1 Fac implements Robot. Fac{ return robot. Factory. get(type). create(); public Robot create(){ return new RV 1(); } } private class RV 2 Fac implements Robot. Fac{ public Robot create(){ return new RV 2(); } } private class Aibo. Fac implements Robot. Fac{ public Robot create(){ return new Aibo(); } } . create ניצור ממשק עם הפקודה . נממש את הממשק ע"י מחלקות עבור כל אחד מסוגי האובייקטים הרצויים כשהמפתח הוא השם דרכו , עבור המחלקות הנ"ל Hash. Map ניצור . המשתמש בוחר ליצור את האובייקט הרצוי Hashmap < ל String, Robot. Fac> נכניס את הזוגות . 1. 2. 3. 4

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 Factory Pattern private Hash. Map<String, Robot. Fac> robot. Factory; import java. util. Hash. Map; public class Robot. Factory { public Robot. Factory(){ robot. Factory=new Hash. Map<String, Robot. Fac> (); robot. Factory. put("RV 1", new RV 1 Fac()); private interface Robot. Fac{ robot. Factory. put("RV 2", new RV 2 Fac()); public Robot create(); robot. Factory. put("Aibo", new Aibo. Fac()); } } public Robot create. Robot(String type){ private class RV 1 Fac implements Robot. Fac{ return robot. Factory. get(type). create(); public Robot create(){ return new RV 1(); } } private class RV 2 Fac implements Robot. Fac{ public Robot create(){ return new RV 2(); } } private class Aibo. Fac implements Robot. Fac{ public Robot create(){ return new Aibo(); } } . create ניצור ממשק עם הפקודה . נממש את הממשק ע"י מחלקות עבור כל אחד מסוגי האובייקטים הרצויים כשהמפתח הוא השם דרכו , עבור המחלקות הנ"ל Hash. Map ניצור . המשתמש בוחר ליצור את האובייקט הרצוי Hashmap < ל String, Robot. Fac> נכניס את הזוגות ניגש בזמן , בהינתן מחרוזת שהמשתמש בחר : פקודת יצירת האובייקט אל האובייקט השמור תחת אותה המחרוזת Hash. Map ל O(1) של . ונקבל את האובייקט הרצוי create כמפתח נקרא ל . 1. 2. 3. 4. 5 . Factory נוחות לתחזק ולהוסיף בעתיד או אובייקטים ל + ליצירת אובייקטים O(1) וקיבלנו זמן של , מקום O(n) זמן ב O(n) החלפנו

 הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות

הקדמה Comparator שימושיים Containers Iterator Factory Pattern Trove הטמעה 3 תרגול - תכנות מתקדם 89 -210 Trove import gnu. trove. *; : • קוד לדוגמא public class Hello. World. App { static TByte. Float. Hash. Map hm=new TByte. Float. Hash. Map(); public static void main(String[] args) { float last. Value. For. This. Key; byte key 1=1, key 2=2, key 3=3; hm. put(key 1, (float) 0. 2); last. Value. For. This. Key=hm. put(key 1, (float) 0. 1); System. out. println("last value for key 1 was "+last. Value. For. This. Key); hm. put(key 2, (float) 0. 2); hm. put(key 3, (float) 0. 3); TByte. Float. Iterator it= hm. iterator(); while (it. has. Next()){ it. advance(); System. out. println("key: "+it. key()+" , value: "+ it. value()); } } } : פלט last value for key 1 was 0. 2 key: 2 , value: 0. 2 key: 1 , value: 0. 1 key: 3 , value: 0. 3