0

I have a list of static Strings:

static final String FOO = "foo";
static final String BAR = "bar";
static final String DUH = "DUH";
static final String GOO = "goo";
static final String ZUT = "zut";

I want to check if a given string is part of this list, but want to keep the possibility reference each item individually. I want the check implementation to be unaware of the individual items. So not like this:

boolean isValid(String item) {
  return (item.equalsIgnoreCase(FOO) || item.equalsIgnoreCase(BAR) || ......)
}

Thnx! Bas Hendriks

4
  • 1
    If these constants have a connection to each other, you might want to use an enum instead of a set of separate static final variables.
    – Jesper
    Jan 26, 2011 at 8:43
  • @jesper I'm restricted to java 1.4, so no enums Jan 26, 2011 at 8:46
  • 3
    There is a typesafe enum pattern for pre 1.5 Java, which is intended for these situations. In that case, you can keep the Set of valid identifiers in the class and add the isValid method there. Nicely encapsulated.
    – Confusion
    Jan 26, 2011 at 8:48
  • 1
    Java 1.4? Poor you, that's ancient and has long since been EOL-ed. Such a restriction should be noted in the question, however, since it's pretty non-standard these days (luckily). Jan 26, 2011 at 8:50

5 Answers 5

5

Create an array containing all the strings and use a for loop to check if one of them matches the input string.

9
  • 1
    You don't usually use an Array to look something up, as that's an O(log n) search, whereas a Set would have O(1) behavior. For smaller numbers of items it may not matter much, but should the number ever grow, it won't lead to unpleasant surprises if you backed the collection by a Set or similar.
    – Confusion
    Jan 26, 2011 at 8:53
  • @Confusion Big Oh notation is irrelevant here. It says nothing about being faster or slower. It says at what rate an algorithm execution time grows. I'd be surprised if the nice data structures (hash tables and trees) do it faster than a linear search for OP's case. It's clear that the OP deals with a small number of elements as he's typing it in code. Otherwise, he wouldn't need to access them individually. Jan 26, 2011 at 9:08
  • 1
    There is hardly ever a good reason to store data that is primarily looked up in an array. You don't know how many constants he has. He may well be generating all possible TLA's or loading hundreds from a a file for lookup. The number may grow from 10 constants now to a 100 in the future. Perhaps the lookup takes place thousands of times every second. To top it off, a Set lookup is shorter in terms of lines of code than a loop over an array.
    – Confusion
    Jan 26, 2011 at 9:23
  • @mehrdad i do not want to use this becouse i'm looking for a check functoin that is unawware of the individual items. When i add an extra item, i have to ad the same item in the array. Jan 26, 2011 at 9:29
  • @Bas: Well, you're pretty much stuck with adding something in two places if you can't use enums. You can improve it a little bit by adding the constant name, not the string to the collection. Jan 26, 2011 at 9:32
2

Depending on your constraints You should Either use a Set or a Map

A. Set <String> stringSet use stringSet.contains(foo) to check if the String exits in the set

B. Map<String,String> stringMap and use StringMap.containsKey(foo) to check if the String exits in the Map;

0

Create a final, immutable Set of Strings to check in:

private static final Set<String> STRINGS = 
    Collections.unmodifiableSet(new HashSet<String>(
         Arrays.asList("foo", "bar", "DUH", "goo", "zut"));

public boolean isValid(String input) {
  return STRINGS.contains(input);
}

If you are restricted to Java 1.4, then simply remove all generics and it should still work the same way.

If you use Guava already (or are willing to include it), then you can make that definition a bit nicer (Guava requires Java 1.5 or later, however):

private static final Set<String> STRINGS = ImmutableSet.of("foo", "bar", "DUH", "goo", "zut");
0

Another approach: create an Enum with a static method to check if an item is valid or not:

enum MyEnum {
    FOO("foo"), 
    BAR("bar"), 
    DUH("DUH");

    private static final EnumSet<MyEnum> set = EnumSet.allOf(MyEnum.class);
    private String name;

    MyEnum(String name) {
        this.name = name;
    }

    public static boolean isValid(String item) {
        for (MyEnum e : set) {
            if (e.name.equalsIgnoreCase(item)) {
                return true;
            }
        }
        return false;
    }
}

To check if an item is a valid member of the enum you would call:

MyEnum.isValid(item);
1
  • I'm limited to java 1.4, so no enum's Jan 26, 2011 at 9:27
0

reflection. gather all static final String fields declared in the class, and build a set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.