views:

50

answers:

2

I have a SortedSet defined this way:

SortedSet<RatedMessage> messageCollection = new TreeSet<RatedMessage>(new Comp());

and I have an array of RatedMessage[]

I had to use the array as the set misses the serialization feature, now I need to construct it back.

Is there a quick way to add all the items from the array to the set again?

+3  A: 

Set has an addAll method, but it only takes a collection, so you'll need to convert the array first:

RatedMessage[] arr;
messageCollection.addAll(Arrays.asList(arr));
Michael Mrozek
+1; loaded just as I was about to post the same.
Carl
+7  A: 
Collections.addAll(messageCollection, array);

Functionally identical to Michael's answer, but as the javadoc says:

Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations.

ColinD
Oh, well done. "The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations."
Michael Mrozek
Just as I was adding that same quote. =)
ColinD