java - 我可以在Java中为不同的List<>创建通用构造函数吗?

标签 java generics

我在一个类中有一个函数,我在其中声明了 List<A>,List<B>,List<C>........到 1000 个,分别在单独的 For 循环中列出它们。

我已经初始化了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;
    }

关于java - 我可以在Java中为不同的List<>创建通用构造函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58021578/

相关文章:

java - 这是通用的自动装箱吗?

java - 在java中读/写图像

java - 使用 PropertySourcesPlaceholderConfigurer 时如何解密属性值

ios - 如何使用泛型协议(protocol)作为变量类型

java - 在方法中使用@SuppressWarnings 会产生编译错误

c# - 无法将 List<Foo> 传递给需要 List<IFoo> 的方法,其中 Foo : IFoo

java - 为什么我无法将 SuperType 添加到 <T extends SuperType> 的 List<T> 中?

java - 使用堆栈将中缀转换为后缀

java - JMVTI :Could not find agent library

java - 使用 Stream.reduce 求逆和不正确