I'd like to know whether it's possible to discard some values when we use the Guava method.
public static Function<Locale, String> GET_LANGUAGE = new Function<Locale, String>() {
@Override
public String apply(Locale input) {
return StringUtils.isBlank(input.getLanguage()) ? "NO_LANGUAGE" : input.getLanguage();
}
};
Maps.uniqueIndex(availableLocales, GET_LANGUAGE);
I know I could have used a multimap since many locales can have the same language but it fits my needs actually.
For exemple, I'd like the locales without any language not to be in the output map. As I can't return null or things like that, I redirect the locales with non-relevant language to key "NO_LANGUAGE". The problem is that the value can still be retrieved.
Is there some kind of "/dev/null" map key, or will I always need to do some filter before/after using a predicate?
Guava contributor here.
Nope. Do a filter...or possibly more efficiently, just build the map yourself, and don't use Maps.uniqueIndex
entirely. It's probably simpler and shorter that way anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With