3

I have a problem, but no solution for now. What i have is around 300 strings for example:

  1. "USNEWYRK";
  2. "USWSHGNT";

And what i have to do is compare if one of these strings are requested string and return something like this:

  1. "USA, New York";
  2. "USA, Washington";

So any good solutions? And i cant use java 1.7. Only 1.6.

3
  • Do you have "decoding" for each of present strings (I mean pair, like "USNEWYRK -> USA, New York", or you have to automatically unwrap this abbreviations? Commented Dec 24, 2012 at 22:24
  • Ok, what i have is android app that is reading files from external storage, and file names are currently like that. So i want to make something that will use that file name and look for real string in some kind of list,hash map,table. Basically to convert file names in to more recognized string representation. Commented Dec 24, 2012 at 22:27
  • Ok, actually this issue doesn't refer do Android. This is a common question about Java and algorithms. Commented Dec 24, 2012 at 22:30

3 Answers 3

6

Create a map of key to value, then just lookup the first value as the key and it will give you the value.

Alternatively you could create an enum, where the enum is the key and the toString of enum is your value.

I would prefer the map solution over the enum myself for this kind of situation.

Example of map

public abstract class LocationHelper {
    public static Map<String, String> locations = new HashMap<String, String>();

    static {
        //either put individual elements into the map or
        //read in from external file etc.
    }
}

In another class you can then get the values by doing the following.

System.out.println(LocationHelper.locations.get("USNWYRK"));

This will print "USA, New York"

Note For anyone unfamiliar with the static { } block this is a static initializer, it is useful for populating static variables like maps. This is different to a insitance initializer { } which is a pre constructor initializer for each instance.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank Jon very useful but I have a doubt: why do you make a wrapper class instead a simple variable?
@HMarioD I used a wrapper class in this instance in case he needs to use the variables in multiple other classes. If however it will only ever be relevant to a single class then it can of course be included as a variable within that class. I included the class code as it allowed me to show the static initializer better.
Thanks again, its working like a charm! Your solution as wrapper class is exactly what i need anyway! :)
3

You could create a CSV file that contains stringCode;stringValue for easy maintenance, then build up a hash table / map from this file when your application starts.

This would look like :

USNEWYRK;USA, New York
USWSHGNT;USA, Washington

Once these values are mapped it should be easy to return whatever the user needs to know.

Comments

0

Any sort of a hash table/map should work. Make the key your first string and the value your second string.

Incidentally, you can enter all your keys and values in a JSON string and use a JSON reader to convert to the map object, vs writing code to enter each one individually.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.