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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:24:01+00:00 2026-05-15T10:24:01+00:00

In Java I’m looking for a way to map multiple keys to the same

  • 0

In Java I’m looking for a way to map multiple keys to the same value. Let’s say I have the numbers 0-9 as keys, and “x”, “y” and “z” as values as follows:

0->y
1->y
2->y
3->x
4->x
5->y
6->z
7->y
8->z
9->z

now x,y and z are really long strings, and I have millions of keys so I can’t afford to store the strings multiple times. How would you go about it?

One idea I had was to create two arrays: an artificial second to key is generated to which the original keys are mapped and which in another array is the key to the actual values. That way the values are only stored once and the original keys can still be indirectly mapped to the values:

0->k1
1->k1
2->k1
3->k2
4->k2
5->k1
6->k3
7->k1
8->k3
9->k3

k1->y
k2->x
k3->z

Question though: Is there a better data structure for this?

  • 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-15T10:24:02+00:00Added an answer on May 15, 2026 at 10:24 am

    Any Map<Integer,String> will do – you are only storing a reference to the string, not a copy of it, so it doesn’t matter how long it is.

    If you are building the same string value multiple times, use intern() to get the same String object for the value each time.

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

Sidebar

Ask A Question

Stats

  • Questions 493k
  • Answers 493k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It gets optimized at compile time anyway, so no runtime… May 16, 2026 at 10:47 am
  • Editorial Team
    Editorial Team added an answer a cookie just holds the Session ID, an id is… May 16, 2026 at 10:47 am
  • Editorial Team
    Editorial Team added an answer I guess you missed the part where the iPhone 4… May 16, 2026 at 10:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Java Newbie here. I have a JFrame that I added to my netbeans project,
The Java static compiler (javac) inlines some static final variables and brings the values
I'm Java beginner, but I thought that when using try-catch-finally I don't have to
I have a Java program with code: public class Test1 { public static void
Java has generics and C++ provides a very strong programming model with template s.
Java has a convenient split method: String str = The quick brown fox; String[]
(Java question) If I reference a field in an inner class, does this cause
Java is one of my programming languages of choice. I always run into the
Java SE 6 (64 bit only) is now on OS X and that is
Java process control is notoriously bad - primarily due to inadequate support by the

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.