在 Java 中的 List 中存储不同类型的元素 [英] Storing different types of elements in a List in Java

查看:93
本文介绍了在 Java 中的 List 中存储不同类型的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个通用的表加载器,其架构在运行时是已知的.这需要有一个包含不同类型元素列表并支持各种 get 和 set 方法的类,例如 getInt(int index)asString(int index)、<代码>asStringList(int index).我考虑的元素类型是 IntegerDoubleStringListListList.每个元素的实际类型在运行时是已知的,我会将它们存储在一个描述其架构的列表中以供进一步处理.

我的问题是:我应该将这样的元素列表存储在 List 还是 List?或者有没有更好的方法来实现这样的类?

解决方案

由于您的类的共同祖先是 Object,并且因为 List 并没有让事情变得更清晰(毕竟,一切都扩展了 Object),看起来 List 是一个不错的选择.>

然而,这样的列表将是一个混合包:您需要检查内部对象的运行时类型,并基于此做出决定.这绝对不是什么好事.

更好的选择是创建自己的类,以统一的方式实现对列表元素的操作,并为每个子类型创建一个子类,以不同的方式实现这些操作.这会让您以统一的方式处理列表,将每个对象的差异推入您的包装器中.

公共接口ItemWrapper {int 计算Something();}公共抽象类 IntWrapper 实现了 ItemWrapper {私有整数值;公共 IntWrapper(int v) {值=v;}公共整数计算某事(){返回值;}}公共抽象类 DoubleListWrapper 实现了 ItemWrapper {私人列表<双>列表;公共 DoubleListWrapper (List lst) {列表 = lst;}公共整数计算某事(){内部资源;对于(双 d:列表){资源 += d;}返回资源;}}//...等等

现在你可以制作一个 ItemWrapper 对象的列表,并在它们上面calculateSomething 而无需检查它们的类型:

ListmyList = new ArrayList();for (ItemWrapper w : myList) {System.out.println(w.calculateSomething());}

I'm trying to develop a general table loader which schema is known at runtime. This requires having a class which contains a list of different types of elements and supports various get and set method such as getInt(int index), asString(int index), asStringList(int index). The types of elements I consider are Integer, Double, String, and List<Integer>, List<Double> and List<String>. The actual type of each element is known in run time, and I will store them in a List describing its schema for further processing.

My question is: should I store such list of elements in List<Object> or List<? extends Object>? or is there better way to implement such class?

解决方案

Since the common ancestor of your classes is Object, and because List<? extends Object> does not make things any cleaner (after all, everything extends Object) it looks like List<Object> would be an OK choice.

However, such list would be a mixed bag: you would need to check run-time type of the object inside, and make decisions based on that. This is definitely not a good thing.

A better alternative would be creating your own class that implements operations on elements of the list the uniform way, and make one subclass for each subtype that implements these operations differently. This would let you treat the list in a uniform way, pushing the per-object differentiation into your wrappers.

public interface ItemWrapper {
    int calculateSomething();
}

public abstract class IntWrapper implements ItemWrapper {
    private int value;

    public IntWrapper(int v) {
      value=v; 
    }

    public int calculateSomething() {
      return value;
    }
}

public abstract class DoubleListWrapper implements ItemWrapper {
    private List<Double> list;

    public DoubleListWrapper (List<Double> lst) {
      list = lst; 
    }

    public int calculateSomething() {
        int res;
        for (Double d : list) {
            res += d;
        }

        return res;
    }
}
// ...and so on

Now you can make a list of ItemWrapper objects, and calculateSomething on them without checking their type:

List<ItemWrapper> myList = new ArrayList<ItemWrapper>();

for (ItemWrapper w : myList) {
    System.out.println(
      w.calculateSomething());
}

这篇关于在 Java 中的 List 中存储不同类型的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