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

  • Home
  • SEARCH
  • 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 8401525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:49:46+00:00 2026-06-09T21:49:46+00:00

Some Message class is able to return a tag name based on tag number

  • 0

Some Message class is able to return a tag name based on tag number

Since this class is instanciated many times, I am a bit reluctant to create a HashMap for each instance:

public class Message {
  private HashMap<Integer,String> tagMap;

  public Message() {
    this.tagMap = new HashMap<Integer,String>();
    this.tagMap.put( 1, "tag1Name");
    this.tagMap.put( 2, "tag2Name");
    this.tagMap.put( 3, "tag3Name");
  }

  public String getTagName( int tagNumber) {
    return this.tagMap.get( tagNumber);
  }
}

In favor of hardcoding:

public class Message {
  public Message() {
  }

  public String getTagName( int tagNumber) {
    switch( tagNumber) {
      case 1: return "tag1Name";
      case 2: return "tag2Name";
      case 3: return "tag3Name";
      default return null;
    }
  }
}

When you put everything in the mix ( Memory, Performance, GC, …)

Is there any reason to stick to HashMap?

  • 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-09T21:49:48+00:00Added an answer on June 9, 2026 at 9:49 pm

    Initialize MAP in a static block.

    And since you will be creating many objects of Message.you should write code like this

    public class Message {
    
      private static HashMap tagMap;
    
      static {
         tagMap = new HashMap();
         tagMap.put( 1, "tag1Name");
         tagMap.put( 2, "tag2Name");
         tagMap.put( 3, "tag3Name");
      }
    
      public Message() {
    
      }
    
      public String getTagName( int tagNumber) {
        return tagMap.get( tagNumber);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose the following class public class Message { // some code } And a
I have a class that raises an event with an error message. In some
I have a web service , i add some extra class which have message
I have a class in c# in that store some user messages, and return
I'd like to do some message debugging on traffic for a few protobuf based
I have this form with the parameter year and name : /register?year=&name=test My class
I am trying to display some message in span. but it is not working.
Instead of throwing new Exception(Some message, maybeSomeCause) , which means that all callers of
I am having a website which stores some message in arabic .When i try
I want to use XmCreate{Error|Warning|Info}Dialog to display some message in screen in my SDL

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.