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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:13:17+00:00 2026-06-09T18:13:17+00:00

A structure k => v ( k and v are >=0 ints) where all

  • 0

A structure k => v (k and v are >=0 ints) where all k are unique while v may be equal (k1 => v and k2 => v) should be sorted in ascending order of v values, for example:

Let we have [35 => 1, 23 => 4, 9 => 9, 2 => 14] and want to insert a new pair 20 => 5, then the result is going to be [35 => 1, 23 => 4, 20 => 5, 9 => 9, 2 => 14].

What is the fastest structure in Java I can use in order to create it based on some input data and further iterate it in a ‘one-by-one’ fashion from the left. SortedHashMap?

  • 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-09T18:13:18+00:00Added an answer on June 9, 2026 at 6:13 pm

    Some time ago I encountered a similar situation; I used a couple of maps in parallel:

    • A HashMap<K, P> M, where P is the pair type, to be able to find pairs by their key.

    • A TreeMap<P, P> S, with a comparator that sorts first by the value and then by the key, to always have the correct sorting order available.

    By maintaining both structures in parallel, it is possible to always have your pairs sorted, without having to use the key as the sorting value.

    To add a pair:

    M.put(key, pair);
    S.put(pair, pair);
    

    To get a pair by key:

    M.get(key);
    

    To remove a pair:

    P pair = M.get(key);
    M.remove(key);
    S.remove(pair);
    

    To get a sorted list of pairs:

    S.keySet();
    

    The average complexity of the core operations is:

    • Add: O(logn) (TreeMap)
    • Get: O(1) (HashMap)
    • Remove: O(logn) (TreeMap)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure holding 3d co-ordinates in 3 ints. In a test I've
Let's say you have the following structure in C#: struct Piece : IEquatable<Piece> {
I have a program structure like following : int firstFunction(unsigned char *firstValue, int s,
Structure of my table : ID(int) | NUMBER(int) | CREATED_AT(date) If I have three
We have a general organizational table structure, think of it s a Tree or
I have a xml document ~10mb in size. It has relatively simple structure but
I have a program which creates a C structure which contains large arrays of
I have a structure that is instantiated in a parent function and I want
I'm trying to store randomly generated dice values in some data structure, but don't
I have a table with the following structure - steamid, itemid, eventid, value ,

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.