One of the most confusing things for a Java developer is usage of Collection.contains(Object) if it is not used with appropriate understanding it can play havoc to the code.
Collection.contains(Object) accepts an Object, which means it essentially accepts an instance of any Java class
If one passes in an instance of a class other than the type of classes that can be stored in a particular collection, it might so happen that this method will simply return
false
. This isn't really
wrong because a different type than the collection holds is obviously
not part of that collection. However, it can be a wrong concept if the developer
relies on the returned value from the contains
call to implement other logic..contains is based on the object equality, so if there is no natural equality that can be performed on the objects then we need to override .equals() and preferably .hashcode() also, to be sure of uniqueness.
Comparators::
-----------------------
Consider an entity of fees as "Amount" for shopping on any site, as in multiple items are purchased for different prices and on different dates, considered those as different amounts due on different dates, so another entity as "Due Date".
Usage of Collections.sort(....) as : Collections.sort(listOfPrices, new PriceComparator());
<Comparator Code>
------------------------------------
public class PriceComparator implements Comparator<Price> {
public int compare(Price price1, Price price2) {
//Dual Comparison for the equality of the Price
//1. w.r.t >> Amount
//2. w.r.t >> Due Dates
int comparison=0;
comparison = price1.getAmount().compareTo( price2.getAmount());
if (comparison == 0)
{
//If Fees are equal then check for the due dates of the fees
try{
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy") ;
String dateString1 = sdf.format(price1.getDueDate() );
String dateString2 = sdf.format(price2.getDueDate() );
Date date1 = sdf.parse(dateString1);
Date date2 = sdf.parse(dateString2);
comparison = date1.compareTo(date2);
}
catch(ParseException ex){
ex.printStackTrace();
}
}
return comparison;
}
}
------------------------------ ------------------------------ ------------------------------ --------------------
// Need to override equals in the Price object if we want to set the equality of the Price Object to be found with amount and due date
// So in below if amount and due date are equal then it is the same price else not
// In above overridden compare method if we don't want to do logical handling of amount and due date comparison (i.e primitives comparison) we can just do Price1.compareTo(Price2) and override the equals method as below , this will set the equality of the Price object on both Amount as well as DueDate object.
Or Different way to look into this is if you want to do sorting of the Price Objects you would need to have overridden equals method setting the equality as below on amount and date both.
So when you call Collections.sort(listOfPriceObjects);, your sorting will work appropriately considering both Amount and Due Date.
------------------------------ ------------------------------ ------------------------------ -------------------
public boolean equals(Object object) {
if (object != null && object instanceof Price) {
Price obj = (Price) object;
if (amount == null) {
return (obj.amount == null);
}
else {
if(amount.equals(obj. getAmount()) && dueDate.equals(obj.getDueDate( ))){
return true;
}
}
}
return false;
}
-----------------------
Consider an entity of fees as "Amount" for shopping on any site, as in multiple items are purchased for different prices and on different dates, considered those as different amounts due on different dates, so another entity as "Due Date".
Usage of Collections.sort(....) as : Collections.sort(listOfPrices, new PriceComparator());
<Comparator Code>
------------------------------------
public class PriceComparator implements Comparator<Price> {
public int compare(Price price1, Price price2) {
//Dual Comparison for the equality of the Price
//1. w.r.t >> Amount
//2. w.r.t >> Due Dates
int comparison=0;
comparison = price1.getAmount().compareTo(
if (comparison == 0)
{
//If Fees are equal then check for the due dates of the fees
try{
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy")
String dateString1 = sdf.format(price1.getDueDate()
String dateString2 = sdf.format(price2.getDueDate()
Date date1 = sdf.parse(dateString1);
Date date2 = sdf.parse(dateString2);
comparison = date1.compareTo(date2);
}
catch(ParseException ex){
ex.printStackTrace();
}
}
return comparison;
}
}
------------------------------
// Need to override equals in the Price object if we want to set the equality of the Price Object to be found with amount and due date
// So in below if amount and due date are equal then it is the same price else not
// In above overridden compare method if we don't want to do logical handling of amount and due date comparison (i.e primitives comparison) we can just do Price1.compareTo(Price2) and override the equals method as below , this will set the equality of the Price object on both Amount as well as DueDate object.
Or Different way to look into this is if you want to do sorting of the Price Objects you would need to have overridden equals method setting the equality as below on amount and date both.
So when you call Collections.sort(listOfPriceObjects);, your sorting will work appropriately considering both Amount and Due Date.
------------------------------
public boolean equals(Object object) {
if (object != null && object instanceof Price) {
Price obj = (Price) object;
if (amount == null) {
return (obj.amount == null);
}
else {
if(amount.equals(obj.
return true;
}
}
}
return false;
}
No comments:
Post a Comment