slides adapted from Marty Stepp and Hélène Martin

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 10
Advertisements

Polymorphism. 3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Computer Science 209 Software Development Equality and Comparisons.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Searching and Sorting I 1 Searching and Sorting 1.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 10: ArrayList.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
CSE 143 Lecture 11 Recursive Programming reading: slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
CSE 143 Lecture 13 Recursive Programming reading: slides created by Marty Stepp
ADSA: Generics/ Advanced Data Structures and Algorithms Objective –to describe basic forms of generic classes, interfaces, and methods for searching.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
1 CSE 331 The Object class; Object equality and the equals method slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R.
1 CSE 331 Comparing objects; Comparable, compareTo, and Comparator slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Lecture 5:Interfaces and Abstract Classes
Lecture 23:More on Interfaces
Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2
Searching.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
Collections.
Interfaces.
Lecture 17: Polymorphism (Part II)
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Building Java Programs Chapter 10
CSE 143 Lecture 22 The Object class; Polymorphism read
Interfaces CS163 Fall 2018.
Homework 8: Critters (cont.)
CSE 143 Lecture 11 Recursive Programming reading:
Topic 33 ArrayList.
CSE 143 Lecture 22 The Object class; Polymorphism read
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 16 (A) Searching and Comparable
COMPUTER 2430 Object Oriented Programming and Data Structures I
Building Java Programs
COMPUTER 2430 Object Oriented Programming and Data Structures I
Binary Search Trees continued; Tree Sets
Lecture 18: Polymorphism (Part II)
Searching.
CSE 373 Objects in Collections: Object; equals; compareTo; mutability
CSE 143 Lecture 3 Implementing ArrayIntList reading:
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
Special instance methods: toString
CSE 373 Data Structures and Algorithms
CSE 143 Lecture 23 Polymorphism; the Object class read
TCSS 143, Autumn 2004 Lecture Notes
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 8 Iterators; Comparable reading: 11.2; 10.2
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
slides adapted from Marty Stepp
CSE 373: Data Structures and Algorithms
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Implementing ArrayIntList
CSE 373: Data Structures and Algorithms
Building Java Programs
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

slides adapted from Marty Stepp and Hélène Martin CSE 143 Section 14 Comparable slides adapted from Marty Stepp and Hélène Martin http://www.cs.washington.edu/143/

Ordering our own types We cannot binary search or make a TreeSet/Map of arbitrary types, because Java doesn't know how to order the elements. The program compiles but crashes when we run it. Set<Note> tags = new TreeSet<Note>(); tags.add(new Note(1.5, Pitch.A, 4, Accidental.Natural, false)); tags.add(new Note(0.6, Pitch.C, 4, Accidental.Natural, true)); ... Exception in thread "main" java.lang.ClassCastException at java.util.TreeSet.add(TreeSet.java:238)

Comparable (10.2) public interface Comparable<E> { public int compareTo(E other); } A class can implement the Comparable interface to define a natural ordering function for its objects. A call to your compareTo method should return: a value < 0 if this object comes "before" the other object, a value > 0 if this object comes "after" the other object, or 0 if this object is considered "equal" to the other. If you want multiple orderings, use a Comparator instead (see Ch. 13.1)

Comparable template public class name implements Comparable<name> { ... public int compareTo(name other) { }

Comparable example public class Point implements Comparable<Point> { private int x; private int y; ... // sort by x and break ties by y public int compareTo(Point other) { if (x < other.x) { return -1; } else if (x > other.x) { return 1; } else if (y < other.y) { return -1; // same x, smaller y } else if (y > other.y) { return 1; // same x, larger y } else { return 0; // same x and same y }

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 } else { return y - other.y; // same x; compare y } The idea: if x > other.x, then x - other.x > 0 if x < other.x, then x - other.x < 0 if x == other.x, then x - other.x == 0 NOTE: This trick doesn't work for doubles (but see Math.signum)

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()); } toString trick - If your object's toString representation is related to the ordering, use that to help you: // sort by date, e.g. "09/19" > "04/01" public int compareTo(Date other) { return toString().compareTo(other.toString());