• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get more than 2 occurrence of objects with identical property value?  RSS feed

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method when invoked, it will return List of Bag instance objects (around 100 objects in a list). However, the list can contain identical bag data ie; they have same name and description properties.
I need to track the number of occurrence of each object and get bag objects with occurrence > 2.

is there an effective way to do this?

This is the bag object


 
Ranch Foreman
Posts: 3071
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you need to know equality for your Bag class then the first thing I would do is to override equals and hashcode.

The next thing (and this is off the top of my head, so there may be a better way to do it) I would do is probably write a simple method that returns a Map<Bag, Integer> and takes your List<Bag>. It's job is to count up the Bags and populate that Map.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also have Bag implement Comparable, then sort the list with Collections.sort(), and implement an algorithm similar to the one discussed in this thread, except your implementation will be based on a different condition. You'll only need one Bag object to use as the reference object while calculating the length of a run. You will update the reference Bag to have the current list element's attributes every time it is different, otherwise, you do what you need to do to track runs of 3 or more consecutive Bags that are equal to each other.
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. But its more harder now. There is a sellerName property inside Bag object and differs even if Bag object has same name and description properties.

I still need to get the occurrence of Bag object via name.

For example I have objects with following properties:
Bag1:
-name= Chanel
- description= Expensive Bag
- seller = Tom's Boutique

Bag2:
-name= Louis Vhitton
- description = Leather Bag
- seller = Sally's Shopping Mall

Bag3:
-name= Chanel
- description= Expensive Bag
- Sally's Shopping Mall
----------------------------------

How can I arrive from a result that
name= Chanel
count = 2
name = Louis Vhitton
count = 1

Can google guava do this?
Please somebody suggest please
 
Dave Tolls
Ranch Foreman
Posts: 3071
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I would argue the model is wrong.
"name" and "description" should probably be one class (possibly Bag), and the other has a Bag and the seller (possibly itself another class).

It all depends on what you're modelling.

That way, you equality makes sense.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a very specific problem and libraries like Guava are meant to address general problems. That's not to say that there's nothing in the Google code that you can use to help solve some parts of your problem, in particular com.google.common.base.Objects has methods that facilitate the implementation of equals and hashcode.

My advice:
1. Clearly define what Bag.equals() means -- what attributes are involved in determining equality of Bags?
2. Define some kind of ordering of Bags by having it implement Comparable, such that when you sort a collection of Bags, instances that are equal to each other will be grouped together.
3. Implement the algorithm I cited before to check a list for groups of similar items.

If you break the work down into these three things, the solution should not be that difficult to implement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!