Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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

 
Saloon Keeper
Posts: 4505
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well spotted! Missed that.
 
Saloon Keeper
Posts: 8229
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
I merged your stuff with the following thread. I hope that is okay by you.
 
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following problem:

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 implement a method that returns a stream as follows:
- every cards is mapped to its ranking (2 of any rank -> 1, 3 of any rank -> 2, ...
T of any rank -> 9, ... A of any rank -> 13)
- the ranks are sorted ascending
- the function calls the previous method to print the ranks
(the Stream received as parameter contains cards in the same format as before)
E.g. For a stream containing "3S, "TC", "AH" the function should return "2 9 13"
 
Piet Souris
Saloon Keeper
Posts: 4505
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use the way that you used before: have a List<Character> like {0, 2, 3, 4, ..., T, ...} and use the indexOf for the rank. But I would prefer to use a Map<rank, value> for that. Then if you have a List of cards, map each card to its value in the Stream, sort it, and you are done.
 
Carey Brown
Saloon Keeper
Posts: 8229
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
You already have a rankOrder() method that is zero based, just add one to the returned int.
 
Marshal
Posts: 72956
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a Card class yet? We know that would be a more object‑oriented solution. If you use the format in the JLS link I showed you a few days ago, you can do something naughty. You can call ordinal() on each of the Rank objects. If you look at that link you will find that ordinal() is not something most people call, but in this case it seems to do what you want.
By the way: ordinal() returns 0‑based numbers, so you may wish to add 1.

Or you can follow Carey's suggestion and add 1 to your existing method's result. Do you really need 1‑based numbers?
 
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
[Hi, I didn't make a class, I tried to keep the structure as it is;
I try not to complicate myself with the new class.
0 based numers...
 
Campbell Ritchie
Marshal
Posts: 72956
330
  • 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 try not to complicate myself with the new class.
. . .

But things are getting more complicated without the class.Each Card object knows its rank and suit numbers, so those methods are really easy to write. It might be more difficult if one of the suits is trumps however.
 
Bartender
Posts: 2864
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a bit late to the party But I found this thread interesting.
My thought process usually works around enums for such types of problems. Even if you get inputs as Strings. You can have helper methods to convert those strings to objects. Here's how I would create the data structures ...

Enum for Suits:
Enum for Ranks:
Class for Card:Example usage based on strings: Once well defined data structures are in place, you can do any logic you want that fits your requirements. I'll leave that to you

 
Campbell Ritchie
Marshal
Posts: 72956
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:I am a bit late to the party But I found this thread interesting. . . .

Bad luck, it's already appeared in the Journal once Oh, it isn't your turn anyway.

I like the use of the Map, which I think isn't really different from my suggestion, but much more complete. I only write five lines with code in  You can use other keys to get the same values; you can readily map ♣♦♥♠ to suits too. Their Unicode numbers are U+2663, U+2666; U+2665, and U+2660; it is easy enough to convert those numbers to \u1234 format.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic