首页 > 解决方案 > 我可以在 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 个构造函数将是非常简单的代码。

标签: javagenerics

解决方案


你为什么不去买一个通用的?

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;
    }

推荐阅读