Basically i have this toString method witch displays sorted cards. Essentially if i have a full deck of cards this is what the output would look like.
{Ace of Clubs | Two of Clubs | Three of Clubs | Four of Clubs | Five of Clubs | Six of Clubs | Seven of Clubs | Eight of Clubs | Nine of Clubs | Ten of Clubs | Jack of Clubs | Queen of Clubs | King of Clubs | Ace of Diamonds | Two of Diamonds | Three of Diamonds | Four of Diamonds | Five of Diamonds | Six of Diamonds | Seven of Diamonds | Eight of Diamonds | Nine of Diamonds | Ten of Diamonds | Jack of Diamonds | Queen of Diamonds | King of Diamonds | Ace of Hearts | Two of Hearts | Three of Hearts | Four of Hearts | Five of Hearts | Six of Hearts | Seven of Hearts | Eight of Hearts | Nine of Hearts | Ten of Hearts | Jack of Hearts | Queen of Hearts | King of Hearts | Ace of Spades | Two of Spades | Three of Spades | Four of Spades | Five of Spades | Six of Spades | Seven of Spades | Eight of Spades | Nine of Spades | Ten of Spades | Jack of Spades | Queen of Spades | King of Spades}
I currently have done this through a simple loop which places a curly brace at the start and loops for how many elements are in the deck adding the output of deck.get(i).getCard() + " | ". And finally subtracting three characters from the end and putting another curly brace. But this is not a very fancy solution and is very programmatic and i like elegant solutions and i think there are functions that exist that serve this kind of purpose but i dont really know lambda expressions or any of that so i was wondering if anyone could walk me through how i could do this. Down below you'll find my current solution. Any help is appreciated Cheers!
public String toString(){
Collections.sort(deck);
String result = "{";
for(int i = 0; i < deck.size(); i++){
result += deck.get(i).getCard() + " | ";
}
if(deck.size() == 0){
return "{}";
}
result = result.substring(0, result.length() - 3);
result += "}";
shuffle();
return result;
}