Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
brightoceanlight@hotmail.com - 30 Sep 2005 08:06 GMT
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?
Ingo R. Homann - 30 Sep 2005 08:22 GMT
Hi,

> Hi,
>
[quoted text clipped - 9 lines]
> 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
Thomas Hawtin - 30 Sep 2005 08:28 GMT
> Hi,
>
[quoted text clipped - 9 lines]
> 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
Signature

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

Roedy Green - 30 Sep 2005 09:06 GMT
> 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?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Ingo R. Homann - 30 Sep 2005 09:17 GMT
Hi Roedy,

>>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.

Ciao,
Ingo
Thomas Hawtin - 30 Sep 2005 09:44 GMT
> Hi Roedy,
>
[quoted text clipped - 7 lines]
> 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
Signature

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

Roedy Green - 30 Sep 2005 10:18 GMT
>Sources are a good place to look.

What source are you looking at?  I'm seeing something quite different.
I have JDK 1.5.0_05.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 30 Sep 2005 10:07 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Ingo R. Homann - 30 Sep 2005 10:15 GMT
Hi,

>>IIR the Sources correctly, it is a minimalist wrapping class that
>>implements List. IIRC, even add() is not possible but throws an
[quoted text clipped - 10 lines]
> 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
Roedy Green - 30 Sep 2005 10:59 GMT
>Note that this is not a java.util.ArrayList, but a
>java.util.Arrays$ArrayList!

Ah Ha!  all makes sense. Thanks!
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 30 Sep 2005 09:01 GMT
>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.


Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Gijs Peek - 30 Sep 2005 09:08 GMT
You could also use the ArrayUtils.contains() method from apache commons
(http://jakarta.apache.org/commons/lang/)

> Hi,
>
[quoted text clipped - 9 lines]
> would have to load the elements into HashSet.  Is there an easy way to
> do this?
Chris Smith - 30 Sep 2005 16:46 GMT
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.