mtbc: maze I (white-red)
Mark T. B. Carroll ([personal profile] mtbc) wrote2020-08-28 08:47 am
Entry tags:

Java standard libraries for prefix search

For a new capability in OMERO.blitz I have a set of string prefix transformations where if a string starts with one then that prefix should be replaced by its partner. Abstractly, this seems easy to implement efficiently: for example, I can store the prefixes in a balanced search tree then, when I have a string whose prefix may need substituting, I can search for it in the tree and that largely gets me to its applicable prefix, if any.

I wondered if the Java standard libraries hand this to me on a plate but it took me some sleep to see how. Eventually I realized that I can roll my own Comparator that returns 0 (for is equal to) if startsWith and use it in a TreeMap that has from prefixes as keys with the same from and its to prefix together as each value. This approach violates equals but that is fine, it works nicely.

Post a comment in response:

This account has disabled anonymous posting.
(will be screened if not on Access List)
(will be screened if not on Access List)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org