gpt4 book ai didi

java - 我可以定义 Map> 以便我可以从 map 中 get() 列表而无需未经检查的转换?

转载 作者:行者123 更新时间:2023-11-30 02:05:48 26 4
gpt4 key购买 nike

我有以下 Java 代码来获取与多个位置的对象相关的多个集合:

Map<String, List<? extends BaseView>> additionalCollections = deployEnvironmentService.getInputPageInfo(getProjectOid());
List<MachineView> machineViews = (List<MachineView>) additionalCollections.get("machineViewCollection");
List<LevelView> levelViews = (List<LevelView>) additionalCollections.get("levelViewCollection");
List<ToolView> toolViews = (List<ToolView>) additionalCollections.get("toolViewCollection");
List<BuildEnvironmentView> buildEnvironmentViews = (List<BuildEnvironmentView>) additionalCollections
.get("buildEnvironmentViewCollection");

集合的名称和数量根据我使用的服务而有所不同,但集合始终继承自 BaseView。集合的检索方法与此类似:

public Map<String,List<? extends BaseView>> getInputPageInfo(Integer projectOid) {
Map<String,List<? extends BaseView>> allInfoMap = new HashMap<String,List<? extends BaseView>>();
PersistenceBroker broker = PersistenceBrokerFactory.createPersistenceBroker();
try {
allInfoMap.put("levelViewCollection", getLevelViewCollection(broker, projectOid));
allInfoMap.put("machineViewCollection", getMachineViewCollection(broker));
allInfoMap.put("toolViewCollection", getToolViewCollection(broker, projectOid));

allInfoMap.put("buildEnvironmentViewCollection", getBuildEnvironments(broker, projectOid));

} finally {
broker.close();
}
return allInfoMap;
}

我遇到的问题是,我总是在第一个片段中的 Map.get() 方法上收到警告,显示 Type safety: Unchecked cast from List<capture#1-of ? extends BaseView> to Collection<LevelView> 。有办法解决这个警告吗?我尝试定义类型 <T extends BaseView> ,但是我找不到在函数调用本身中使用该类型 T 的方法。

最佳答案

将 map 包装在“查找”对象中,然后输入键:

public final class PageInfoKey<T extends BaseView> {
public static final PageInfoKey<MachineView> MACHINE_VIEWS
= new PageInfoKey<>("machineViews", MachineView.class);
/*... more static final keys defined here ...*/

/* Adding the 'name' and 'type' fields,
* and equals() and hashCode() as below,
* allow us to have multiple instances of keys
* defined elsewhere if desired, which should still
* work with the lookup.
*
* If all keys are defined statically inside this class
* (i.e. constructor is made private), then these fields
* and methods are unnecessary.
*/

private final String name;
private final Class<T> type;

public PageInfoKey(String name, Class<T> type) {
this.name = name;
this.type = type;
}

@Override
public int hashCode() {
return Objects.hash(name, type);
}

@Override
public boolean equals(Object other) {
if(other==this) return true;
if(other instanceof PageInfoKey) {
PageInfoKey<?> o = (PageInfoKey<?>)other;
return Objects.equals(this.name, o.name) &&
Objects.equals(this.type, o.type);
}
return false;
}
}

public class PageInfoLookup {

private final Map<PageInfoKey<?>, List<? extends BaseView>> data = new HashMap<>();

public <T extends BaseView> void put(PageInfoKey<T> key, List<T> value) {
data.put(key, value);
}

@SuppressWarnings("unchecked")
public <T extends BaseView> List<T> get(PageInfoKey<T> key) {
return (List<T>) data.get(key);
}

}

现在您可以通过传入静态定义的键来获取 View :

List<MachineView> machineViews = lookup.get(PageInfoKey.MACHINE_VIEWS);

一切都是类型安全的,并在编译时进行检查,前提是您仅通过查找类中的公共(public)方法修改/访问映射。也不需要只在 PageInfoKey 类中定义键 - 只要该类上有合理定义的 equalshashCode 方法即可,您可以在任何地方定义新实例,包括在依赖库中。

Aleksander指出,如果您知道映射中最多只会有一个特定类型的列表,那么您可以使用 Class 本身作为键。

关于java - 我可以定义 Map<String, List< 吗?扩展 BaseView>> 以便我可以从 map 中 get() 列表而无需未经检查的转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51419061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com