Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8558359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:47:38+00:00 2026-06-11T15:47:38+00:00

I can’t help noticing that I’m using quite a lot of string comparisons while

  • 0

I can’t help noticing that I’m using quite a lot of string comparisons while parsing a well defined XML file in Android (with a XmlPullParser).

As of now it typically looks something like this (somewhat simplified):

...
tag = parser.getName().toLowerCase();
if ("tag1".equals(tag)) {
    // Do something with the state machine
}
else if ("tag2".equals(tag)) {
    // Do something else with the state machine
}

...

else if ("tag23".equals(tag)) {
    // Do something more with the state machine
}

What I would like to have instead is something like this (where StringMatcher would be the hypothetical happy-maker for me):

private static final StringMatcher tagMatcher = new StringMatcher(StringMatcher.NO_MATCH);

static {
    tagMatcher.addString("tag1", 1);
    tagMatcher.addString("tag2", 2);
    ....
    tagMatcher.addString("tag23", 23);
}

...

tag = parser.getName().toLowerCase();
switch (tagMatcher.match(tag)) {
    case 1:
        // Do something with the state machine
        break;
    case 2:
        // Do something else with the state machine
        break;
    ...
    case 23:
        // Do something more with the state machine
        break;
    default:
        Log.e("PARSER", "Unexpected tag: " + tag);
        break;
}

As you see I would like a UriMatcher pattern applied to my XML file tags. Do any of you know of such a class I can use in Android? Any other fast filtering on strings would do as well (it would be neat, though, if the UriMatcher-pattern could be reused).

So far I’ve been looking at regular expressions but I’m not really sure I can fit it to my needs (I would like a switch – case style test) and, of course, the regular string comparison as shown in above example.

Cheers,
— dbm

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:47:39+00:00Added an answer on June 11, 2026 at 3:47 pm

    You can either use a HashMap since that does not need to iterate over the whole array to find the match value

    private static final HashMap<String, Integer> tagMatcher =
            new HashMap<String, Integer>();
    
    static {
        tagMatcher.put("tag1", 1);
        tagMatcher.put("tag2", 2);
        tagMatcher.put("tag23", 23);
    }
    
    private void parse (String node) {
        Integer value = tagMatcher.get(node);
        int match = value != null ? value.intValue() : 0;
        switch (match) {
            case 1:
                // etc
                break;
            case 0: // no match
                break;
        }
    }
    

    or you can use a SparseIntArray using the same hash approach. Advantage here is that you don’t need to box int into Integer which should result in a slight speed / memory advantage.

    private static final SparseIntArray tagMatcher2 = new SparseIntArray();
    private static void put(String key, int value) {
        tagMatcher2.put(key.hashCode(), value);
    }
    private static int get(String key) {
        return tagMatcher2.get(key.hashCode());
    }
    static {
        put("tag1", 1);
        put("tag2", 2);
        put("tag23", 23);
    }
    
    private void parse2 (String node) {
        switch (get(node)) {
            case 1:
                // etc
                break;
            case 0: // no match
                break;
        }
    }
    

    This is doing binary search instead of iterating over the whole thing like SparseArray#indexOfValue(t) does. Note that there is a chance of hash collisions in this approach.

    I think using an approach like that is faster than a long chain of if (equals) else if (equals) for larger amounts of comparisons. The if .. else if approach needs to check String.equals() every time which boils down to comparing all characters of the strings while a hash based approach needs to calculate the hash value just once and can do a binary search over all known hash values then.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can I authenticate with just Google account username and password instead of using OAuth?
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
Can PHP PDO extension bind nested objects automatically ? I mean using foreign key
Can anybody help me? What should be the datatype for this type -07:00:00 of
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can you guys help me understand a concept real quick, I'm having trouble understanding
Can we change the color of the divider? Apple documentations says, that we can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.