Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 103 Comperator and Comparable Thomas Kuehne 2015-T2 Lecture 9

Similar presentations


Presentation on theme: "COMP 103 Comperator and Comparable Thomas Kuehne 2015-T2 Lecture 9"— Presentation transcript:

1 COMP 103 Comperator and Comparable Thomas Kuehne 2015-T2 Lecture 9
School of Engineering and Computer Science, Victoria University of Wellington 2015-T2 Lecture 9

2 RECAP  TODAY RECAP TODAY Declaring and Creating Collections
2 RECAP Declaring and Creating Collections Different Collections: List, Set, Stack, Queue, Map, … Using collections: adding, removing, getting, setting,…. Collections of collections Iterators TODAY Ways to compare objects of the same type

3 Bringing Order to Collections
3 Ordered collections may be sorted the original order of addition may not be useful different orderings may serve different purposes public class Product { String name; int price; int rating; : } Products in an online shop should be sortable according to different criteria

4 Custom Sorting Orders Looking for products by name? Product p1, p2;
4 Looking for products by name? Galaxy S6 $789 iPhone 6 $959 Xperia Z3 $668 Product p1, p2; : p1.name < p.2 name : Does this work?

5 Custom Sorting Orders Looking for products by rating? Product p1, p2;
5 Looking for products by rating? Xperia Z3 $668 Galaxy S6 $789 iPhone 6 $959 Product p1, p2; : p1.rating > p.2 rating :

6 Custom Sorting Orders Looking for products by price? Product p1, p2;
6 Looking for products by price? Galaxy S6 $789 iPhone 6 $959 Xperia Z3 $668 Product p1, p2; : p1.price > p2.price :

7 Sorting Collections The good news p1 p2
works on any List (with comparable elements) 7 The good news java.util.Collections provides a sort method for Lists But how to provide the sorting criterion? products p1.name < p2.name Sorting Algorithm p1.rating > p2.rating p1 p2 p1.price > p2.price products sorted

8 Using a Sorting Criterion
8 List Sorting Algorithm Comparator -2 -4 3 Three potential outcomes of a comparison p1 = p2  result = 0 p1 > p2  result > 0 p1 < p2  result < 0

9 return p1.price – p2.price;
Supplying the Sorting Criterion 9 Approach 1: Passing in a method (lambda) Collections.sort(phones, (p1, p2) –> { } ); return p1.price – p2.price; Annotation Type FunctionalInterface -> if there is only one abstract method, you can provide a lambda = 0  p1 = p2 > 0  p1 > p2 < 0  p1 < p1 products are provided by sorting algorithm

10 object that responds to one compare(…) message
Supplying the Sorting Criterion 10 Approach 2: Passing in an object (comparator) object that responds to one compare(…) message Collections.sort(phones, ); priceComparator

11 Supplying the Sorting Criterion
11 Approach 2: Passing in an object (comparator) object that responds to one compare(…) message Collections.sort(phones, ); new PriceComparator() class PriceComparator implements Comparator<Product> { public int compare(Product p1, Product p2) { return p1.price - p2.price; } Where is a good place for this class?

12 Defining Sorting Criteria
12 The key to achieving different sorting orders is to define different comparators PriceComparator, RatingComparator, NameComparator, … Comparator is a Java interface stipulates “int compare(T o1, T o2)” method Comparators have further usages priority queues, supporting sorted structures, …

13 Comparator Alternative
13 do we always have to supply or even define a comparator? can we not just use “Collections.sort(phones);”? how about “if (todayDate > deadlineDate) {…}”? can we have default, “natural” orderings? e.g., lexicographical ordering for String Interface Comparable to the rescue! natural ordering for products, dates, strings, … stipulates “int compareTo(T o)” method

14 Comparator Alternative
14 “Berta”.compareTo(“Celia”)  -1 iPhone.compareTo(xperia)  -291 today.compareTo(new Date(2015, 8, 2))  2 say “It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y))” Method compareTo(…) yields 0, if the receiver and the argument are equal <0, if the receiver is smaller than the argument >0, if the receiver is bigger than the argument (cf. API documentation regarding “equals”…)

15 Making Products Comparable
15 public class Product implements Comparable<Product> { String name; // product name int price; // end price int rating; // star rating : public int compareTo(Product p) { return this.price - p.price; } } Natural ordering for products is defined to be by price

16 Multiple Criteria with Priority?
16 how to support multi-criterion comparison? e.g., first star rating then price easy to support by first comparing using the highest priority criterion … and in the case of a tie, use the next etc.

17 Comparator vs Comparable
17 A comparator object defines one of many ways to compare two objects to each other think “operator” as in “o1 operator o2” (e.g., vs 2 * 3) A comparable object is one that can be compared to another object of the same kind think “can be comparedTo” as in o1 <natural order o2 (o1 and o2 are comparable)

18 Comparator vs Comparable
18 Comparator is an interface that allows clients to use custom comparison criteria int compare(o1, o2)  -ve, 0, +ve Comparable is an interface that allows objects to be compared according to their natural ordering int o1.compareTo(o2)  -ve, 0, +ve A default comparator may just refer to compareTo()


Download ppt "COMP 103 Comperator and Comparable Thomas Kuehne 2015-T2 Lecture 9"

Similar presentations


Ads by Google