Since class AbstractCollection
implement toString()
method, like,
/**
* Returns a string representation of this collection. The string
* representation consists of a list of the collection's elements in the
* order they are returned by its iterator, enclosed in square brackets
* (<tt>"[]"</tt>). Adjacent elements are separated by the characters
* <tt>", "</tt> (comma and space). Elements are converted to strings as
* by <tt>String.valueOf(Object)</tt>.<p>
*
* This implementation creates an empty string buffer, appends a left
* square bracket, and iterates over the collection appending the string
* representation of each element in turn. After appending each element
* except the last, the string <tt>", "</tt> is appended. Finally a right
* bracket is appended. A string is obtained from the string buffer, and
* returned.
*
* @return a string representation of this collection.
*/
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("[");
Iterator<E> i = iterator();
boolean hasNext = i.hasNext();
while (hasNext) {
E o = i.next();
buf.append(o == this ? "(this Collection)" : String.valueOf(o));
hasNext = i.hasNext();
if (hasNext)
buf.append(", ");
}
buf.append("]");
return buf.toString();
}
That's why you get get a string like, [0,2,4,5,9], when you are trying to print TreeSet directly.
To he contrary, if you have String like [0,2,4,5,9],
You can remove the characters, including [
and ]
. and then using String.split(",")
method to put the values into an string array.
And then use new TreeSet(Arrays.asList(stringArray))
; to convert the values into TreeSet
object.