Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 35 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
35
Dung lượng
205,67 KB
Nội dung
GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU CẤU TRÚC DỮ LIỆU DATA STRUCTURES [214331] Iterator - Comparable - Comparator /XX 12/3/15 Nguyễn Xuân Vinh nguyenxuanvinh@hcmuaf.edu.vn /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH Java Collection Architecture GV: NGUYỄN XUÂN VINH The Differences!!! How to browse element? Elements of List are indexed List index value 12 0 0 Linked List int value = list[0]; Elements of LinkedList, Sets and Maps can't be accessed MÔN: CẤU TRÚC DỮ LIỆU by index How to remove element? "the" Set "from" /XX 12/3/15 Map "to" "we" GV: NGUYỄN XUÂN VINH Examining sets and maps elements of Java Sets and Maps can't be accessed by index must use a "foreach" loop: Set scores = new HashSet(); for (int score : scores) { System.out.println("The score is " + score); MÔN: CẤU TRÚC DỮ LIỆU } Problem: foreach is read-only; cannot modify set while looping for (int score : scores) { if (score < 60) { // throws a ConcurrentModificationException scores.remove(score); } /XX 12/3/15 } iterator: An object that allows a client to traverse the elements of any collection, regardless of its implementation Remembers a position within a collection, and allows you to: get the element at that position advance to the next position (possibly) remove or change the element at that position MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH Iterators (11.1) index value 12 0 0 size /XX 12/3/15 list Benefit: A common way to examine any collection's elements iterator set "to" "we" "from" current element: current index: "the" iterator current element: next element: "the" "from" GV: NGUYỄN XUÂN VINH Iterator methods hasNext() returns true if there are more elements to examine next() returns the next element from the collection (throws a NoSuchElementException if there are none left to examine) /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU remove() removes from the collection the last value returned by next() (throws IllegalStateException if you have not called next() yet) Iterator interface in java.util every collection has an iterator() method that returns an iterator over its elements Set set = new HashSet(); Iterator itr = set.iterator(); GV: NGUYỄN XUÂN VINH Iterator example Set scores = new HashSet(); scores.add(38); scores.add(94); scores.add(87); scores.add(43); scores.add(62); MÔN: CẤU TRÚC DỮ LIỆU Iterator itr = scores.iterator(); while (itr.hasNext()) { int score = itr.next(); System.out.println("The score is " + score); // eliminate any failing grades if (score < 60) { itr.remove(); 12/3/15 System.out.println(scores); } /XX } // [62, 94, 87] GV: NGUYỄN XUÂN VINH Iterator example Map scores = new HashMap(); scores.put("Kim", 38); scores.put("Lisa", 94); scores.put("Ryan", 87); scores.put("Morgan", 43); scores.put("Marisa", 62); MÔN: CẤU TRÚC DỮ LIỆU Iterator itr = scores.keySet().iterator(); while (itr.hasNext()) { String name = itr.next(); int score = scores.get(name); System.out.println(name + " got " + score); // eliminate any failing students if (score < 60) { /XX 12/3/15 itr.remove(); // removes name and score } } System.out.println(scores);// {Marisa=62, Lisa=94, Ryan=87} GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX Exercise Modify the Book Search program from last lecture to eliminate any words that are plural or all-uppercase from the collection GV: NGUYỄN XUÂN VINH Set/Map and ordering Some types have a notion of a natural ordering TreeSet/Map store values sorted by their natural ordering Set scores = new HashSet(); scores.add(38); scores.add(94); MÔN: CẤU TRÚC DỮ LIỆU scores.add(87); scores.add(43); // unpredictable order scores.add(62); System.out.println(scores); // [62, 94, 43, 87, 38] Set scores = new TreeSet(); scores.add(38); scores.add(94); 12/3/15 System.out.println(scores); 10 scores.add(43); /XX scores.add(87); // sorted natural order scores.add(62); // [38, 43, 62, 87, 94] GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 21 Understanding Comparable public int compareTo(Object obj) Elements must be mutually comparable: You can only compare like elements Or ClassCastException will be thrown In most cases, the two objects must be of the same type (or a subtype) GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 22 Understanding Comparable The return value states the relative position to the natural ordering: compareTo() method can return one of three values: Negative number: current object > object compared to Positive number: current object < object compared to Zero: two objects are equal GV: NGUYỄN XUÂN VINH Understanding Comparable The natural ordering should be consistent with equals(): Two elements are equal (equals()): compareTo() should return zero Your class implements Comparable and inconsistent with equals(): won't work properly within a SortedSet or 23 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU SortedMap GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 24 Understanding Comparable Never call the method directly: Collections Framework call the compareTo() method for you when necessary Not your responsibility to call the method 25 /XX Comparator 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 26 Comparator basics Comparable objects: compare multiple instances of themselves Comparator: compare two other objects Custom ordering by creating a Comparator’s implement when: Don't like the natural ordering Your class doesn't implement Comparable GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 27 Comparator basics Ordering is done through the compare () method GV: NGUYỄN XUÂN VINH Understanding Comparator You have to implement compare() compare() compareTo() But, both objects to compare must be passed in as arguments: 28 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU public int compare(Object obj1, Object obj2) There is one predefined Comparator already implemented for you to sort elements in their reverse natural order: Collections reverseOrder() comparator MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH Understanding Comparator When should we use Comparable or 29 /XX 12/3/15 Comparator? 30 /XX ITERABLE - ITERATOR 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 31 Iterator Object An iterator is an object that implements the interface Iterator GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU Usage of Iterator Provide a uniform way of accessing collection elements sequentially in earlier versions of Java From JDK 32 /XX 12/3/15 will work with anything that implements the interface Iterable GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 33 Directly implementing Iterable Collection interface was made to extend Iterable any collection can be the target of foreach To build your own implementation of Iterable GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 34 Summary Sorting: Comparable, Comparator Iterable, Iterator 35 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH HỎI ĐÁP [...]... comparator MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH Understanding Comparator When should we use Comparable or 29 /XX 12/3/15 Comparator? 30 /XX ITERABLE - ITERATOR 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 31 Iterator Object An iterator is an object that implements the interface Iterator GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU Usage... /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU SortedMap GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 24 Understanding Comparable Never call the method directly: Collections Framework call the compareTo() method for you when necessary Not your responsibility to call the method 25 /XX Comparator 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX... into a random order sort(list) arranges elements into ascending order GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 19 Comparable basics Ordering is done through the compareTo() method 20 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH System-Defined Comparable classes GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 21 Understanding Comparable public int compareTo(Object obj)... GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 33 Directly implementing Iterable Collection interface was made to extend Iterable any collection can be the target of foreach To build your own implementation of Iterable GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 34 Summary Sorting: Comparable, Comparator Iterable, Iterator 35 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN... class doesn't implement Comparable GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 27 Comparator basics Ordering is done through the compare () method GV: NGUYỄN XUÂN VINH Understanding Comparator You have to implement compare() compare() compareTo() But, both objects to compare must be passed in as arguments: 28 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU public int compare(Object obj1, Object... type (or a subtype) GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 22 Understanding Comparable The return value states the relative position to the natural ordering: compareTo() method can return one of three values: Negative number: current object > object compared to Positive number: current object < object compared to Zero: two objects are equal GV: NGUYỄN XUÂN VINH Understanding Comparable... thread "main" java.lang.ClassCastException at java.util.TreeMap.put(TreeMap.java: 542 ) 11 /XX 12/3/15 at java.util.TreeSet.add(TreeSet.java:238) at MyProgram.main(MyProgram.java: 24) GV: NGUYỄN XUÂN VINH Comparable (10.2) public interface Comparable { public int compareTo(E other); } 12 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU A class can implement the Comparable interface to define a natural ordering... return 0; } 13 /XX } } // same x and same y GV: NGUYỄN XUÂN VINH compareTo tricks subtraction trick - Subtracting related numeric values produces the right result for what you want compareTo to return: // sort by x and break ties by y public int compareTo(Point other) { if (x != other.x) { return x - other.x; // different x 14 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU } else { return y - other.y; // same x;...GV: NGUYỄN XUÂN VINH Ordering our own types We cannot make a TreeSet or TreeMap of any arbitrary type, because Java doesn't know how to order the elements The program compiles but crashes when we run it MÔN: CẤU TRÚC DỮ LIỆU Set tags = new TreeSet(); tags.add(new HtmlTag("body", true)); tags.add(new... - other.x < 0 == other.x, then x - other.x == 0 GV: NGUYỄN XUÂN VINH compareTo tricks 2 delegation trick - If your object's fields are comparable (such as strings), use their compareTo results to help you: // sort by employee name, e.g "Jim" < "Susan" public int compareTo(Employee other) { return name.compareTo(other.getName()); MÔN: CẤU TRÚC DỮ LIỆU } toString trick - If your object's toString ... NGUYỄN XUÂN VINH GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 31 Iterator Object An iterator is an object that implements the interface Iterator GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU... GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 19 Comparable basics Ordering is done through the compareTo() method 20 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH System-Defined... Iterable GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 34 Summary Sorting: Comparable, Comparator Iterable, Iterator 35 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH HỎI ĐÁP