构建作为 HashMap 但在输入和输出上转换值的 Java Map 的正确方法是什么?

发布于 2024-11-20 00:43:54 字数 743 浏览 7 评论 0原文

我基本上需要一个稍微修改过的 HashMap 版本,每当读取或写入一个值时,都需要对其进行转换。到目前为止,我已经尝试了以下方法:

public class MyMap implements Map<String, Object> {

    private HashMap<String, MyType> map;

    public Object get(Object key) {
        return map.get(key).getValue();
    }

    public Object put(String key, Object value) {
        return map.put(key, MyType.wrap(value)).getValue();
    }

    ... all other methods of the Map interface are handled by map
}

我希望这就是我需要做的全部,但我正在努力解决这两种方法:

Set<Entry<String,Object>> entrySet()
Collection<Object> values()

我不能直接转发到地图,因为值需要一一转换。当然,我基本上可以从 HashMap 复制实现并修改它以满足我的需要,但这似乎是相当丑陋的解决方案,在我这样做之前我想确保我没有错过一个更好的解决方案。

还有更好的办法吗?

I basically need a slightly modified version of a HashMap where whenever a value is read or written it needs to be converted. So far I have tried the following:

public class MyMap implements Map<String, Object> {

    private HashMap<String, MyType> map;

    public Object get(Object key) {
        return map.get(key).getValue();
    }

    public Object put(String key, Object value) {
        return map.put(key, MyType.wrap(value)).getValue();
    }

    ... all other methods of the Map interface are handled by map
}

I was hoping that was all I needed to do, but I am struggling with those two methods:

Set<Entry<String,Object>> entrySet()
Collection<Object> values()

Those I can't just forward to map as the values need to be converted one by one. Of course I could basically copy the implementation from HashMap and modify it to fit my needs, but that seemed to be rather ugly solution and I before I do that I wanted to make sure I am not missing a much nicer solution.

Is there any better way?

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

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

发布评论

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

评论(3

刘备忘录 2024-11-27 00:43:54

您可以将哈希图包装在一个类中,并公开该类上的方法以进行添加或删除。

像这样

Class MyHashmap {

private Map map = new HashMap();

//methods for add/remove to wrap and put in HashMap
public void add(String ,Object) {
  map.put(key, MyType.wrap(value)).getValue();

}

//同样的方式你可以添加删除方法。

you can wrap the hashmap in a class and expose method on that class to add or remove.

like this

Class MyHashmap {

private Map map = new HashMap();

//methods for add/remove to wrap and put in HashMap
public void add(String ,Object) {
  map.put(key, MyType.wrap(value)).getValue();

}

//same way you can add method for remove.

小红帽 2024-11-27 00:43:54

看看 Apache Commons TransformedMap并看看它是否符合您的需求。

Take a look at Apache Commons TransformedMap and see if it fits your needs.

反话 2024-11-27 00:43:54

你所做的事情本身就很丑陋,所以我不会担心一个稍微丑陋的实现。要么按照您的建议进行操作,要么让 get/put 方法将转换后的值放入映射中,然后其他方法只是简单的包装器。

What you're doing is per se quite ugly, so I wouldn't worry to death about a slightly ugly implementation. Either do what you're suggesting, or else make the get/put methods put the converted values in the map, and then the other methods are just simple wrappers.

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