Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading from a Collection

 
Chris Donald
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks
Chris
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65113
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Chris Donald
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear for your reply.What I meant to say was the following:

I have a method like shown below:

public boolean isSameRank(Card cardA,Card cardB,Card cardC,Card cardD,Card cardE){

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

return false;
}

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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,I did not use collections.I made a mistake mentioning it.

Thanks
Chris
 
Sam Aran
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Please advise.

Thanks Chris
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

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:
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I'd start at 1 in this case.
 
Nina Milo
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
****************
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.


Thanks.

***************************************************************


********************************************

[ September 08, 2008: Message edited by: Nina Milo ]

[ September 08, 2008: Message edited by: Nina Milo ]
[edit]Add code tags. CR[/edit]
[ September 08, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49764
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your response and help.

Regards
Chris
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic