Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

What I have tried:

I have tried this:

public static Stream<String> getCardsByRank(List<String> cards, char rank)
		{
			
			return cards.stream()
					.distinct()
                    .filter(s -> s.charAt(1) == rank)
                    .sorted();
		}


but it did not work...
Posted
Updated 31-May-21 5:56am
Comments
Richard MacCutchan 31-May-21 11:40am    
"but it did not work."
Please do not say that; it means nothing to us. Show the actual data you are using, and the results you get.
BZR1990 31-May-21 11:45am    
It means that when I call the method, it displays an empty list .... sorry if I was not understood, I am new to programming :)

1 solution

The card number is item 0 of the string not item 1.
Java
			return cards.stream()
					.distinct()
                    .filter(s -> s.charAt(0) == rank)
//                                        ^
                    .sorted();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900