• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I determine if a string array contains a particular string?  RSS feed

 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

Is there a simple way to determine if a string array contains a string? Something like:

if (stringArray.contains("lookingFor")) {
System.out.println("Life is good");
}

I've looked at CharSequence, StringList, etc. but haven't come up with anything. I searched this forum and noticed some reference to hash tables; but, I'm not familiar with them.

I suppose I could loop through every element in the array and compare it to the search string but this seems a bit klunky especially when you consider I have four search strings I need to check for.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could you replace the String with a Collection?
If so I would use the Set collection and use the functionality of Sets to find if a given string exists in a Set of Strings.

Not sure if this is the best solution, or even the neatest, but it is the one I would personally use.

Java Trail for Collections
You will also find information about HashMap/HashTable collections too.

G
[ February 15, 2007: Message edited by: Gavin Tranter ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi "Jerry",

your idea with the hash tables points into the right direction.

But don't use hash tables, take lists or sets as collection.

The Arrays class has a static method "asList" that turns an array into a List. And the collection classes that implement List all have a method "contains". You can take an ArrayList for example:


If you don't like generics in your compositions, you can omit the <String> in this case.

Yours,
Bu.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an unordered list (java.util.List, array), you will definitely have to check every element to ensure that the element you are looking for isn't the last element in the list. You could gain some efficiency by checking the element against each search String, therefore you would only have to traverse the list once. There are hovever faster search methods for ordered lists (see: java.util.Collections.binarySearch(), java.util.Arrays.binarySearch()) and for hash based sets (see: java.util.HashSet.contains()). Note that the code Burkhard Hassel provided just delegates the "looping" to another method, but it is still there.
[ February 15, 2007: Message edited by: Garrett Rowe ]
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess that the List collection contains() method, uses an optomised search method to search the List.
Check the the source files to find out.

If you are searching the same list for all four search strings, then use the Set collection, put all four search strings in a set and do an intersection of the Set of strings to be searched (a) with the Set of search Strings (b) this should (if memory servers me) return a 3rd set that is the result of intersection, i.e. the strings that lie in both Sets.

G
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gavin Tranter:
I would guess that the List collection contains() method, uses an optomised search method to search the List.

Nope, it just does a linear scan of the list elements until it finds the Object, no magic here (for java.util.ArrayList):


[ February 15, 2007: Message edited by: Garrett Rowe ]
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for the helpful replies.

List list = Arrays.asList(array); did the trick.

Thanks Burkhard.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


quite efficient way of doing it (the flag), but it would probably
be even faster with a while vs. a for. (while(!found))

Justin
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Garrett, you learn something new every day. (or you should try too anyway)
I still say Sets are the best choice (Ok so i have a bit of a thing for sets currently )

G
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin writes: "quite efficient way of doing it (the flag), but it would probably be even faster with a while vs. a for. (while(!found))"

You don't need 'while'. You can do this with 'for' also. I'd write it like the following. This is also 'null safe' - i.e. it also works if value is null or the array contains an entry that is null.

By the way Justin, why did you use 'equalsIgnoreCase'? Jerry doesn't say anywhere that he wants a case-insensitive search.
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone for the wealth of information provided.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!