I need a multi map which keys are case insensitive. is there such implementation in google collections?
Here is a case insensitive version of a ForwardingMap
:
public class CaseInsensitiveForwardingMap<V> extends ForwardingMap<String, V>
implements Serializable{
private static final long serialVersionUID = -7741335486707072323L;
// default constructor
public CaseInsensitiveForwardingMap(){
this(new HashMap<String, V>());
}
// constructor with a supplied map
public CaseInsensitiveForwardingMap(final Map<String, V> inner){
this.inner = inner;
}
private final Map<String, V> inner;
@Override
protected Map<String, V> delegate(){
return inner;
}
// convert keys to lower case Strings, preserve null keys
private static String lower(final Object key){
return key == null ? null : key.toString().toLowerCase();
}
@Override
public V get(final Object key){ return inner.get(lower(key)); }
@Override
public void putAll(final Map<? extends String, ? extends V> map){
if(map == null || map.isEmpty()){ inner.putAll(map); }
else{
for(final Entry<? extends String, ? extends V> entry :
map.entrySet()){
inner.put(lower(entry.getKey()), entry.getValue());
}
}
}
@Override
public V remove(final Object object){ return inner.remove(lower(object)); }
@Override
public boolean containsKey(final Object key){
return inner.containsKey(lower(key));
}
@Override
public V put(final String key, final V value){
return inner.put(lower(key), value);
}
}
Using this map, you can create the MultiMap
using the Supplier methods in MultiMaps
.
Example:
Map<String, Collection<String>> map =
new CaseInsensitiveForwardingMap<Collection<String>>();
Multimap<String, String> caseInsensitiveMultiMap =
Multimaps.newMultimap(map, new Supplier<Collection<String>>(){
@Override
public Collection<String> get(){ return Sets.newHashSet(); }
});
Caveat: keySet()
will return lowercase values only, regardless how the keys were entered.
Couldn't you use a Map<String,List<Payload>>
and give it a Comparator<String>
which did a case-insensitive compare?
It appears that neither Google Collections nor Apache Collection frameworks have a multimap that accepts a Comparator for evaluating key equality.
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