aspose file tools*
The moose likes Other Open Source Projects and the fly likes Object Equality Google gwava and Apache Commons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Object Equality Google gwava and Apache Commons" Watch "Object Equality Google gwava and Apache Commons" New topic

Object Equality Google gwava and Apache Commons

Alexander Volkov

Joined: Sep 01, 2012
Posts: 8
Hello Guys

Trying to understand the results I am getting from object equality ...
Ok first let me describe the problem..

The is multiple i need to filter duplicates objects from two two tables from db (Hibenate all of the cluses are set up) preset condition is firstName, lastName, dateOfbirth. Not shure about best practices I have created an Class to hold a equality object

So in my service class I call Query on 2 tables and create custom list Iterate over list and objects

So as you can see I have implemented a an equality and wiz.addAll on the list here is the funny result

2012-09-01 18:49:09,198 INFO [com.equality.service.CopareToServices] - < Size from wizard 392> (thread 1008 invoke CopareToServices.compareObjects)

After itteration >>>>

2012-09-01 18:49:09,910 INFO [com.equality.service.CopareToServices] - <List tow size 391 List hash set size > (thread 1008 invoke CopareToServices.compareObjects)
Equality method only found a single duplicate record >>>

And this is after Implementing removeAll on the heshSet
2012-09-01 18:49:09,918 INFO [com.equality.service.CopareToServices] - <Added assignors to the list 216> (thread 1008 invoke CopareToServices.compareObjects)

171 Duplicates were removed which sound about right...

I have tried with Apache commons and guava equality implementation ... for equality

Why native removeAll on the list removes duplicates but the equality does not return true ???

Will appreciate any help I can get on this

Just found out That ound out that loop with in the loop it's like testing against the last element ...

What would be the proper way to itterate over two lists and perform equality check of an element agains other list ..

Harsha Ka
Ranch Hand

Joined: Mar 14, 2010
Posts: 45

If your requirement is just to have a a collection of distinct objects and not to perform any deletion to the tables, you may consider just adding all the elements from both the tables into a Set. By definition of Set, it will not have any duplicates and even if there are duplicates as you are overriding the equals method, there will be only one instance of the object.

SCJP 1.6,Preparing (Tryin to prepare) for scwcd
Alexander Volkov

Joined: Sep 01, 2012
Posts: 8
Thank you for you response Harsha Ka
Yes I did that while testing ..

But I need to preform an operation on the duplicate objects ...
Harsha Ka
Ranch Hand

Joined: Mar 14, 2010
Posts: 45

In that case, you might try to add the elements of first table to one HashSet and then loop over the second table and call HashSet.contains() to check if the set already contains the element.
I agree. Here's the link:
subject: Object Equality Google gwava and Apache Commons
Similar Threads
Casting Problem *Help Please
How to get sub lists of a list
Problem with refresh() method of EntityManamger in JPA
dynamic (?!) casting
String and Numeric Parameters