File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Reading from a Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading from a Collection" Watch "Reading from a Collection" New topic
Author

Reading from a Collection

Chris Donald
Greenhorn

Joined: Jul 15, 2008
Posts: 23
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

Joined: Jan 10, 2002
Posts: 61201
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chris Donald
Greenhorn

Joined: Jul 15, 2008
Posts: 23
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

Joined: Jul 15, 2008
Posts: 23
Also,I did not use collections.I made a mistake mentioning it.

Thanks
Chris
Sam Aran
Greenhorn

Joined: Sep 06, 2008
Posts: 12
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
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

Joined: Dec 22, 2004
Posts: 142
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nina Milo
Greenhorn

Joined: Jul 29, 2008
Posts: 19
****************
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

Joined: Oct 13, 2005
Posts: 38793
    
  23
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
Thank you all for your response and help.

Regards
Chris
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
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 ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading from a Collection