[Biojava-dev] SortedMap comparator question

Michael L. Heuer heuermh@acm.org
Wed, 9 Oct 2002 18:28:27 -0400 (EDT)


On Wed, 9 Oct 2002, Thomas Down wrote:

> I don't think there's anything specifically illegal about doing
> this (although do, of course, check the javadocs).  I do, however,
> have a couple of concerns about how this will work out in practice.
> IIRC, TreeMap really is a tree-based data structure, which is
> accessed by comparing a new object to objects already in the tree
> (using the specified comparator).  If you look up an unknown key,
> the comparator you list below will fail (NullPointerException).
> Obviously, this is fixable (just make unknown keys sort above or
> below all defined keys).  However, more seriously, I think the
> same situation might also apply during inserts.  In this case,
> the obvious workaround doesn't help, since it means that they new
> entry will probably be inserted into the wrong part of the tree.

Yes, you're right -- this is a problem.

> How about defining a little datablob class which is an
> Object-double pair. defining a comparator over those, then
> just using a SortedSet?

I had thought about this, but the convenience of the keys and entrySet
views was desireable.

What I'm going for is something like

interface ScoredSet extends Set (?)
{
  public double getScore(Object o);
  public double minScore();
  public double maxScore();
  public ScoredSet subSet(double fromScore, double toScore);
}

and

interface RankedSet extends ScoredSet
{
  public int getRank(Object o);
  public int minRank();
  public int maxRank();
  public RankedSet subSet(int fromRank, int toRank);
}

providing

		score   rank
object1		100.0	1
object2 	90.0	2
object3 	90.0	2
object4		75	3


The Set contract is nice in that in that it provides for non-duplicate
elements, but it might not make sense to view this as a simple Collection
after all.

   michael