Is there an IndexSet and a Range class for Java?

2019-06-19 23:37发布

问题:

In Objective-C Cocoa we have the NSIndexSet class which stores a series of unique indexes efficiently by keeping an array of ranges. E.g. the set 1, 2, ... 30, 57 would be stored as the ranges 1-30 and 57 rather than an array of 32 numbers. This facilitates huge selections to be stored in a simple and fast way. For example, if all rows between 1 and a million in a table selected, the index set collapses to just a tiny range and is fast to compare and intersect with.

Unfortunately this turns out to be rather hard to Google for. Is there an equivalent class for Java?

回答1:

It seems a useful class, and I don't recall a standard implementation.

Here's a bunch of some -perhaps useful- pointers.

A range intersection algorithm better than O(n)?

http://www.codeproject.com/KB/recipes/rangeset.aspx

http://healpix-rangeset.googlecode.com/svn/trunk/healpix-rangeset/src/org/asterope/healpix/LongRangeSet.java

http://pcj.sourceforge.net/docs/api/bak/pcj/set/IntRangeSet.html

Data structure to build and lookup set of integer ranges

Representing sparse integer sets?

http://www.iis.uni-stuttgart.de/intset/doc/intset/TreeIntegerSet.html



回答2:

There is the Apache commons IntRange



回答3:

Certainly not last and not least, there is a Range class in the Guava library. This article does a nice job of illustrating how you might use it.