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

The Archive Base Latest Questions

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

If I want an empty map or a one element map, Java Collections has

  • 0

If I want an empty map or a one element map, Java Collections has a method for that. Why is there no method for more than one element? What’s the best way to create a static final map with 2 elements in it? I know I can do something like:

private static final Map<String, String> MAP = new HashMap<String, String>() {
    { put("a", "b"); put("c", "d"); }
};

But then Eclipse complains about the serialVersionUID…

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

    The reason Collections has methods for 0 and 1 entry maps is because they are special cases… the empty Map is an immutable singleton for example.

    For what you want to do, though, I’d strongly recommend using Guava. Its Immutable* collections (ImmutableMap specifically) are what you want I think:

    private static final ImmutableMap<String, String> MAP = ImmutableMap.of(
        "a", "b",
        "c", "d");
    

    You can do the above for small maps, and for bigger maps you can write:

    private static final ImmutableMap<String, String> MAP = 
        ImmutableMap.<String, String>builder()
          .put("a", "b")
          .put("b", "c")
          ...
          .put("y", "z")
          .build();
    

    If you don’t use Guava, you’ll still likely want to ensure that this map can’t be changed. This is a lot uglier:

    private static final Map<String, String> MAP;
    
    static {
      Map<String, String> temp = new HashMap<String, String>();
      temp.put("a", "b");
      temp.put("b", "c");
      MAP = Collections.unmodifiableMap(temp);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to empty a table in hbase... eg: user . Is there any
I want that find empty tags, here is a example txt =<lol1><><lol2> rgx =
I want to check if a Map doesn't contain empty values. If the value
Simple question, I just want to initialize a map to be empty, instead of
I have a Google Map on one web page where I want to disable
I'm creating an application in Node that has some CRUD components. On one of
I want to empty the listed field value to empty on success. In the
i want to make empty textbox checking for android. i tried try catch but
I want to eliminate empty elements within my String array. This is what I
I want to implement a empty div with background color in it. <html> <head>

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.