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

B

brightoceanlight

Hi,

I basically have a set of strings :

String[] strs = {"First", "Next", "Third", "Fourth", "Fifth"};

as an array.

I would like to see if an element is contained in this set of strings.
If it was a Set, I could use the contains() method of Set or HashSet to
see if an element is contained in the set of strings. But there I
would have to load the elements into HashSet. Is there an easy way to
do this?
 
I

Ingo R. Homann

Hi,

Hi,

I basically have a set of strings :

String[] strs = {"First", "Next", "Third", "Fourth", "Fifth"};

as an array.

I would like to see if an element is contained in this set of strings.
If it was a Set, I could use the contains() method of Set or HashSet to
see if an element is contained in the set of strings. But there I
would have to load the elements into HashSet. Is there an easy way to
do this?

It should be something like this:

HashSet<String> s=new HashSet<String>();
s.addAll(Arrays.asList(strs));

Hth,
Ingo
 
T

Thomas Hawtin

Hi,

I basically have a set of strings :

String[] strs = {"First", "Next", "Third", "Fourth", "Fifth"};

as an array.

I would like to see if an element is contained in this set of strings.
If it was a Set, I could use the contains() method of Set or HashSet to
see if an element is contained in the set of strings. But there I
would have to load the elements into HashSet. Is there an easy way to
do this?

Arrays.asList(strs).contains(str)

Doesn't even copy the array. Of course, using a HashSet, sort followed
by binary search, or similar, would be faster for repeated runs.

Tom Hawtin
 
R

Roedy Green

I would like to see if an element is contained in this set of strings.
If it was a Set, I could use the contains() method of Set or HashSet to
see if an element is contained in the set of strings. But there I
would have to load the elements into HashSet. Is there an easy way to
do this?

You have a minor chicken/egg problem

HashSet, A Collection, has a constructor that takes a Collection.

Hint arrays are very much like Lists, so perhaps there is a method to
turn an array into a List (which is a flavour of Collection).

See http://mindprod.com/jgloss/array.html#COLLECTION
for the answer.
 
R

Roedy Green

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?
 
I

Ingo R. Homann

Hi Roedy,

Roedy said:
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.

Ciao,
Ingo
 
T

Thomas Hawtin

Ingo said:
Hi Roedy,




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
 
R

Roedy Green

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

I was curious what this minimalist class was called. Now I am more
puzzled than ever:

Here is the code for Arrays.asList

public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);

Which indicates you actually get a full blown ArrayList. But the odd
thing is I could find no ArrayList constructor to match. There is a
collection constructor but not an array constructor. Is there some
sort of boxing happening? Am I just not seeing the constructor for
some reason? I don't see it in the JavaDoc either.
 
I

Ingo R. Homann

Hi,

Roedy said:
I was curious what this minimalist class was called. Now I am more
puzzled than ever:

Here is the code for Arrays.asList

public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);

Which indicates you actually get a full blown ArrayList. But the odd
thing is I could find no ArrayList constructor to match.

Note that this is not a java.util.ArrayList, but a
java.util.Arrays$ArrayList!

Ciao,
Ingo
 
C

Chris Smith

Gijs Peek said:
You could also use the ArrayUtils.contains() method from apache commons
(http://jakarta.apache.org/commons/lang/)

You could... but since the core API provides a trivial and effective way
to solve the problem, it wouldn't really make sense. Unless, of course,
the code is inextricably dependent upon Commons Lang already.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top