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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:40:43+00:00 2026-06-14T08:40:43+00:00

I am trying to find a data structure that takes in a particular value

  • 0

I am trying to find a data structure that takes in a particular value from a range of values and map it to a key.

For example, I have the following conditions:

  1. From 1 to 2.9, I want to map it to A.
  2. From 4 to 6, I want to map it to B.
  3. From 6.5 to 10, I want to map it to C.

I have a value of 5 and I would like to map it to a key. So based on the above conditions, I should map it to B.

Is there any data structure in Java that anyone can recommend to me to solve the problem?

Currently I am using a hashtable that can only map a value to a key. I tried to map the range of values to a particular value that exists in the hashtable. However, I got stuck in the mapping of the range of values to a particular value. So now I am trying to do another way of mapping the ranges of values to a key. Does anyone have any idea how I can solve this problem?

EDIT:

Thanks to Martin Ellis, I decided to use TreeMap to solve the problem.

  • 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-14T08:40:44+00:00Added an answer on June 14, 2026 at 8:40 am

    Are your ranges non-overlapping? If so you could use a TreeMap:

    TreeMap<Double, Character> m = new TreeMap<Double, Character>();
    m.put(1.0, 'A');
    m.put(2.9, null);
    m.put(4.0, 'B');
    m.put(6.0, null);
    m.put(6.5, 'C');
    m.put(10.0, null);
    

    The lookup logic is a bit complicated by the fact that you probably want an inclusive lookup (i.e. 2.9 maps to ‘A’, and not undefined):

    private static <K, V> V mappedValue(TreeMap<K, V> map, K key) {
        Entry<K, V> e = map.floorEntry(key);
        if (e != null && e.getValue() == null) {
            e = map.lowerEntry(key);
        }
        return e == null ? null : e.getValue();
    }
    

    Example:

    mappedValue(m, 5) == 'B'
    

    More results include:

    0.9 null
    1.0 A
    1.1 A
    2.8 A
    2.9 A
    3.0 null
    6.4 null
    6.5 C
    6.6 C
    9.9 C
    10.0 C
    10.1 null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find how to read data from my database in my iphone
I trying to find function in PHP that can output the binary data. The
I'm trying to find a CRC32 computation algorythm that output data in positive 8-character
I have a set of structured data, that I'm trying to merge together, based
I have a data structure in my C++ program that has some attributes of
I have a dataset and I am trying to find results that reference each
I'm trying to assemble this JSON object of Baobab data (tree structure). I have
I'm trying to read a JSON file into a data structure so that I
Trying to find a standard. The CmdLet will process data - multiple input, defined
I am trying to find and replace html data using jquery. The jquery script

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.