谷歌集合中是否有不区分大小写的多重映射

发布于 2024-10-10 09:34:10 字数 38 浏览 7 评论 0 原文

我需要一个多映射,其键不区分大小写。谷歌集合中有这样的实现吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恋竹姑娘 2024-10-17 09:34:10

以下是<的不区分大小写的版本code>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);
    }
}

使用此映射,您可以使用 MultiMaps

示例:

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(); }

  });

警告:keySet() 将仅返回小写值,无论键是如何输入的。

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.

很糊涂小朋友 2024-10-17 09:34:10

您不能使用 Map> 并给它一个 Comparator 来进行不区分大小写的比较吗?

看来 Google Collections 和 Apache Collection 框架都没有接受用于评估键相等性的比较器的多重映射。

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.

百合的盛世恋 2024-10-17 09:34:10

您可以使用

You could define a case-insensitive String Comparator using a Collator. Then create a TreeMultimap with keys sorted by that Comparator.

傲娇萝莉攻 2024-10-17 09:34:10

不,但想必您正在使用字符串键?如果是这样,为什么不规范对常规多重映射的所有访问呢?对于 80% 的情况,这将使所有调用 put 和 gets 小写密钥。

有关不区分大小写的多重映射问题的完整讨论,请参阅 此 Google 群组讨论

No, but presumably you're using String keys? If so, why not just normalise all access to a regular multimap? For the 80% case, that'll be making all calls puts and gets lowercase the key.

For a full discussion of the issues with case-insensitive multimaps, see this google group discussion

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文