简体   繁体   English

如何在 Java 中给出任何 object 的列表作为参数?

[英]How to give a List of any object as parameter in Java?

I have several classes that are doing the same thing: iterates over a List in an object, and add each items in a private field.我有几个类在做同样的事情:迭代 object 中的列表,并将每个项目添加到私有字段中。 I have two objects: MyCustomObject, that have several fields, and ResultOfQuery, where it has a field called data that is a List<Map<String, Object>> .我有两个对象:MyCustomObject,它有几个字段,以及 ResultOfQuery,它有一个名为 data 的字段,它是List<Map<String, Object>>

For example :例如

private List<MyCustomObject> myCustomObjectList = new LinkedList();

public void setMyCustomObject (ResultOfQuery resultOfQuery){
    ObjectMapper objectMapper = new ObjectMapper();
    if(resultOfQuery!= null) {
        for (Map<String, Object> map : resultOfQuery.getData()) {
            myCustomObjectList.add(objectMapper.convertValue(map, 
                    MyCustomObject.class));
        }
    }

The problem is that I have other classes that does the exact same method, but with another object instead of MyCustomObject.问题是我有其他类执行完全相同的方法,但使用另一个 object 而不是 MyCustomObject。

So I thought that a good idea would be that all of these classes should extends a class that contains this method, and as a parameter it should take first a resultOfQuery, then a list of any objects, and then a Class.所以我认为一个好主意是所有这些类都应该扩展一个包含此方法的 class,并且作为参数,它应该首先采用 resultOfQuery,然后是任何对象的列表,然后是 Class。 Does it sounds good, or there is a better way to achieve this?听起来不错,还是有更好的方法来实现这一目标?

Also, how to give a list of any object?另外,如何列出任何 object? I tried List<?> , but this shows me the following error:我尝试了List<?> ,但这向我显示了以下错误:

Error at list add line列表添加行出错

You can achieve type safety with generics and inheritance.您可以使用 generics 和 inheritance 实现类型安全。 If you declare a base class having the common stuff like:如果您声明具有以下常见内容的基本 class :

public class BaseClass<T> {
    
    private List<T> tList = new LinkedList<>();
    private final Class<T> classT;
    
    public BaseClass(Class<T> classT) {
        this.classT = classT;
    }
    
    public void setObject (ResultOfQuery resultOfQuery){
        ObjectMapper objectMapper = new ObjectMapper();
        if(resultOfQuery!= null) {
            for (Map<String, Object> map : resultOfQuery.getData()) {
                tList.add(objectMapper.convertValue(map, classT));
            }
        }
    } 
}

Then it is easy to extend it for each different type, like:然后很容易为每种不同的类型扩展它,例如:

public class MyCustomObjectExtendedClass extends BaseClass<MyCustomObject> {
    public MyCustomObjectExtendedClass() {
        super(MyCustomObject.class);
    }
}

I have renamed stuff because it was decoupled from the MyCustomObject .我重命名了东西,因为它与MyCustomObject分离。

You can use private List myCustomObjectList = new LinkedList();您可以使用private List myCustomObjectList = new LinkedList();

Instantiating a class using a raw type (ie without a type parameter, as in List list = new ArrayList(3)), is something you shouldn't do, as it is less type-safe, and is only allowed for backwards compatibility.使用原始类型(即没有类型参数,如 List list = new ArrayList(3))实例化 class 是您不应该做的事情,因为它的类型安全性较低,并且只允许向后兼容。

Link: Java Generics List and ArrayList with and without Parameters链接: Java Generics 列表和 ArrayList 有和没有参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM