Download presentation
Presentation is loading. Please wait.
1
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 1 Chapter 11 Searching and Sorting with Objects Comparable Interface Comparator objects
2
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 2 Searching Objects Similar to searching primitive arrays except you have several choices –Search for a particular object (by reference) in the array Compare using == –Search for a single object with a particular property or group of properties Match all the properties of a particular object using equals method Use accessor methods to get appropriate data for comparison –Search for all objects that meet search criterion Return an array (or collection) instead of a single object
3
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 3 Sorting Objects In order to sort objects of a particular type, we need a way to compare them –The relational operators ( = >) do not work with objects Two approaches to object comparison –Comparable interface The object class must implement the Comparable interface Class must define the method int compareTo( o) –Comparator interface Create a new class which implements the Comparator interface Need to define two methods int compare( o1, o2) boolean equals( Object o) // compare two Comparators
4
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 4 Comparable Interface Any class that implements Comparable must contain the method int compareTo( o) Since it is inside the class, compareTo has access to all instance variables The Arrays class has a sort method that sorts Comparable objects –public static void sort(Comparable[] a)
5
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 5 Semantics of compareTo o1.compareTo( o2) returns –negative number if o1 comes before o2 –zero if o1 and o2 are the same –positive number if o1 comes after o2 For Strings –"abc".compareTo("bcd") is negative –"abc".compareTo("ABC") is positive –"abc".compareTo("abc") is 0
6
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 6 Comparator Interface Create a separate class whose sole purpose is to provide a comparison method int compare( o1, o2) Since it is a separate class, compareTo has to use accessor methods to get instance data The Arrays class has a sort method that sorts Comparable objects public static void sort(T[] a, Comparator c)
7
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 7 Semantics of compare ComparatorClass.compare ( o1, o2) returns –negative number if o1 comes before o2 –zero if o1 and o2 are the same –positive number if o1 comes after o2
8
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 8 Comparing Person Objects We define the Person class so we can compare Person objects by name or by age
9
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 9 Comparing Person Objects First, we need to determine how to compare Person objects The following does not make sense: Person p1 = …; Person p2 = …; if ( p1 < p2 ) { … }
10
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 10 Choosing an attribute to compare Modify the Person class to include a static variable that tells which attribute to compare. class Person implements Comparable { … public static final int NAME = 0; public static final int AGE = 1; public static int compareAttribute = NAME; … public static void setCompareAttribute(int attribute) { compareAttribute = attribute; public int compareTo( Object person){ …} } … }
11
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 11 The compareTo Method To compare Person objects, first set the comparison attribute and then call the compareTo Method. Person.setCompareAttribute (Person.NAME); int compResult = p1.compareTo(p2); if (compResult < 0) { //p1 “less than” p2 } else if (compResult == 0) { //p1 “equals” pw } else { //p2 “greater than” p2 } public int compareTo(Person p) { int compResult; if ( comparisonAttribute == AGE ) { int p2age = p.getAge(); if ( this.age < p2age ) { compResult = LESS; } … } else { //compare Name String p2Name = p.getName(); compResult = this.name. compareTo(p2Name); } return compResult; }
12
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 12 Using Comparators Implement the Comparator interface. We can define the implementation class so we can compare Person objects using any combination of their attributes –For example, we can define a comparator that compares Person objects by using both name and age
13
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 13 The AgeComparator Class This class compares the age attributes of Person objects class AgeComparator implements Comparator { private final int LESS = -1; private final int EQUAL = 0; private final int MORE = 1; public int compare(Object p1, Object p2) { int comparisonResult; int p1age = ((Person)p1).getAge( ); int p2age = ((Person)p2).getAge( ); if (p1age < p2age) { comparisonResult = LESS; } else if (p1age == p2age) { comparisonResult = EQUAL; } else { assert p1age > p2age; comparisonResult = MORE; } return comparisonResult; }
14
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 14 The NameComparator Class This class compares the age attributes of Person objects Because the name attribute is a string we can use the compareTo method of the String class class NameComparator implements Comparator { public int compare(Object p1, Object p2) { String p1name = ((Person)p1).getName( ); String p2name = ((Person)p2).getName( ); return p1name.compareTo(p2name); }
15
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 15 The sort Method We use the sort method of the java.util.Arrays class public Person[ ] sort ( int attribute ) { if (!(attribute == Person.NAME || attribute == Person.AGE) ) { throw new IllegalArgumentException( ); } Person[ ] sortedList = new Person[ count ]; //copy references to sortedList for (int i = 0; i < count; i++) { sortedList[i] = entry[i]; } Arrays.sort(sortedList, getComparator(attribute)); return sortedList; }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.