|
|||||||
|
|
![]() |
|
|
|
|
||
|
Comparing Objects in Java Hey, David: I hope you can help me with a compare problem in Java. I have users enter a search variable (customer number 8 characters) in a jtextfield to search for a customer record. I then pass this to another Java program as a string called compare, where it loses any blanks padding ("0390 ") and becomes ("0390"). The customer number field on the AS/400 file is 8 char and appears to retain this size when I pass it to a string for comparison using cncust = (String) record.getField("CNCUST"). I use (cncust.equals(this.compare)) to make the comparison. How do I make these two fields look the same for comparison? The compare field thinks it is 4 long, and the cncust field thinks it is 8 long. I appreciate your help, and hope you understand this. --Darrell You bring up an interesting question that I will try to answer in two parts. First, I will show you how to use the String class trim() method to solve your issue, and then I will show you how to use the Comparitor interface to create classes that allow you to specify the rules used to determine equality. This second technique comes in handy when comparing objects that are stored in some form of Collection or Map. The following code demonstrates comparison using the trim method to remove trailing blanks, so that varying length strings compare equally:
JTextField jTextField = new JTextField(8);
// Some processing occurs to set jTextField
String compare = jTextField.getText();
// Retrieve database value
cncust = (String) record.getField("CNCUST"));
// Perform comparison
if (compare.trim().equals(cncust.trim())) {
// equal
} else {
// unequal
}
To create a loosely comparable object that ignores length and is case-insensitive, you override the equals method for an object. In most cases, equal objects should sort equally, so you should also implement the Comparable interface. The Comparable interface provides a natural ordering for sorted Sets and Maps. For this example, I created a LooselyComparableString class that implements Comparable. Classes that implement the Comparable interface must contain a compareTo(object) method that compares the current object (this) with the passed object and returns an integer value of 0 for equality, -1 if the current object is less than the passed object, and 1 if the current object is greater than the passed object. LooselyComparableString objects ignore case and length when determining equality or sort order. The main method in LooselyComparableString provides a few tests that demonstrate how this class might be used. Click here to view the code. Running LooselyComparableString's main method produces the following output: The loosely comparable strings are equal. The first string is less than the second string. String "String" added to ArrayList. The ArrayList contains the string "string ". There is one other class that you may need to understand when working with Collections. That class is java.util.Comparitor, which imposes total ordering on a collection of objects. You use Comparitor to provide alternate sort ordering. Classes that implement Comparitor must have a compare(object1, object2) method that receives two objects. When object1 is equal to object2, compare returns equal. When object1 is less than object2, compare returns -1. And when object1 is greater than object2, compare returns 1. This should get you through your comparison dilemma and help you better understand how Java allows you to override behavior, allowing you to extend Java's built-in functionality. --David
|
Editors
Contact the Editors |
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |