Hey Folks, I have a collection of 5 Java Beans(eg:Card Bean).One of the properties of the Bean is rank.What is the best way to check if there are 3 bean instances that have the same rank?I know that one way of doing this is to loop through the collection

for eg: if((cardA.getRank()==cardB.getRank())&&(cardB.getRank()==cardC.getRank())&&(cardC.getRank()==cardD.getRank())&&(cardD.getRank()==cardE.getRank())){ return true; }

The above is only one combination.There will be others like comparing cardA to cardC or cardD or cardE.Is there a more elegant way of checking if there are atleast 3 cards have the same rank?

Originally posted by Chris Donald: I know that one way of doing this is to loop through the collection...

But your example does no looping. You just have a set number of instance hard-coded into an if. How could you write a true loop that would be independent of the number of items in the collection?

The goal here is to find out if there are atleast 3 cards here with the same rank.Instead of adding more AND OR Clauses to the code shown above,is there a better way of doing things.

Please advise.

Thanks Chris

Chris Donald
Greenhorn

Joined: Jul 15, 2008
Posts: 23

posted

0

Also,I did not use collections.I made a mistake mentioning it.

I don't know what "rank" is, so I'm going to use a general collection for this, but you'll need to use a specific one for "rank" if you want to get rid of the inevitable warning.

I'm assuming your cards *are* in a collection. Since I'm using general collections, I'll use a standard for loop, if you use generics, you can convert it to enhanced.

If three or more cards have the same rank, a rank entry will be inserted for the first one, but not the two duplicates, so you compare it to the total number of possible choices (independent ranks) minus the two duplicates. You needed "at least", so if it is less than that (for instance, there are 4 cards with the same rank), the set will have less and this will be true.

Chris Donald
Greenhorn

Joined: Jul 15, 2008
Posts: 23

posted

0

Thanks for your reply.The HashSet is useful.I have another question I don't have an answer to.Lets say that I have a Collection of 10 numbers.Is there a nice way to find out the number of occurences of a particulat number in the collection?For ex: number of times the numbers 5,4 and 7 occur in the collection.I know that we could use a counter to do so.But I am wondering if there is a better way of doing so?

a more general way of doing this would be to group the cards by rank. I assume that you have constants for the ranks, like Card.ACE = 0, Card.TWO = 1, Card.THREE = 2, ..., Card.KING = 12. I know the numbers are a little un-intuitive, but it's the (zero-based) way Java is, just as with Calendar.JANUARY = 0 ... Now let's group the cards:

Originally posted by Guido Sautter: [QB]a more general way of doing this would be to group the cards by rank. I assume that you have constants for the ranks, like Card.ACE = 0, Card.TWO = 1, Card.THREE = 2, ..., Card.KING = 12. I know the numbers are a little un-intuitive, but it's the (zero-based) way Java is, just as with Calendar.JANUARY = 0 ...

And Calendar.SUNDAY = 1

It doesn't really matter what value you give them; you could start at 481 if you like. That's what you are naming them for.

**************** Lets say that I have a Collection of 10 numbers. Is there a nice way to find out the number of occurences of a particulat number in the collection?For ex: number of times the numbers 5,4 and 7 occur in the collection.I know that we could use a counter to do so.But I am wondering if there is a better way of doing so? *****************

Chris, I have a simple solution to find the occurence of a single number within the collection as you requested without using the counters. You can create a simple collection of 10 numbers as you wish and try using this code snippet to find the occurenece of single number.

Nina Milo, please find the code button; I have edited your post to show how much better it is to read.

What does that method do? It looks very convoluted.

Chris Donald
Greenhorn

Joined: Jul 15, 2008
Posts: 23

posted

0

Thank you all for your response and help.

Regards Chris

Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142

posted

0

Hi Rob,

Originally posted by Rob Prime:

And Calendar.SUNDAY = 1

It doesn't really matter what value you give them; you could start at 481 if you like. That's what you are naming them for.

I'd start at 1 in this case.

surely vou can sart at 481, but then you would have to do adjustment computations in order to use the constants as array indices ... that's why I proposed starting with Card.ACE = 0 ... Otherwise, you'd hav to subtract Card.ACE all the time, and still asume that the card rank constants are subsequent, distinct integers of which Card.ACE is the smalest ...

Don't get me started about those stupid light bulbs.