John Le

Greenhorn
+ Follow
since Dec 18, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Le

Every once in awhile I come across an anonymous inner class, especially for comparators.
My question is why bother with this type of class? What do I get out of using it? Is the only reason to avoid a bunch of extra java files? or a bunch of extra classes in a single java file? Don't they all get compiled to class files? Even the anonymous inner class?

I just don't see the utility in using it.
Can anyone explain it to me? Or point me to an article that tells me why I should use them?

Thanks
15 years ago
That is pretty cool Steve. I'll have to dig into it a little deeper to get a better understanding of what you've done.

I've run a couple of quick tests against it and it has done exactly what I expected.

I'd swipe the whole thing from you except: 1) we're on 1.4, 2) I already checked in my inferior solution, and 3) I guess outright stealing your code is wrong.

Thanks for you help Steve!
15 years ago
I've looked around the system and it does seem to me that the equals method is incorrectly implemented.....however; I'm not allowed to fix it. The set idea is good for eliminating duplicates, but I need to find the duplicates and report the dups back to the user.

I've decided to just loop through the collection, I was just hoping that there was a 'cooler' way to do it.

Thanks for the reply,
John
15 years ago
I'm looking at a class with this method

public boolean equals(Object o){
if (o instanceof MyObject) {
MyObject other = (MyObject) o;

return new EqualsBuilder ()
.append(this.getPropA(), other.getPropA())
.append(this.getPropB(), other.getPropB())
.append(this.getPropC(), other.getPropC())
.isEquals();
}

return false;
}

And in another class I'm trying to do this

List duplicateList = (ArrayList) CollectionUtils.intersection(listA, listB);

All the lists are of type MyObject.

I've discoverd that the equals method does not satisfy my requirements. It turns out that I am comparing objects that were instantiated in different ways.

So for the objects to be equal for my purpose I would need this method:


public boolean equals(Object o){
if (o instanceof MyObject) {
MyObject other = (MyObject) o;

return new EqualsBuilder ()
.append(this.getPropX(), other.getPropX())
.append(this.getPropY(), other.getPropX())
.append(this.getPropZ(), other.getPropZ())
.isEquals();
}

return false;
}

I cannot extend the class ( the class is not final, I 'm just not allowed to), and I don't really want to loop through each list.

So can anyone point me to a way to 'pass in' my own equals method?

Thanks
15 years ago