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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:47:12+00:00 2026-06-17T15:47:12+00:00

I have Objects which need to be mapped to Integers (or primitive ints). So

  • 0

I have Objects which need to be mapped to Integers (or primitive ints).

So for example

Object[] objects = new Object[X];
objects[2]=o1;
objects[34]=o2;
objects[126]=o3;
...

So I have a range from 0 to X for the keys but only need a few (lets say 20) mapping pairs.
(The mapping is only done once and will not change)

Would it be (performance and memory-usage vise) a better idea to use a Map (and if which implementation would suite best) instead of the “large” array which is mostly unused.

The needed range might actually scale up later during development – so if it is only important for very large X that would still be interesting for me. (Currently X is 256 – so rather small)

To sum it up: I want to efficiently map Numbers to Objects in Java.

  • 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-17T15:47:16+00:00Added an answer on June 17, 2026 at 3:47 pm

    whether you use the fastest possibility (array) or a HashMap depends on your application. Do you have millions of calculations (array accesses) per second? Yes then take the array, and spend the 1k of memory.
    Oherwise take the Map.
    But there is a third solution if x is big:
    This solution is nearly as fast as the map (maybe faster), but uses less space:
    Use a sorted array of int as index into an object array:

    int[] idx ={2, 34, 126}
    Object[] objs = {o1, o2, o3};
    

    Idx must be sorted ascending.
    now get object for key 34:

    int pos= Arrays.binsearch(34, idx); // params might be wrong, please correct
    Object o = objs[pos];
    

    But this i would use this only for a high number of objects with minimal storage space requirementm where objects do not change on runtime.

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

Sidebar

Related Questions

I have a list of an object which need to be sorted depending on
I have an object which is contained within a List<>, I need to remove
I have a program in which I need to store a Class object into
I have a string data which I need to parse into a dictionary object.
I have some objects which will be used in the same manor. This object
I have a mapped POJO which I need to persist. In that POJO I
I have several objects classes mapped to a database using annotations and need some
I have this example of an array of objects and I need to randomize
Object-Relational-Mappers have been created to help applications (which think in terms of objects) deal
I have objects which create other child objects within their constructors, passing 'this' so

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.