【问题标题】:Type-safe retrieval from collection of generic class从泛型类的集合中进行类型安全的检索
【发布时间】:2021-03-17 08:23:48
【问题描述】:

我有一个通用的容器,它包含 E 类型的对象:

public class Container<E> {

    private final E e;

    public Container(E e) {
        this.e = e;
    }

    public E get() {
        return e;
    }
}

我有一个装各种容器的桶:

public class Bucket {
    private final Map<String, Container<?>> containers = new HashMap<>();

    public void put(String name, Container<?> container) {
        containers.put(name, container);
    }

    public Container<?> get(String name) {
        Container<?> container = containers.get(name);
        return container;
    }
}

我希望能够将(各种类型的)容器放入存储桶并以类型安全的方式取回它们。

    Container<Long> longs = new Container<>(100L);
    Container<String> strings = new Container<>("Hello");

    Bucket bucket = new Bucket();
    bucket.put("longs", longs);
    bucket.put("strings", strings);

但你可以,我失去了类型安全:

   Container<?> longs1 = bucket.get("longs");
   Container<?> strings1 = bucket.get("strings");

我似乎无法弄清楚需要什么才能让我实现以下目标:

   Container<Long> longs1 = bucket.get("longs");
   Container<String> strings1 = bucket.get("strings");

【问题讨论】:

标签: java generics collections


【解决方案1】:

我的解决方案。我想它很好地满足了我的需求:

public class Container<E> {

    private final E e;

    public Container(E e) {
        this.e = e;
    }

    public <T> T get(Class<T> target) {
        if (target.isAssignableFrom(e.getClass())) {
            return (T) e;
        }
        throw new ClassCastException(e.getClass().getName() + " '" + e + "' cannot be converted to " + target.getName());
    }
}
public class Bucket {
    private final Map<String, Container<?>> containers = new HashMap<>();

    public void put(String name, Container<?> container) {
        containers.put(name, container);
    }

    public Container<?> getContainer(String name) {
        return containers.get(name);
    }
}

进行测试:

Container<Long> longs = new Container<>(100L);
Container<String> strings = new Container<>("Hello");

Bucket bucket = new Bucket();
bucket.put("longs", longs);
bucket.put("strings", strings);

Container<?> longContainer = bucket.getContainer("longs");
Long aLong = longContainer.get(Long.class);
log.debug("{}", aLong); // Prints 100

Container<?> stringContainer = bucket.getContainer("strings");
String aString = stringContainer.get(String.class);
log.debug("{}", aString); // Prints Hello

log.debug("{}", stringContainer.get(Long.class)); // Throws java.lang.ClassCastException: java.lang.String 'Hello' cannot be converted to java.lang.Long

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    相关资源
    最近更新 更多