在 Java 中从键值对构建映射

发布于 2024-12-27 06:10:34 字数 485 浏览 0 评论 0原文

可能的重复:
HashMap 构建器

是否有任何实用程序类允许从多个键值对创建 Map以方便且可读的方式?

我认为 guava 应该包含一些东西,但我找不到任何具有必要功能的东西。

我想要的是这样的:

MapBuilder.newHashMap()
  .with("key1", 10)
  .with("key2", 20)
  .with("key3", 30)
  .build();

PS 我也知道双括号方法 (new HashMap<>() {{ put(..); put(..); }}) 但是我觉得它既不可读也不方便。

Possible Duplicate:
builder for HashMap

Are there any utility class which allows to create a Map from a number of key-value pairs in a convenient and readable manner?

I thought that guava should have contain something but I couldn't find anything with necessary functionality.

What I want is something like this:

MapBuilder.newHashMap()
  .with("key1", 10)
  .with("key2", 20)
  .with("key3", 30)
  .build();

P.S. I also know about double-brace approach (new HashMap<>() {{ put(..); put(..); }}) but I don't find it either readable or convenient.

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

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

发布评论

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

评论(4

疯到世界奔溃 2025-01-03 06:10:34

有什么问题吗?

Map<String, Integer> map = new HashMap<>();
map.put("key1", 10);
map.put("key2", 20);
map.put("key3", 30);

这对我来说看起来非常可读,而且我看不出您从 MapBuilder 中获得了什么。无论如何,这样的 MapBuilder 并不难实现。

What's wrong with

Map<String, Integer> map = new HashMap<>();
map.put("key1", 10);
map.put("key2", 20);
map.put("key3", 30);

That looks very readable to me, and I don't see what you gain from your MapBuilder. Anyway, such a MapBuilder wouldn't be hard to implement.

百合的盛世恋 2025-01-03 06:10:34

为什么不自己推出呢?

public class MapBuilder<K,V> {

    private Map<K,V> map;

    public static <K,V> MapBuilder<K,V> newHashMap(){
            return new MapBuilder<K,V>(new HashMap<K,V>());
    }

    public MapBuilder(Map<K,V> map) {
        this.map = map;
    }

    public MapBuilder<K,V> with(K key, V value){
        map.put(key, value);
        return this;
    }

    public Map<K,V> build(){
        return map;
    }

}

Why not just roll your own?

public class MapBuilder<K,V> {

    private Map<K,V> map;

    public static <K,V> MapBuilder<K,V> newHashMap(){
            return new MapBuilder<K,V>(new HashMap<K,V>());
    }

    public MapBuilder(Map<K,V> map) {
        this.map = map;
    }

    public MapBuilder<K,V> with(K key, V value){
        map.put(key, value);
        return this;
    }

    public Map<K,V> build(){
        return map;
    }

}
油饼 2025-01-03 06:10:34

使用返回 this 的 put 方法创建您自己的 AbstractMap 怎么样?

public class MyMap<K, V> extends AbstractMap<K, V>{

    @Override
    public Set<java.util.Map.Entry<K, V>> entrySet() {
        // return set
        return null;
    }

    public MyMap<K, V> puts(K key, V value) {
        this.put(key, value);
        return this;
    };

}

然后使用该方法来链接对:

new MyMap<String, String>()
            .puts("foo", "bar")
            .puts("Hello", "World");

How about creating your own AbstractMap with a put method that returns this?

public class MyMap<K, V> extends AbstractMap<K, V>{

    @Override
    public Set<java.util.Map.Entry<K, V>> entrySet() {
        // return set
        return null;
    }

    public MyMap<K, V> puts(K key, V value) {
        this.put(key, value);
        return this;
    };

}

Then use that method to chain pairs:

new MyMap<String, String>()
            .puts("foo", "bar")
            .puts("Hello", "World");
终遇你 2025-01-03 06:10:34

来自头部,未测试:

import java.util.HashMap;

public class MapBuilder<K, E> {
    private HashMap<K, E>       m_hashMap;

    public static HashMap newHashMap(Class<K> keyClass, Class<E> elementClass) {
        return new MapBuilder<K, E>();
    }

    public MapBuilder() {
        m_hashMap = new HashMap<K, E>();
    }

    public MapBuilder with(K key, E element) {
        m_hashMap.put(key, element);

        return this;
    }

    public HashMap<K, E> build() {
        return m_hashMap;
    }
}

用法:

HashMap<String, Integer> myMap = MapBuilder.newHashMap(String.class, Integer.class)
    .with("key1", 10)
    .with("key2", 20)
    .with("key3", 30)
    .build();

from head, not tested:

import java.util.HashMap;

public class MapBuilder<K, E> {
    private HashMap<K, E>       m_hashMap;

    public static HashMap newHashMap(Class<K> keyClass, Class<E> elementClass) {
        return new MapBuilder<K, E>();
    }

    public MapBuilder() {
        m_hashMap = new HashMap<K, E>();
    }

    public MapBuilder with(K key, E element) {
        m_hashMap.put(key, element);

        return this;
    }

    public HashMap<K, E> build() {
        return m_hashMap;
    }
}

usage:

HashMap<String, Integer> myMap = MapBuilder.newHashMap(String.class, Integer.class)
    .with("key1", 10)
    .with("key2", 20)
    .with("key3", 30)
    .build();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文