• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Return a distinct & sorted Stream

 
Greenhorn
Posts: 14
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


We consider a deck of playing cards in the following format:

Ranks: 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A

Suits: S - Spade, D - Diamond, H - Heart, C - Club

Every card will be identified by 2 characters
E.g. "3S" (3 of Spades), "TC" (10 of Club), "AH" (Ace of Heart).

I want to modify the getCardsBySuit function in order to return a Stream with all the cards belonging to that suit.

E.g. for a list that contains "2H", "3S", "TH" and the suit is 'H', the result should be a stream containing "2H" and "TH"

 
Saloon Keeper
Posts: 8237
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 73012
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you using Strings instead of cards? Create a Card class; there is an example in the Java® Language Specification (=JLS: read the last part of that section). Note that example uses enums, so there will be exactly 52 different Cards. No risk of writing "0X" or anything else strange. You can now use a Stream<Card>. If you have a Hand or Pack (Deck in America) class with 13 or 52 Cards respectively, you can make those classes implement a stream() method and filter those Streams just as Carey showed.You can do the same with the Pack/Deck class. The ordering might then be different. The JLS section tells you somewhere that it is permissible to use the same object operator == on enums.
 
Campbell Ritchie
Marshal
Posts: 73012
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
mircea stefanescu
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I modify getCardsByRank function in order to return a Stream
with all the DISTINCT cards of that rank,
alphabetically SORTED ?

E.g. for a list that contains "2H", "2S", "TH" and the rank is '2'
the result should be a stream containing "2H" and "2S" in this order.

I have tried this:




but it did not work...
 
Sheriff
Posts: 16278
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this: "It doesn't work" is useless.

