Question

I have a class similar to this:

class MyClass implements Iterable<Set<String>> {
  @Override
  public Iterator<Set<String>> iterator() {
    //Lots of code here
  }

  @Override
  public String toString() {
    return ImmutableList.copyOf(this).toString();
  }
}

It implements Iterable<Set<String>> correctly. For toString(), I'd like to display it as if it were a List<Set<String>>. My solution for this, so far, is as above. Is there a better way to do this or is this the right way? The private member variables are part of the state to output the iterator, but there isn't a member on which I could call toString() to get what I want.

Was it helpful?

Solution

If you're already using Guava, this can be done lazily in the one line

return Iterables.toString(this);

...without making an unnecessary copy.

OTHER TIPS

Since you're using Guava already, try this:

StringBuilder sb = new StringBuilder("[");
Joiner.on(",").appendTo(this, sb);
return sb.append("]").toString();

It will do pretty much the same as your call, but without making a copy.

What you are doing is not "wrong" at least. The list is constructed explicitly, which could be avoided with a small utility method, but there are few cases where something like this really matters.

class IteratorToString
{
    public static <T> String toString(Iterator<T> iterator)
    {
        if (!iterator.hasNext())
        {
            return "[]";
        }

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        while (true)
        {
            T t = iterator.next();
            sb.append(t);
            if (!iterator.hasNext())
            {
                return sb.append(']').toString();
            }
            sb.append(',').append(' ');
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top