Re: Is there a way to use String[] as a Set??



Ingo R. Homann wrote:
Hi Roedy,

Roedy Green wrote:

Arrays.asList(strs)


What actually pops out, I know it is a List,  but is it an ArrayList
really or some minimalist wrapping around the original array without
making a copy?


IIR the Sources correctly, it is a minimalist wrapping class that implements List. IIRC, even add() is not possible but throws an UnsupportedException.

Sources are a good place to look.

Yup, no structural modifications allowed, although you can use set. It only requires a single, tiny little object with a single reference (plus header) to be allocated, much like an iterator. In this day and age the cost of allocating a small object is more or less negligible. For comparison, javac's code for concatenating two strings creates at least four objects (it could use String.concat instead).

The clue (without looking at the docs or source) that it is a live wrapper rather than a copy, is that the method name is asList, not toList.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
.