151: COMPARING OBJECTS in Java

Generation Quick Response Code in Java 151: COMPARING OBJECTS
151: COMPARING OBJECTS
QR Code 2d Barcode Creation In Java
Using Barcode generation for Java Control to generate, create Quick Response Code image in Java applications.
Example 1510 Natural Ordering and Total Ordering import javautilComparator; import javautilSet; import javautilTreeSet; public class ComparatorUsage { public static void main(String[] args) { // Choice of comparator Set<String> strSet = new TreeSet<String>(); Set<String> strSet = new TreeSet<String>(StringCASE_INSENSITIVE_ORDER); Set<String> strSet = new TreeSet<String>(new RhymingStringComparator()); // Add each command line argument to the set for (String argument : args) { strSetadd(argument); } Systemoutprintln(strSet); } } class RhymingStringComparator implements Comparator<String> { public int compare(String obj1, String obj2) { // (5) Create reversed versions of the strings: String reverseStr1 = new StringBuilder(obj1)reverse()toString(); String reverseStr2 = new StringBuilder(obj2)reverse()toString(); // Compare the reversed strings lexicographically return reverseStr1compareTo(reverseStr2); } }
Make Barcode In Java
Using Barcode printer for Java Control to generate, create bar code image in Java applications.
// // //
Bar Code Recognizer In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
// (1a) // (1b) // (1c)
Denso QR Bar Code Generator In C#.NET
Using Barcode creator for .NET framework Control to generate, create QR-Code image in VS .NET applications.
// (2)
Draw QR In .NET
Using Barcode printer for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications.
// (3)
Printing QR Code In VS .NET
Using Barcode drawer for VS .NET Control to generate, create Denso QR Bar Code image in .NET applications.
// (4)
QR Maker In VB.NET
Using Barcode generation for .NET Control to generate, create QR Code JIS X 0510 image in Visual Studio .NET applications.
// (6)
Printing UPC-A Supplement 2 In Java
Using Barcode generator for Java Control to generate, create UPC A image in Java applications.
The program is run with the following program arguments on the command line:
EAN13 Generation In Java
Using Barcode creator for Java Control to generate, create UPC - 13 image in Java applications.
>java ComparatorUsage court Stuart report Resort assort support transport distort
Make Bar Code In Java
Using Barcode generator for Java Control to generate, create bar code image in Java applications.
Output from the program using the natural ordering (1a):
Bar Code Creation In Java
Using Barcode encoder for Java Control to generate, create bar code image in Java applications.
[Resort, Stuart, assort, court, distort, report, support, transport]
Painting Barcode In Java
Using Barcode drawer for Java Control to generate, create bar code image in Java applications.
Output from the program using the case insensitive ordering (1b):
UPC - E0 Generation In Java
Using Barcode drawer for Java Control to generate, create UPC-E Supplement 5 image in Java applications.
[assort, court, distort, report, Resort, Stuart, support, transport]
Making Code 128A In VB.NET
Using Barcode generator for .NET framework Control to generate, create Code 128C image in VS .NET applications.
Output from the program using the rhyming ordering (1c):
Making Code-39 In Visual C#.NET
Using Barcode drawer for VS .NET Control to generate, create USS Code 39 image in .NET applications.
[Stuart, report, support, transport, Resort, assort, distort, court]
Read EAN13 In Visual Studio .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET framework applications.
Example 1511 illustrates using a comparator that orders version numbers (Example 158) according to their reverse natural ordering Such a comparator is implemented as an anonymous class by the method reverseComparatorVNO() at (1) The
Encode Code 128B In Visual C#.NET
Using Barcode generation for .NET Control to generate, create ANSI/AIM Code 128 image in Visual Studio .NET applications.
CHAPTER 15: COLLECTIONS AND MAPS
Print Data Matrix In .NET Framework
Using Barcode generator for Visual Studio .NET Control to generate, create ECC200 image in .NET framework applications.
comparator leverages on the compareTo() method implemented by the VersionNumber class
Printing EAN128 In VS .NET
Using Barcode creator for .NET framework Control to generate, create EAN128 image in .NET applications.
A list of version numbers is created at (3), that is backed by the array created at (2) This list is sorted using the comparator at (4) A binary search is done in this list at (5) We have used the same comparator for the search as we did for the sorting in order to obtain predictable results
Draw Barcode In VS .NET
Using Barcode creator for ASP.NET Control to generate, create barcode image in ASP.NET applications.
Example 1511 Using a Comparator for Version Numbers import static javalangSystemout; import import import import javautilArrays; javautilCollections; javautilComparator; javautilList;
public class UsingVersionNumberComparator { /** Comparator for reverse natural ordering of natural numbers */ public static Comparator<VersionNumber> reverseComparatorVNO() { // (1) return new Comparator<VersionNumber>() { public int compare(VersionNumber vno1, VersionNumber vno2) { return vno2compareTo(vno1); // Comparing vno2 with vno1 } }; } public static void main(String[] args) { VersionNumber[] versions = new VersionNumber[] { // (2) new VersionNumber(3, 49, 1), new VersionNumber(8, 19, 81), new VersionNumber(2, 48, 28), new VersionNumber(10, 23, 78), new VersionNumber(9, 1, 1) }; List<VersionNumber> vnList = ArraysasList(versions); // (3) outprintln("List before sorting:\n " + vnList); Collectionssort(vnList, reverseComparatorVNO()); // (4) outprintln("List after sorting according to " + "reverse natural ordering:\n " + vnList); VersionNumber searchKey = new VersionNumber(9, 1, 1); int resultIndex = CollectionsbinarySearch(vnList, searchKey, reverseComparatorVNO()); // (5) outprintln("Binary search in list using reverse natural ordering" + " found key " + searchKey + " at index: " + resultIndex); } }
Program output:
List before sorting: [(3491), (81981), (24828), (102378), (911)] List after sorting according to reverse natural ordering: [(102378), (911), (81981), (3491), (24828)] Binary search in list using reverse natural ordering found key (911) at index: 1
151: COMPARING OBJECTS
Review Questions
151 Which statements about the hashCode() and equals() methods are true Select the two correct answers (a) Two objects that are different according to the equals() method, must have different hash values (b) Two objects that are equal according to the equals() method, must have the same hash value (c) Two objects that have the same hash value, must be equal according to the equals() method (d) Two objects that have different hash values, must be unequal according to the equals() method 152 Given that the objects referenced by the parameters override the equals() and the hashCode() methods appropriately, which return values are possible from the following method
String func(Object x, Object y) { return (x == y) + " " + xequals(y) + " " + (xhashCode() == yhashCode()); }
Select the four correct answers (a) "false false false" (b) "false false true" (c) "false true false" (d) "false true true" (e) "true false false" (f) "true false true" (g) "true true false" (h) "true true true" 153 Which code, when inserted at (1), in the equalsImpl() method will provide a correct implementation of the equals() method
public class Pair { int a, b; public Pair(int a, int b) { thisa = a; thisb = b; } public boolean equals(Object o) { return (this == o) || (o instanceof Pair) && equalsImpl((Pair) o); } private boolean equalsImpl(Pair o) { // (1) INSERT CODE HERE } }