从泛型类集合中的java类型安全检索

b1uwtaje  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(262)

我有一个通用容器,其中包含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");
hmmo2u0o

hmmo2u0o1#

我的解决方案。我想这很符合我的需要:

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

相关问题