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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:03:33+00:00 2026-05-15T22:03:33+00:00

Is there a map like data structure with integer as key , string as

  • 0

Is there a map like data structure with integer as key , string as value, which ends up to be sorted by key no matter on insertion order like when you insert first item {4, somevalue} that it will be on the fourth place when you use it in further application flow

  • 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-15T22:03:34+00:00Added an answer on May 15, 2026 at 10:03 pm

    The interface SortedMap<K,V> defines a type that has the behavior you’re looking for:

    A Map that further provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time. This order is reflected when iterating over the sorted map’s collection views (returned by the entrySet, keySet and values methods).

    A particular implementation of a SortedMap that may be of interest is a TreeMap:

    A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

    This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

    Note that interface NavigableMap<K,V> extends SortedMap<K,V>. That is, a NavigableMap is a SortedMap, but it also specifies additional methods, e.g. all-inclusive bounds subMap. If you don’t need these additional functionarlities, simply a SortedMap should work just fine.

    See also

    • Java Tutorials/Collections

    Example

    This shows basic usage of NavigableSet<K,V>:

        NavigableMap<Integer,String> nmap =
            new TreeMap<Integer,String>();
        
        nmap.put(3, "Three");
        nmap.put(1, "One");
        nmap.put(4, "Four");
        nmap.put(5, "Five");
        nmap.put(7, "Seven");
        
        System.out.println(nmap);
        // {1=One, 3=Three, 4=Four, 5=Five, 7=Seven}
    
        System.out.println(nmap.firstKey()); // 1
        System.out.println(nmap.lastEntry().getValue()); // Seven
        System.out.println(nmap.higherKey(1)); // 3 
        System.out.println(nmap.lowerEntry(6).getValue()); // Five
    
        NavigableMap<Integer,String> sub = nmap.subMap(2, true, 5, true);
        for (Map.Entry<Integer,String> entry : sub.entrySet()) {
            System.out.printf("%s => %s%n",
                entry.getKey(),
                entry.getValue()
            );
        }
        // 3 => Three
        // 4 => Four
        // 5 => Five
    

    Related questions

    On basic Map manipulation:

    • Iterate Over Map
    • iterating over and removing from a map

    Others of interest:

    • Why doesn’t Java Map extends Collection?

    On SortedMap vs NavigableMap:

    • How to do inclusive range queries when only half-open range is supported (ala SortedMap.subMap)
    • Writing a synchronized thread-safety wrapper for NavigableMap
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a map like data structure that uses Java Class objects as keys
Does map() iterate through the list like for would? Is there a value in
Is there any way to get a map or other data structure of the
I need to test my data structure (in java) which is like a dictionary
Is there a data structure in C++ with O(1) lookup? A std::map has O(log(n))
I have a map generated by others with data structure like this: x =
I am building a tree-like data structure out of associative arrays. Each key is
I have a similar data structure like this: struct Data { std::string id; Blob
I would like to store a mapping of an integer key to a float
I would like to represent matrix-like data in a suitable data-structure in Java. 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.