File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes duplicate/unique value count generics list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "duplicate/unique value count generics list" Watch "duplicate/unique value count generics list" New topic

duplicate/unique value count generics list

Udayan Kumar
Ranch Hand

Joined: Jan 16, 2007
Posts: 66
Hi there,

I have a scenario where in I would need to get a count of duplicate objects and unique objects from a generics list based on attribute values of the object in the generics list.
The generics list collection comprises of this custom java object.
Class Apartment {
int aptNo; int lcnNo;
Based on the above instance attribute values I have to find count of duplicates/uniques in the generics collection.
All duplicates (based on value of aptNo)
All uniques (based on value of lcnNo)

Let me know your thoughts on the same.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

I don't think your question is very clear...

you have some kind of list that holds Apartment objects, I take it. When you say "duplicate objects", are you talking about java objects on the heap, or do you mean you want to know if two Apartment objects are functionally equal?

This List has the same object duplicated in the list:

Apartment A = new Apartment();

This has two different java objects, but they are for all intents and purposes equal

Apartment A = new Apartment(101, 11);
Apartment B = new Apartment(101, 11);

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Udayan Kumar
Ranch Hand

Joined: Jan 16, 2007
Posts: 66
I mean functionally equal. The problem I am facing is with override of the equals method.
Basically I am looking at creating a HashSet from this List so that I can skip the duplicates and get the unique count.
My intent here is to get a count of Apartment objects with unique lcnNo values.
So say if I use the lcnNo as differentiator in the equals and hashcode method and when I convert the aforementioned list to a HashSet I get all the Apartment objects with unique lcnNo. This works.

But there is another requirement where in I need to get all objects with unique aptNo value.

Is there any other way to grab such "functionally" unique objects w/o resorting to equals/hashcode differentiator for the same.

Udayan Kumar
Ranch Hand

Joined: Jan 16, 2007
Posts: 66
Can someone provide help or pointers on how can i resolve the same

Thanks in Advance,
wood burning stoves
subject: duplicate/unique value count generics list
It's not a secret anymore!