kodi-pleasant
kodi-pleasant

Reputation: 13

Returning elements of a set in Java using toString() method on separate lines

I am using a toString() method to return elements of Set<String> in Java, but with each element on a separate line.

An example of the code:

import java.util.Set;

public class Animals{
    Set<String> animals;
    String newanimal;

    public Anmimals (Set<String> animals){
        this.animals = animals;
        }

    public setAnimals(String newanimal){
        this.newanimal = newanimal;
        animals.add(newanimal);
    }

    public String toString(){
        /* this is where my problem is i want to return the set (below), 
           however i also need each animal to be on a new line */
        return animal
    }

}

The only things that I could find that were slightly useful, all ended up suggesting to not use toString() and instead use an actual class with System.out.println() to print out the information, however, toString() is required for this particular problem.

Is it possible to add a for loop to the method and iterate through each method as I return each element of a separate line using \n ? All suggestions are welcome, as this one, even if it is possible, seems to be really messy.

Upvotes: 1

Views: 1927

Answers (2)

Mureinik
Mureinik

Reputation: 312086

I'd stream the set, convert each element to a String and join them with a line separator:

@Override
public String toString() {
    return animals.stream()
                  .map(Object::toString)
                  .collect(Collectors.joining(System.lineSeparator()));
}

EDIT:
With the question edited, it was made clear that the question is about joining a Set<String>, and not any old Set. In this case, the solution can be substantially simplified:

@Override
public String toString() {
    return String.join(System.lineSeparator(), animals);
}

Upvotes: 3

SedJ601
SedJ601

Reputation: 13859

You have a lot of mistakes in your code.

import java.util.HashSet;
import java.util.Set;

public class Animals{
    Set<String> animals;
    //String newanimal; delete this!

    //Constructor allows users to set animals using a set.
    public Animals (Set<String> animals){
        this.animals = animals;
        }

    //This default constructor is needed because you did not do Set<String> animals = new HashSet();
    public Animals()
    {
        this.animals = new HashSet();
    }

    //This method adds new elements to the animal set. What you did is wrong, so look over and compare
    public void setAnimals(String newanimal){
        animals.add(newanimal);
    }

    //The toString method you were asking about. I used StringBuilder but you can use someString += item + "\n".
    @Override
    public String toString(){
        /* this is where my problem is i want to return the set (below), 
           however i also need each animal to be on a new line */
        StringBuilder stringBuilder = new StringBuilder();
        for(String item : animals)
        {
            stringBuilder.append(item).append("\n");
        }

        return stringBuilder.toString();
    }
}

Upvotes: 1

Related Questions