Intro: Searching

Class date: February 5

HW: JM Ch 14 1-4, pages 404-405

Warm up: Class meeting (see this posting)

Searching is a task involving a set of data elements represented in some way in memory. Each element includes a key that can be tested against a target value for an exact match.

There are three ways to do this:

  1. Comparing objects
    1. The equals method determines if two objects are the same or not (this video reviews some things you will remember from previous chapters, then introduce the equals method–it’s good to see how this method is different.) 
    2. compareTo     implements Comparable<> interface (interface review)
      1. There are two variants of this method. First method compares this String to another Object and second method compares two strings lexicographically.
      2. compareTo allows you to make comparisons between objects String compareTo review (referenced in video)
    3. compare aka a comparator 
      1. Compares objects to each other in their “natural” order
      2. The purpose of comparators is to be passed as parameters to constructors and methods of certain library classes (or your own). By creating different types of comparators, you can specify different ways of comparing objects of your class. You can create different comparators for ordering objects by different fields in ascending or descending order.

Leave a comment