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 3337732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:17:16+00:00 2026-05-18T00:17:16+00:00

Given a specific set of strings, what’s the best way to map them to

  • 0

Given a specific set of strings, what’s the best way to map them to a corresponding set of integers? Say I have a class with a few integer constants that I use internally, but need to take incoming external strings and determine the correct corresponding integer constant they map to.

Here’s a simplified example:

public class Example {
    public static final int ITEM_APPLE = 0;
    public static final int ITEM_BANANA = 1;
    public static final int ITEM_GRAPE = 3;

    public void incomingData(String value) {
        // Possible values would be "apple", "banana", and "grape" in this case.
    }
}

What would the most appropriate approach be to go from that value to its corresponding integer constant? A HashMap? Or is ther any way to define these mappings in a static member? Another idea?

  • 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-05-18T00:17:17+00:00Added an answer on May 18, 2026 at 12:17 am

    I would use a HashMap as that is closest to what you want to achieve, and therefore a maintainable solution. You can define a static HashMap:

    Class Example {
        public static final int ITEM_APPLE = 0;
        public static final int ITEM_BANANA = 1;
        public static final int ITEM_GRAPE = 3;
    
        private static final Map<String, Integer> fruitCodes = new HashMap<String, Integer>();
    
        static {
            fruitCodes.put("apple", ITEM_APPLE);
            fruitCodes.put("banana", ITEM_BANANA);
            // ...
        }
    
    
        public void incomingData(String value) {
            // Possible values would be "apple", "banana", and "grape" in this case.
            Integer code = fruitCodes(value);
    
            if (null == code) {
                throw new IllegalArgumentException("Forbidden fruit: " + value);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a specific word pattern (say, balloon), I would like to find the number
I have an array of ints like this: [32,128,1024,2048,4096] Given a specific value, I
Given that two branches have diverged and a specific commit from one branch (and
Given a set of letters, say from A.. F, how can one generate a
Given that this field locale have been set for norwegian bokmål and norway: Locale
When displaying all articles of a specific month (given through the urls like this
I need to draw a circle onto a bitmap in a specific colour given
Given that the tasks in a specific sprint will not divide perfectly into the
I am trying to check for a specific file in a given directory. I
...meaning pattern in its general English usage, not specific to OO design patterns. Given

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.