-1

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;
    } 
  • 1
    Arguably, one of the most underrated additions to Java 8, [`StringJoiner`](https://docs.oracle.com/javase/10/docs/api/java/util/StringJoiner.html) – MadProgrammer Jan 16 '20 at 04:19
  • 1
    I'd be more worried about the `sort` and `shuffle`. Just rearranging the deck to print it out, ugh. At least make a copy and sort that, if you cannot just use a sorted deck in advance. – Maarten Bodewes Jan 16 '20 at 04:31

2 Answers2

2

Based on MadProgammer's suggesting I experimented a little with StringJoiner (which I didn't know existed).

Here is one way to do it:

List<String> deck = new ArrayList<>();
deck.add("Ace of Clubs");
deck.add("Two of Clubs");
deck.add("Three of Clubs");

StringJoiner stringJoiner = new StringJoiner("|", "{", "}");
for (String card : deck) {
  stringJoiner.add(card);
}
System.out.println(stringJoiner.toString());

This gives as output:

{Ace of Clubs|Two of Clubs|Three of Clubs}

That said I am not sure why you shuffle in the toString method as that wouldn't make sense? If it is for printing just create a copy as Maarten suggests.

k88
  • 1,858
  • 2
  • 12
  • 33
2

With Java Lamda expression You can achieve expected result in much less code and effort. Like :

public String toString(){
        Collections.sort(deck);
        String result = "{"+ deck.stream().map(element->element.getCard()).collect(Collectors.joining("|")) + "}";
        shuffle();
        return result;

    }