32

From Sun's Java Tutorial, I would have thought this code would convert a set into an array.

import java.util.*;

public class Blagh {
    public static void main(String[] args) {
        Set<String> set = new HashSet<String>();
        set.add("a");
        set.add("b");
        set.add("c");
        String[] array = set.toArray(new String[0]);
        System.out.println(set);
        System.out.println(array);
    }
}

However, this gives

[a, c, b]
[Ljava.lang.String;@9b49e6

What have I misunderstood?

3
  • 2
    "[Ljava.lang.String;@9b49e6" IS an array of Strings. Commented Jun 14, 2009 at 13:16
  • Was the order of the elements accurate in this? I am having a similar situation where the order seems to have a mind of its own and not in the order that items were added. Commented Jan 24, 2012 at 3:45
  • Sets in general don't have a defined order, so you can't rely on the order you put the the elements into the set being the same as the order you get them out. Try the LinkedHashSet implementation of the Set interface if that's what you want; it adds this guarantee. Commented Jul 10, 2012 at 13:29

6 Answers 6

33

The code works fine.

Replace:

System.out.println(array);

With:

System.out.println(Arrays.toString(array));

Output:

[b, c, a]
[b, c, a]

The String representation of an array displays the a "textual representation" of the array, obtained by Object.toString -- which is the class name and the hash code of the array as a hexidecimal string.

Sign up to request clarification or add additional context in comments.

4 Comments

Some parts of Java can be a little bit unfriendly.
he should replace System.out.println(array); not System.out.println(set);
@coobird I know this is over 3 years old, but it's best to use Arrays.toString over Arrays.asList followed by List.toString :-)
ps: note that when you call the "toArray" method, you shouldn't construct a zero length array. it's a waste, since it will internally have to create a new array to fit your data. If you do this: set.toArray(new String[set.size()]) you can save yourself an array allocation, as the "toArray" method will see that the passed array is large enough to hold the data. Directly from the javadocs: If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.
10

for the sake of completeness check also java.util.Arrays.toString and java.util.Arrays.deepToString.

The latter is particularly useful when dealing with nested arrays (like Object[][]).

1 Comment

I agree, it's good to not that these methods may be more efficient than creating an intermediate list via asList(), especially for large arrays. Note that deepToString() is most useful for multi-dimensional arrays and complex nesting.
5

It's OK.

You are not seeing the array contents with System.out.println(array) because println calls object.toString() to get the bytes from an Object for output.

Since HashSet overrides the default toString() implementation, you can see the set contents with System.out.println(set);

As arrays do not override the default toString() (that gives the class name and some sort of identity hash code), you are getting the fuzzy [Ljava.lang.String;@9b49e6

Hope that helps

Comments

2

As dfa mentioned, you can just replace:

System.out.println(array);

with...

System.out.println(Arrays.toString(array));

Comments

1

I don't think you have misunderstood anything; the code should work. The array, however, is not smart enough to print its contents in the toString method, so you'll have to print the contents with

for(String s : array) println(s);

or something like that.

Comments

0

You have the correct result. Unfortunately the toString()-method on the array is still the original Object.toString() so the output is somewhat unusable per default but that goes for all arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.