Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a case insensitive multimap in google collections

I need a multi map which keys are case insensitive. is there such implementation in google collections?

like image 937
oshai Avatar asked Jan 04 '11 10:01

oshai


2 Answers

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.

like image 63
Sean Patrick Floyd Avatar answered Sep 27 '22 19:09

Sean Patrick Floyd


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.

like image 29
David Bullock Avatar answered Sep 27 '22 21:09

David Bullock