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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:21:07+00:00 2026-05-24T04:21:07+00:00

Is it possible in Java to make a Dictionary with the items already declared

  • 0

Is it possible in Java to make a Dictionary with the items already declared inside it? Just like the below C# code:

   Dictionary<string, int> d = new Dictionary<string, int>()
    {
        {"cat", 2},
        {"dog", 1},
        {"llama", 0},
        {"iguana", -1}
    };

How do I do this and what type do I use? I’ve read that Dictionary is obsolete.

  • 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-24T04:21:09+00:00Added an answer on May 24, 2026 at 4:21 am

    This will do what you want:

    Map<String,Integer> map = new HashMap<String, Integer>(){{
        put("cat", 2);
        put("dog", 1);
        put("llama", 0);
        put("iguana", -1);
    }};
    

    This statement creates an anonymous subclass of HashMap, where the only difference from the parent class is that the 4 entries are added during instance creation. It’s a fairly common idiom in the Java world (although some find it controversial because it creates a new class definition).

    Because of this controversy, as of Java 9 there is a new idiom for conveniently constructing maps: the family of static Map.of methods.

    With Java 9 or higher you can create the map you need as follows:

    Map<String, Integer> map = Map.of(
        "cat", 2,
        "dog", 1,
        "llama", 0,
        "iguana", -1
    );
    

    With larger maps, this alternative syntax may be less error-prone:

    Map<String, Integer> map = Map.ofEntries(
        Map.entry("cat", 2),
        Map.entry("dog", 1),
        Map.entry("llama", 0),
        Map.entry("iguana", -1)
    );
    

    (This is especially nice if Map.entry is statically imported instead of being referenced explicitly).

    Besides only working with Java 9+, these new approaches are not quite equivalent to the previous one:

    • They don’t allow you to specify what Map implementation is used
    • They only create immutable maps
    • They don’t create an anonymous subclass of Map

    However, these differences shouldn’t matter for many use cases, making this a good default approach for newer versions of Java.

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

Sidebar

Related Questions

Is it possible to make a Java program that prints its source code to
Is it possible to make a progressbar in Java like displayed on this page?
Why is it not possible to make Java or C++ typeless ? int, float,
I'm just wondering, is it possible in JAVA to make a circular GUI with
I am wondering whether it is possible to make dynamic variables in Java. In
Is it possible, in Java, to make a JPanel skip drawing its background thus
It seems to be possible in Java to write something like this: private enum
I'd like to know if it is possible in Java to nest Enums. Here
Is this possible to make aero frame in java swing application?
I'm trying to make my code as generic as possible. I'm trying to parse

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.