I am looking for a Java implementation of Set that provides a search on the properties of elements. Thinking in terms of Guava, it can be created using Function<Element, SearchKey> (it is expected to be unique to all elements of the set) and will provide a find(SearchKey key) method that returns an Element for which the function will return key .
Obvious assumptions that must be satisfied:
- The result of the
function(element) is constant for the entire lifetime of the Element in the set. Function - gives unique results for all elements of the set
Cause:
Sometimes there is a need for Set<Element> , and the type of the field cannot be changed to Map<SearchKey, Element> (for example, in the JPA entity or in the case of the 4th batch code). However, when building such an object, you can safely use your own implementation of Set with Map capabilities.
Alternatives:
There are several alternatives that I have already found, none of which seem perfect
- does not have
Map capabilities - using linear search to implement find(SearchKey) (works with each implementation of Set :) - using a
TreeSet with Comparator comparing SearchKeys is a bit like a hack , especially since it no longer respects element equality
the "find" method is called ceiling and requires you to build an artificial Element for search purposes (uogh ...) - "equivalence set" ( http://code.google.com/p/guava-libraries/issues/detail?id=576 ) - but this is not implemented and does not seem to be
( If you want to answer that you do not know more alternatives - save your time and do not do this. This is what I already know, I can not accept your answer.)
source share