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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:56:12+00:00 2026-05-11T03:56:12+00:00

I have a HashMap with millions of entries. Need to retrieve all entries whose

  • 0

I have a HashMap with millions of entries.

Need to retrieve all entries whose keys match a specific set of criteria (in this case, each key is an object with two integer properties; I need to retrieve all keys where each of these integers fall within a specified range).

What is the fastest, most efficient way to iterate through all such keys?

UPDATE: In this particular case, though I didn’t specify it up front, the first integer in the key has a natural precedence over the second integer.

  • 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. 2026-05-11T03:56:12+00:00Added an answer on May 11, 2026 at 3:56 am

    Here’s a solution using TreeMap:

    public static void main(String[] args) {     Comparator<Foo> fooComparator = new Comparator<Foo>() {         @Override         public int compare(Foo o1, Foo o2) {             return o1.compareTo(o2);         }     };      TreeMap<Foo, String> map = new TreeMap<Foo, String>(fooComparator);      map.put(new Foo(1, 4), '');     map.put(new Foo(1, 3), '');     map.put(new Foo(2, 4), '');     map.put(new Foo(3, 4), '');     map.put(new Foo(8, 10), '');     map.put(new Foo(8, 17), '');     map.put(new Foo(10, 10), '');      int a = 2;     int b = 5;      for (Foo f : getKeysInRange(map, a, b)) {         System.out.println(f);     } }  public static List<Foo> getKeysInRange(TreeMap<Foo, String> map, int low, int high) {     Foo key1 = new Foo(low, low);     Foo key2 = new Foo(high, high);      Foo fromKey = map.ceilingKey(key1);     Foo toKey = map.floorKey(key2);      if (fromKey != null && toKey != null && fromKey.compareTo(toKey) < 0)         return new ArrayList<Foo>(map.subMap(fromKey, true, toKey, true).keySet());     return new ArrayList<Foo>(); }  public static class Foo implements Comparable<Foo> {     private int i;     private int j;      private Foo(int i, int j) {         super();         this.i = i;         this.j = j;     }      public int min() {         if (i < j)             return i;         else             return j;     }      public int max() {         if (i > j)             return i;         else             return j;     }      @Override     public String toString() {         return 'I=' + i + 'J=' + j;     }      @Override     public int compareTo(Foo o) {         if (this.min() > o.min()) {             return 1;         } else if (this.min() < o.min())             return -1;         else {             if (this.max() > o.max())                 return 1;             else if (this.max() < o.max())                 return -1;             else                 return 0;         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 65k
  • Answers 65k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Rails is all about convention over configuration. You have to… May 11, 2026 at 11:20 am
  • added an answer It is encouraged to generate random IVs for each encryption… May 11, 2026 at 11:20 am
  • added an answer I did this, essentially we had a page that the… May 11, 2026 at 11:20 am

Related Questions

I have a HashMap with millions of entries. Need to retrieve all entries whose
I have a HashMap that I am serializing and deserializing to an Oracle db,
I have a HashMap (although I guess this question applies to other collections) of
I have a HashMap relating Keys to Strings, and I need to compare some
I have a variable of type Hashmap <String,Integer >. In this, the Integer value
Suppose I have a hash_map and a code like // i is an iterator
I have a web-service that I will be deploying to dev, staging and production.
I have a .Net desktop application with a TreeView as one of the UI
I have a Queue<T> object that I have initialised to a capacity of 2,
I have a complete XML document in a string and would like a Document

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.