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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:48:39+00:00 2026-06-18T12:48:39+00:00

I am looking for a collection class for the scenario below: Fast collection lookup,

  • 0

I am looking for a collection class for the scenario below:

  • Fast collection lookup, one item at a time.
  • The collection contains around 300 K items.
  • Collection population speed might not be important, but ideally fast too.
  • No update/delete/insert is needed once collection is loaded

Example of items of type Ip2Location that would be populated into the collection:

public class Ip2Location
{
   public long IpFrom {get; set;}
   public long IpTo {get; set;}
   public string Country {get; set;}  
}

IpFrom      IpTo        Country
16909056    16909311    AU
16909312    16941055    US

Item lookup against the collection is done via a specified IP, like this:

IpFrom < currentIp < IpTo

Any ideas, including reference links, would be very much appreciated!

Comparison: HashSet, SortedSet

Are there any better collection classes?

Reference: the comparison table in the link below:
http://geekswithblogs.net/BlackRabbitCoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx

Update:

Problem using Array.BinarySearch:

var index = Array.BinarySearch(ipCountries, new IpCountry { IpFrom = 16909056}, new Ip2LocationComparer());

It works in small numbers of row, doesn’t work in 300k items (e.g. index is -(totalrow+1) ). The search item is loaded within the 300 K items collection.

        public class Ip2LocationComparer: IComparer<IpCountry>
        {
            public int Compare(IpCountry x, IpCountry y)
            {
                if (x != null && y != null)
                    return (x.IpFrom <= y.IpFrom && y.IpFrom <= x.IpTo)? 0 : -1;

                return -1;

            }
        }

Update 2

I changed it to below

public class Ip2LocationComparer: IComparer<IpCountry>
            {
                public int Compare(IpCountry x, IpCountry y)
                {
       if (x != null && y != null)

            {
                if (x.IpFrom > y.IpFrom)
                    return 1;

                if (x.IpFrom < y.IpFrom)
                    return -1;

                if (x.IpFrom == y.IpFrom)
                {
                    if (y.IpFrom > x.IpTo)
                        return 1;

                    if (y.IpFrom < x.IpTo)
                        return -1;

                }

            }

            return 0;
}

But the index return from BinarySearch is still nagtive, which is right between the matching item and the follow item. e.g. if my search IpFrom is 3, the index is between 2 and 4. Why it doen’t return 2? I haven’t test the IpTo scenario yet.

Any idea would be appreciated!

  • 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-18T12:48:41+00:00Added an answer on June 18, 2026 at 12:48 pm

    You could store this in an array.

    If you sorted the array after populating, then a BinarySearch would be a very fast way to lookup where your currentIp falls.

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

Sidebar

Related Questions

I'm looking for an interval tree C# collection class. I need to be able
I'm looking for a function, class or collection of functions that will assist in
I'm looking for a class from the Java Collection Framework that would not allow
I'm looking for a best practise way to handle incoming time series data. One
In .NET (VB), how can I take all of the items in one collection,
I'm looking to have a collection of objects that implement a certain interface, but
I am looking for what most people use as their collection type when making
I am looking for the proper term to describe this well-known property of collection
I'm looking to take a website source and populate an array with a collection
I'm looking for a fast, embeddable key/value store with cursor semantics over key collections

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.