Computer Science 209 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Computer Science 209

Description:

set.add('Carrie'); // Can use an iterator to access in order ... list.add('Carrie'); Collections.sort(list); // Uses compareTo to sort the list ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 16
Provided by: KenLa2
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 209


1
Computer Science 209
  • Using Interfaces
  • Comparisons

2
Ordering Objects
  • The comparison operators lt and gt cannot be used
    with objects, even if one could order them
  • Instead, we use the method compareTo, as
    specified in the ComparableltTgt interface
  • Any object whose class implements ComparableltTgt
    can be used in collections whose items are
    ordered

3
Examples Sorted Sets and Maps
SortedSetltStringgt set new TreeSetltStringgt() Sor
tedMapltString, Bookgt map new TreeMapltString,
Bookgt() set.add("Bill") set.add("Ann") set.add
("Carrie") // Can use an iterator to access in
order Book b1 new Book("Silence of the Lambs",
"Harris") Book b2 new Book("Extreme
Programming", "Beck") map.put("Silence of the
Lambs", b1) map.put("Extreme Programming", b2)
String implements ComparableltStringgt, so items
can be added to sorted sets and maps and
retrieved in sorted order.
4
Examples Lists
ListltStringgt list new ArrayListltStringgt() list
.add("Bill") // Items added in random
order list.add("Ann") list.add("Carrie") Collect
ions.sort(list) // Uses compareTo to sort the
list Book b new Book("Silence of the Lambs",
"Harris") Collections.sort(list) list.clear() l
ist.add(b) // Syntax error
Collections.sort uses compareTo to sort a list,
so it assumes that the items implement
ComparableltTgt
5
Comparable and compareTo
public interface ComparableltTgt // Returns 0
if receiver equals other // Returns lt 0 if
receiver is less than other // Returns gt 0 if
receiver is greater than other public int
compareTo(T other)
public class Book implements ComparableltBookgt
public int compareTo(Book other) return
title.compareTo(other.getTitle())
Books can now be used in contexts wherein they
are ordered by title.
6
Multiple Orderings
  • A client might ask for a list of books sorted by
    title or another list of books sorted by author
  • compareTo provides just one ordering
  • Use a ComparatorltTgt object for each distinct
    ordering

7
Using a Comparator
public static ltTgt void sort(ListltTgt list,
Comparatorlt? super Tgt c)
Another Collections.sort method expects an object
whose class implements the ComparatorltTgt
interface. The sort method uses the comparator
objects compare method to sort the list.
8
Using a Comparator
ListltBookgt list new ArrayListltBookgt() Book b1
new Book("Silence of the Lambs",
"Harris") Book b2 new Book("Extreme
Programming", "Beck") list.add(
b1) list.add(b2) Collections.sort(list, new
BookComparatorByTitle()) Collections.sort(list,
new BookComparatorByAuthor())
Each comparator object specifies a distinct
ordering for comparisons.
9
ComparatorltTgt and compare
public interface ComparatorltTgt // Returns 0
if obj1 equals obj2 // Returns lt 0 if obj1 is
less than obj2 // Returns gt 0 if obj1 is
greater than obj2 public int compare(T obj1,
T obj2)
import java.util.Comparator public class
BookComparatorByAuthor implements
ComparatorltBookgt public int compare(Book
obj1, Book obj2) String author1
obj1.getAuthor() String author2
obj2.getAuthor() return author1.compareTo(a
uthor2)
10
ComparatorltTgt and compare
public interface ComparatorltTgt // Returns 0
if obj1 equals obj2 // Returns lt 0 if obj1 is
less than obj2 // Returns gt 0 if obj1 is
greater than obj2 public int compare(T obj1,
T obj2)
import java.util.Comparator public class
BookComparatorByTitle implements
ComparatorltBookgt public int compare(Book
obj1, Book obj2) return obj1.compareTo(obj2
)
11
Using Comparators
  • We need a new comparator class for each new
    possible ordering
  • We might need to use each comparator just once in
    our code
  • Lots of new named classes add clutter to our
    system

12
Anonymous Classes
  • An anonymous class is a class without a name
  • Its usually defined at the point where its
    instantiated
  • An anonymous class is like inline code its
    written at the place where it needs to be used
    and there is just one such place

13
Format for Creating an Anonymous Class
new ltinterface namegt () ltimplementations of
methods required by the interfacegt
Creates an instance of an anonymous class that
can be used wherever an object of the interface
type is expected.
14
Example Compare by Author
ComparatorltBookgt comp1 new ComparatorltBookgt()
public int compare(Book obj1, Book obj2)
String author1 obj1.getAuthor() String
author2 obj2.getAuthor() return
author1.compareTo(author2) Collections.s
ort(list, comp1)
Create the comparator and assign it to a
variable. Then use the variable/comparator to
sort the list.
15
Example Compare by Title
Collections.sort(list, new ComparatorltBookgt()
public int compare(Book obj1, Book obj2)
String author1 obj1.getAuthor() String
author2 obj2.getAuthor() return
author1.compareTo(author2) )
You dont even need the extra variable. Just
create the comparator when its needed.
Write a Comment
User Comments (0)
About PowerShow.com