java - Efficient way to get all map values whose keys match a criterion -
i have following map map<indexderivedkey, collection<data<d>>> indexeddata
in indexable data structure.
indexderivedkey
conforms index
, index consists of keys , corresponding values. keys used extract values elements in indexable data structure.
for example there's index, consists of keys firstname
, lastname
, use index extract values objects, possess these attributes, operation yields indexderivedkey
per object.
that indexderivedkey
contains mapping above keys respective values , used store objects in map indexeddata
, mentioned before. map value collection type, since possible several objects equal (with respect index).
question how can extract objects firstname = "john"
, hereby ignoring value of lastname
. can iterate on keys , check value of firstname
in o(n)
.
but since indexderivedkey
{firstname = "john"}
subset of other keys firstname = "john"
, e.g. {firstname = "john", lastname = "smith"}
, suppose there has more efficient way. maybe utilizing treeset
?
1
public indexderivedkeyimpl(index index, map<string, string> keyvalues) { this.keyvalues = keyvalues; this.index = index; (string key : keyvalues.keyset()) if (!index.supportskey(key)) throw new indexkeymismatchexception(key, index); }
2
// in data.index.index(indexable) @override public indexderivedkey index(indexable data) { map<string, string> keyvalues = new hashmap<string, string>(); indexderivedkey key = new indexderivedkeyimpl(this, keyvalues); (string k : keys) { string value = data.get(k); if (value != null) keyvalues.put(k, value); } return key; }
if search on specific order, make key class comparable, , equals.
and use sortedmap
, treemap
. navigable too. might instance use submap(fromkey, tokey)
Comments
Post a Comment