Question

I've a Object val with values [0,2,4,5,9], actually these values are been created from TreeSet like as shown below

TreeSet ts=new TreeSet ();
ts.add(0);
ts.add(2);
ts.add(4);
ts.add(5);
ts.add(9);

System.out.println(ts);

Output

[0,2,4,5,9]

i'm passing the output as string from server to client side

in the client side i 've

Object obj="[0,2,4,5,9]";

Can anyone please tell me how to convert the object values [0,2,4,5,9] to TreeSet Object so that i can iterate the values

Was it helpful?

Solution

You can send the treeset value back to the client in the form of JSONArray. I would recommend you to look into org.json package to easily do these kind of client and server interaction.

Note : JSON is light-weight data interchange format than xml. Latest browsers support json as a defacult standard. if you further want to go for string based value, you have to make use of javascript function to split the values and make use of them

OTHER TIPS

how to convert the object values [0,2,4,5,9] to TreeSet Object so that i can iterate the values

Following is the way to convert object values to TreeSet,

 Object obj="[0,2,4,5,9]";

 String[] arr=obj.toString().substring(1, obj.toString().length()-1).split(",");

 TreeSet ts=new TreeSet(Arrays.asList(arr));

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.

For example: You have the String value of TreeSet in below variable

var str = "[0,2,4,5,9]";
str =  str.substring(1,str.length-1);

var res = str.split(","); // This gives you a array of the elements.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top