繁体   English   中英

类java的多个泛型类型

[英]Multiple generic type for a class java

有没有办法在不设置最大数量的情况下使用具有泛型类型的类? 我有这门课

public class Repository<V> {
    private Map<String, HashSet<V>> repo = new HashMap<>();
    private static Repository instance = null;

    private Repository() {}
    
    public static synchronized Repository getInstance() {
        if(instance == null) {
            instance = new Repository();
        }
        
        return instance;
    }
    
    public void addRepository(String key) throws ClassNotFoundException, IOException {
        repo.put(key, new HashSet<>());
    }

    .....
}

这是一个“通用存储库”, HashMap包含一个标识符作为键,而作为值, HashSet<V>包含数据。

我希望HashMap中的每个HashSet都包含不同的类类型。 更准确地说,我希望泛型类型V对于HashMap中的每个HashSet都不同

如何修复代码以实现此结果?

您不能添加诸如Repository<V>之类的类参数并期望V对于映射中的每种类型的条目都不同。

但是,您可以执行以下操作:

从存储库中删除泛型类型:

public class Repository {
}

生成存储库映射,以便将Class<?>作为键(而不是字符串)和Set<?>作为值):

private final Map<Class<?>, Set<?>> repo = new HashMap<>();

然后,创建一个添加新存储库的方法和一个获取现有存储库的方法:

public <T> void addRepository(Class<T> key) {
    Set<?> existing = repo.putIfAbsent(key, new HashSet<>());
    if (existing != null) {
        throw new IllegalArgumentException("Key " + key + " is already associated to a repository");
    }
}

public <T> Set<T> getRepository(Class<T> key) {
    Set<?> subRepo = repo.get(key);
    if (subRepo == null) {
        throw new IllegalArgumentException("No repository found for key " + key);
    }
    return (Set<T>) subRepo; //unchecked cast
}

注意: getRepository()将执行未经检查的强制转换,但它是“安全的”未经检查的强制转换,因为将新条目添加到地图中的唯一方法是通过<T> void addRepository(Class<T> key)并且您将无法在返回的Set<T>中插入不是T的值。

示例用法:

Repository repository = Repository.getInstance();
repository.addRepository(String.class);
repository.addRepository(Integer.class);
Set<String> stringRepo = repository.getRepository(String.class);
stringRepo.add("Hey");
stringRepo.add("Jude");
Set<Integer> intRepo = repository.getRepository(Integer.class);
intRepo.add(1);
intRepo.add(4);

但是,我认为每种类型都应该有一个存储库,这会更干净,因为使用上述解决方案,您基本上根本没有利用 Java 泛型(除了getRepository方法中使用的方法<T> ,您无论如何都需要执行未经检查的演员表)。

没有办法干净地实现这一目标。 您可以为您拥有的每种类型创建一个存储库,但您不能使用此设置将它们合并到一个存储库中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM