java - 我可以在 Java 中为不同的 List<> 制作通用构造函数吗?
问题描述
我在一个类中有一个函数,我List<A>,List<B>,List<C>........
在单独的 For 循环中声明了 1000 个列表。
我已经初始化了 cacheMetaDataList 以放置所有数据:
List<CacheMetaData> cacheMetaDataList=new ArrayList<>();
我正在添加 For 循环的所有数据:
cacheMetaDataList.add(new CacheMetaData(key,size,value));
CacheMetaDataImpl.java 类
public List<CacheMetaData> getAllCacheName(){
List<CacheMetaData> cacheMetaDataList=new ArrayList<>();
for( Cache.Entry<String, GenericClassForList> entry : this.operatingParametersCache) {
String key = entry.getKey();
List<A> value = entry.getValue();
int size=value.size();
cacheMetaDataList.add(new CacheMetaData(key,size,value));
}
for( Cache.Entry<String, GenericClassForList> entry : this.securitiesTradingParameterCache) {
String key = entry.getKey();
List<B> value = entry.getValue();
int size=value.size();
cacheMetaDataList.add(new CacheMetaData(key,size,value));
}
for( Cache.Entry<String, GenericClassForList> entry : this.marketCloseStatisticsCache) {
String key = entry.getKey();
List<C> value = entry.getValue();
int size=value.size();
cacheMetaDataList.add(new CacheMetaData(key,size,value));
}
return cacheMetaDataList;
}
问题不在这个类,但问题从这个类开始:
package com.cache.model;
import com.tms.component.securitiestradingparams.entity.SecuritiesTradingParamDTO;
import java.util.List;
public class CacheMetaData {
private String cacheName;
private int count;
private List<A> aList;
private List<B> bList;
private List<C> cList;
public CacheMetaData(String cacheName, int count,List<A> a) {
this.cacheName = cacheName;
this.count = count;
tlis.aList=a;
}
public CacheMetaData(String cacheName, int count,List<A> b) {
this.cacheName = cacheName;
this.count = count;
tlis.bList=b;
}
public CacheMetaData(String cacheName, int count,List<A> c) {
this.cacheName = cacheName;
this.count = count;
tlis.cList=c;
}
}
我在课堂上遇到的问题是每个 new CacheMetaData(key,size,value);
. 假设如果有 1000 个列表,那么我需要创建 1000 个构造函数。我需要一个可以初始化不同对象列表的通用构造函数。为不同的列表创建 1000 个构造函数将是非常简单的代码。
解决方案
你为什么不去买一个通用的?
package com.cache.model;
import com.tms.component.securitiestradingparams.entity.SecuritiesTradingParamDTO;
import java.util.List;
public class CacheMetaData<CACHE> {
private String cacheName;
private int count;
private List<CACHE> cache;
public CacheMetaData(String cacheName, int count,List<CACHE> cache) {
this.cacheName = cacheName;
this.count = count;
tlis.cache = cache;
}
所以在 CacheMetaDataImpl.java 类
public List<CacheMetaData> getAllCacheName(){
List<CacheMetaData> cacheMetaDataList=new ArrayList<>();
for( Cache.Entry<String, GenericClassForList> entry : this.operatingParametersCache) {
String key = entry.getKey();
List<operatingParametersCache> value = entry.getValue();
int size=value.size();
cacheMetaDataList.add(new CacheMetaData<operatingParametersCache>(key,size,value));
}
for( Cache.Entry<String, GenericClassForList> entry : this.securitiesTradingParameterCache) {
String key = entry.getKey();
List<securitiesTradingParameterCache> value = entry.getValue();
int size=value.size();
cacheMetaDataList.add(new CacheMetaData<securitiesTradingParameterCache>(key,size,value));
}
for( Cache.Entry<String, GenericClassForList> entry : this.marketCloseStatisticsCache) {
String key = entry.getKey();
List<marketCloseStatisticsCache> value = entry.getValue();
int size=value.size();
cacheMetaDataList.add(new CacheMetaData<marketCloseStatisticsCache>(key,size,value));
}
return cacheMetaDataList;
}
推荐阅读
- buildfire - Buildfire:条形码扫描服务在 Previewer 应用程序中工作,但在 android 上的内置应用程序中不工作
- ios - 如果我创建一个新窗口并 makeKeyAndVisible,前一个窗口会发生什么
- python - Python:参数的参数字符串
- pandas - 使用 Numpy 计算中位数的命名变量
- microsoft-graph-api - 无法使用 Beta Graph API 删除 AppRoleAssignment
- keras - 如何调试“ValueError:检查输入时出错”
- google-sheets-api - Google Sheets API 失败并显示 500
- java - BinarySearchTree 中的 balance 方法有什么问题?(爪哇)
- reactjs - 如何用子项目构建项目?
- python - 如何在其他文本之间抓取特定文本?