Instead, please TellTheDetails (←click that, it's a link)
 
Campbell Ritchie
Marshal
Posts: 73012
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us what went wrong. The rank isn't at index 1; it is index 0.
This is the problem with using Strings, as I said in your other thread. If you used the same technique as in the JLS, you would have exactly 52 Card objects and there would be no need to use distinct(). Note the example in the JLS uses the suits in the order conventionally used in Bridge: ♣<<<♠. It just so happens that the English names of the suits are in the same order alphabetically, but in other languages you would need to create a Comparator<Card> by suit name. In German the four suits are ♣=Kreuz, =Karo, =Herz, and ♠=Pik.
 
mircea stefanescu
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please tell us what went wrong. The rank isn't at index 1; it is index 0.
This is the problem with using Strings, as I said in your other thread. If you used the same technique as in the JLS, you would have exactly 52 Card objects and there would be no need to use distinct(). Note the example in the JLS uses the suits in the order conventionally used in Bridge: ♣<<<♠. It just so happens that the English names of the suits are in the same order alphabetically, but in other languages you would need to create a Comparator<Card> by suit name. In German the four suits are ♣=Kreuz, =Karo, =Herz, and ♠=Pik.




I understand what you said, but these are the conditions under which I want to implement the method
 
Campbell Ritchie
Marshal
Posts: 73012
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . . The rank isn't at index 1; it is index 0. . . .

Using charAt(...) suggests to me that you aren't using a properly object‑oriented design, If you had a Card class, you would use its getRank() method and there would be no risk of that sort of confusion. I can't quite remember what sort of sorting the example in the JLS uses, but it should be obvious if you read its code. I think that comparison technique would be compatible with your current requirements.
 
Campbell Ritchie
Marshal
Posts: 73012
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mircea stefanescu wrote:. . . these are the conditions under which I want to implement the method

Those conditions, I am afraid, are by no means ideal. I hope nobody has tried to tell you that is the best way to implement Cards.

And please tell the people on SO that you have asked the same question on two fora.
 
mircea stefanescu
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you , I succeeded
 
Campbell Ritchie
Marshal
Posts: 73012
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done Please tell them on SO, too.
 
Campbell Ritchie
Marshal
Posts: 73012
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
mircea stefanescu
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to modify the getCardsLowerThan function in order to return a Stream with all the cards that are ranked lower than the rank received as parameter ?

I tried this but it's not the right code

 
Carey Brown
Saloon Keeper
Posts: 8237
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edit: I had missed the "lower than" criteria.

When you show code that doesn't work, explain in what way isn't it working. Perhaps show output.

Please don't keep making new topics for expansions of an existing topic.
 
Carey Brown
Saloon Keeper
Posts: 8237
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorting by rank.

 
mircea stefanescu
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Sorting by rank.



Hi, both codes are good but the idea is that I want them to show me the first 3 values ​​in the list.
How can I do that?
 
mircea stefanescu
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mircea stefanescu wrote:

Carey Brown wrote:Sorting by rank.



Hi, both codes are good but the idea is that I want them to show me the first 3 values ​​in the list.
How can I do that?



I found in the meantime,
I had to use .limit (3)
 
Campbell Ritchie
Marshal
Posts: 73012
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mircea stefanescu wrote:. . .
I had to use .limit (3)

Please use limit(3L) because the parameter taken is a long. Why do you have to reduce your Stream to a maximum of 3?
 
mircea stefanescu
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

mircea stefanescu wrote:. . .
I had to use .limit (3)

Please use limit(3L) because the parameter taken is a long. Why do you have to reduce your Stream to a maximum of 3?



There are some requirements that I am trying to solve in an attempt to become a better Java programmer.
 
Carey Brown
Saloon Keeper
Posts: 8237
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helper methods make it much more readable.
You really should consider using enums Rank and Suite instead of chars as suggested in one of your other threads.
 
mircea stefanescu
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now, I want to modify the getCardsLowerThanSorted function in order to return a Stream
with all the cards that are ranked lower than the rank received as parameter

Additionally they should be sorted in the following manner:
- first all the cards of Spade by rank ascending
- then all the cards of Diamond by rank ascending
- then all the cards of Heart by rank ascending
- finally all the cards of Club by rank ascending

 
Carey Brown
Saloon Keeper
Posts: 8237
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Similarly, make a Comparator "orderBySuitAndRank".
 
Campbell Ritchie
Marshal
Posts: 73012
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Card class in the JLS which I keep going on about implements Comparable (at least I think it does). Now, as some people already know, my opinion is that Comparable should be reserved for types with only one single ordering type. Cards obviously have several orderings, so you can sort and get four aces or four spades. But maybe we shouldn't expect such good design in a simple JLS example. You can sort like this and you don't have too much trouble with compareTo() because the enums already have the features in the correct order.There are problems with simply using chars because a simple comparison will put Queen, King, Ace in the wrong order. Carey's rankOrder() method corrects that.
Note you will get different results if you swap the order of the limit() and filter() calls.
 
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP

your method has become very specialised. As an exercise, can you give that method a Predicate argument, and possibly a Comparator?
 
Campbell Ritchie
Marshal
Posts: 73012
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody suggested yesterday that your topics were so similar they ought to be merged into one thread; I think they were correct and have merged the topics
 
Campbell Ritchie
Marshal
Posts: 73012
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations Have a cow for having this thread quoted in the May 2021 JavaRanch Journal.
 
Carey Brown
Saloon Keeper
Posts: 8237
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Similarly, make a Comparator "orderBySuitAndRank".



 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way to write this:

 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I often use this way too to define a compareTo method:
 
Carey Brown
Saloon Keeper
Posts: 8237
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mircea has yet to tell us what class we are designing so, so far, everything's been static. A "Card" class would be a good place to start.
 
mircea stefanescu
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Mircea has yet to tell us what class we are designing so, so far, everything's been static. A "Card" class would be a good place to start.



My design is as follows:

I have a functional interface:



and a Utils class in which I implement different methods, anonymous classes.
So far I have this:



Now I'm struggling with the getCardsLowerThanSorted method, which I still haven't been able to implement properly
 
mircea stefanescu
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mircea stefanescu wrote:

Carey Brown wrote:Mircea has yet to tell us what class we are designing so, so far, everything's been static. A "Card" class would be a good place to start.



My design is as follows:

I have a functional interface:



and a Utils class in which I implement different methods, anonymous classes.
So far I have this:



Now I'm struggling with the getCardsLowerThanSorted method, which I still haven't been able to implement properly



Here I display the results in main:

 
mircea stefanescu
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mircea stefanescu wrote:

mircea stefanescu wrote:

Carey Brown wrote:Mircea has yet to tell us what class we are designing so, so far, everything's been static. A "Card" class would be a good place to start.



My design is as follows:

I have a functional interface:



and a Utils class in which I implement different methods, anonymous classes.
So far I have this:



Now I'm struggling with the getCardsLowerThanSorted method, which I still haven't been able to implement properly



Here I display the results in main:



I made the following changes ...



but I still can't sort them out properly
 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that in your method

you first sort by Rank and next, sort by Card, and that last sort may ruin the first sort.

Carey showed you a combined Comparator "orderBySuitAndRank". Use that instead of the two sorts that you are doing now.
 
Campbell Ritchie
Marshal
Posts: 73012
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree a Comparator is better, because Cards don't have a single ordering; even the suits may change order if you play bridge or whist where one suit is Trumps. But as you asked here, and as I replied only a few minutes ago, sorting an ordered Stream is “stable”.
 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, I would have thought that the two sorts would give a good result myself (in this case). But OP is still complaining about a failure. Maybe that is because the sorting base for the ranks is final String order = "23456789TJKQA";"with K and Q exchanged?
 
Carey Brown
Saloon Keeper
Posts: 8237
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mircea stefanescu wrote:I made the following changes ...


but I still can't sort them out properly

You have both rank and suit looking at charAt(0). One of them is wrong.
 
Legend has it that if you rub the right tiny ad, a genie comes out.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic