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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:11:08+00:00 2026-05-14T06:11:08+00:00

HashMap selections = new HashMap<Integer, Float>(); How can i get the Integer key of

  • 0
HashMap selections = new HashMap<Integer, Float>();

How can i get the Integer key of the 3rd smaller value of Float in all HashMap?

Edit
im using the HashMap for this

for (InflatedRunner runner : prices.getRunners()) {
       for (InflatedMarketPrices.InflatedPrice price : runner.getLayPrices()) {
           if (price.getDepth() == 1) {
             selections.put(new Integer(runner.getSelectionId()), new Float(price.getPrice()));
           }
         }                    

}

i need the runner of the 3rd smaller price with depth 1

maybe i should implement this in another way?

  • 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-14T06:11:09+00:00Added an answer on May 14, 2026 at 6:11 am

    Michael Mrozek nails it with his question if you’re using HashMap right: this is highly atypical scenario for HashMap. That said, you can do something like this:

    • get the Set<Map.Entry<K,V>> from the HashMap<K,V>.entrySet().
    • addAll to List<Map.Entry<K,V>>
    • Collections.sort the list with a custom Comparator<Map.Entry<K,V>> that sorts based on V.
      • If you just need the 3rd Map.Entry<K,V> only, then a O(N) selection algorithm may suffice.

    //after edit

    It looks like selection should really be a SortedMap<Float, InflatedRunner>. You should look at java.util.TreeMap.

    Here’s an example of how TreeMap can be used to get the 3rd lowest key:

    TreeMap<Integer,String> map = new TreeMap<Integer,String>();
    map.put(33, "Three");
    map.put(44, "Four");
    map.put(11, "One");
    map.put(22, "Two");
    
    int thirdKey = map.higherKey(map.higherKey(map.firstKey()));
    System.out.println(thirdKey); // prints "33"
    

    Also note how I take advantage of Java’s auto-boxing/unboxing feature between int and Integer. I noticed that you used new Integer and new Float in your original code; this is unnecessary.


    //another edit

    It should be noted that if you have multiple InflatedRunner with the same price, only one will be kept. If this is a problem, and you want to keep all runners, then you can do one of a few things:

    • If you really need a multi-map (one key can map to multiple values), then you can:
      • have TreeMap<Float,Set<InflatedRunner>>
      • Use MultiMap from Google Collections
    • If you don’t need the map functionality, then just have a List<RunnerPricePair> (sorry, I’m not familiar with the domain to name it appropriately), where RunnerPricePair implements Comparable<RunnerPricePair> that compares on prices. You can just add all the pairs to the list, then either:
      • Collections.sort the list and get the 3rd pair
      • Use O(N) selection algorithm
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HashMap where the key is a word and the value is
I have a hashmap with some information(key and value) in a perl file. I
I have a HashMap of a following structure: HashMap<Integer, int[]> map = new HashMap<Integer,
I have a HashMap defined like this HashMap<String, IConnection> connections = new HashMap<String, IConnection>();
I have this field: HashMap<String, HashMap> selects = new HashMap<String, HashMap>(); For each Hash<String,
I have a HashMap with various keys and values, how can I get one
I have my HashMap, private final HashMap<Integer, Poll> pollmap = new HashMap<Integer, Poll>(); and
I'm trying to read all the contacts using RawContacts.entityIterator but I'm seeing this error:
Hashmap contains key and value(result of parsing an XML). Hashmap contains things in the
Hashmap contains key and value(result of parsing an XML). Hashmap contains things in 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.