![]() | Java Programming Comparing Objects | Exceptions![]() |
NavigateAggregate topic:() |
In Java, we can distinguish two kinds of equality:
If two variables are identical (equal in reference), they are logically equivalent (equal in value) too.
The==
operator can be used to check if two object references point to the same object.
![]() | Code section 5.19: Reference equality.if(objRef1==objRef2){// The two object references point to the same object} |
To be able to compare two Java objects of the same class, the
method must be overridden and implemented by the class.boolean
equals(Object
obj)
The implementor decides which values must be equal to consider two objects to be equal. For example in thebelow class, thename
and theaddress
must be equal but not thedescription
.
![]() | Code listing 5.5: Customer.javapublicclassCustomer{privateStringname;privateStringaddress;privateStringdescription;// ...publicbooleanequals(Objectobj){if(this==obj){returntrue;}elseif(obj==null){returnfalse;}elseif(objinstanceofCustomercust){if(((cust.getName()==null&&name==null)||cust.getName().equals(name))&&((cust.getAddress()==null&&address==null)||cust.getAddress().equals(address))){returntrue;}}returnfalse;}} |
After theequals
method is overriden, two objects from the same class can be compared like this:
![]() | Code section 5.20: Method usage.Customercust1=newCustomer();Customercust2=newCustomer();//...if(cust1.equals(cust2)){// Two Customers are equal, by name and address} |
Note that equal objectsmust have equal hash codes. Therefore, when overriding theequals
method, you must also override thehashCode
method. Failure to do so violates the general contract for thehashCode
method, and any classes that use the hash code, such asHashMap
will not function properly.
In Java, there are several existing methods that already sort objects from any class likeCollections.sort(List<T> list)
. However, Java needs to know the comparison rules between two objects. So when you define a new class and want the objects of your class to be sortable, you have to implement theComparable
and redefine thecompareTo(Object obj)
method.
int
compareTo(T o)Let's say that the name is more important than the address and the description is ignored.
![]() | Code listing 5.6: SortableCustomer.javapublicclassSortableCustomerimplementsComparable<SortableCustomer>{privateStringname;privateStringaddress;privateStringdescription;// ...publicintcompareTo(SortableCustomeranotherCustomer){if(name.compareTo(anotherCustomer.getName())==0){returnaddress.compareTo(anotherCustomer.getAddress());}else{returnname.compareTo(anotherCustomer.getName());}}} |
Objects that implement this interface can be used as keys in a sorted map or elements in a sorted set, without the need to specify a comparator.
The natural ordering for a class C is said to be consistent with equals if and only ife1.compareTo((Object) e2) == 0
has the same boolean value ase1.equals((Object) e2)
for every e1 and e2 of class C. Note that null is not an instance of any class, ande.compareTo(null)
should throw a NullPointerException even thoughe.equals(null)
returns false.
It is strongly recommended (though not required) that natural orderings be consistent with equals. This is because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method.
Sometimes we may want to change the ordering of a collection of objects from the same class. We may want to order descending or ascending order. We may want to sort byname
or byaddress
.
We need to create a class for each way of ordering. It has to implement theComparator
interface.
Since Java 5.0, theComparator
interface is generic; that means when you implement it, you can specify what type of objects your comparator can compare.
![]() | Code listing 5.7: CustomerComparator.javapublicclassCustomerComparatorimplementsComparator<Customer>{publicintcompare(Customercust1,Customercust2){returncust1.getName().compareTo(cust2.getName());}} |
The above class then can be associated with a SortedSet or other collections that support sorting.
![]() | Code section 5.21: Comparator usage.Collection<Customer>orderedCustomers=newTreeSet<Customer>(newCustomerComparator()); |
Using the Iterator theorderedCustomers
collection can be iterated in order of sorted byname
.
A List can be sorted by theCollections
'sort
method.
![]() | Code section 5.22: Customized comparison.java.util.Collections.sort(custList,newCustomerComparator()); |
Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator.
An array of objects can also be sorted with the help of aComparator
.
![]() | Code section 5.23: Array sorting.SortableCustomer[]customerArray;//...java.util.Arrays.sort(customerArray,newCustomerComparator()); |
Sorts the specified array ofCustomer
objects (customerArray) according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator.
![]() | To do: |
![]() | Java Programming Comparing Objects | Exceptions![]() |