简体   繁体   English

如何使用传入的通用对象列表并使用我的 Java 方法返回通用 object?

[英]How do I use passed in generic Lists of objects and return a generic object with my Java method?

I am trying to make my application more efficient by using Java generics, but I don't have a lot of generics experience.我试图通过使用 Java generics 使我的应用程序更高效,但我没有很多 generics 经验。 I am trying to do this:我正在尝试这样做:

My Two Model Objects:我的两个 Model 对象:

public class Person {
    private String firstName;
    private String lastName;
    private int age;
    // ...

    // Getters and Setters Here
}

public class Car {
    private String make;
    private String model;
    private int year;
    // ...

    // Getters and Setters Here
}

My Result Objects:我的结果对象:

private class PersonResult {
    private boolean success;
    private String notes;
    private int score;

    // Getters and Setters Here
}

private class CarResult {
    private boolean success;
    private String notes;
    private int score;
    private double price;

    // Getters and Setters Here
}

My Main Class:我的主要 Class:

public class MyClass{

    public mainMethod(){

        List<Person> listOfPersons = new ArrayList<>();
        List<Car> listOfCars = new ArrayList<>();

        listOfPersons.add(// a Person object);
        // ...

        listOfCars.add(// a Car object);
        // ...

        PersonResult = resultMethod(listOfPersons);
        CarResult = resultMethod(listOfCars);
    }

    private <E,T> E resultMethod(List<T> listOfData)
    {
        Engine engine = EngineFactory.getEngine();

        // if(listOfData.contains(Person.class))
        if (listOfData instanceof List<Person>)
        {
            PersonResult personResult = new PersonResult();
            perosnResult = engine.getPersonResult(listOfData);
            return personResult;
        }
        // else if(listOfData.contains(Car.class))
        else if (listOfData instanceof List<Car>)
        {
            CarResult carResult = new CarResult();
            carResult engine.getCarResult(listOfData);
            return carResult;
        }

        return null;
    }
}

However my IDE is complaining about:但是我的 IDE 抱怨:

1 Illegal generic type for instanceof 1 Illegal generic type for instanceof

2 (for the return value) Incompatible types: Required: E - Found PersonResult 2(对于返回值) Incompatible types: Required: E - Found PersonResult

As you can see, I also tried listOfData.contains(Person.class) , that took care of error #1, but not the return error.如您所见,我还尝试了listOfData.contains(Person.class) ,它处理了错误 #1,但没有处理返回错误。

You cannot test for generic parameters by instanceof .您不能通过instanceof测试泛型参数。 instanceof will be evaluated at runtime. instanceof将在运行时评估。 But the compiler removes the generic types.但是编译器删除了泛型类型。 They are not present at runtime.它们在运行时不存在。 For example List<Integer> will be the same as List at runtime.例如List<Integer>在运行时将与List相同。

You can use a simple implementation without generics here:您可以在此处使用没有 generics 的简单实现:

private PersonResult resultMethodPerson(List<Person> listOfData) {
  return EngineFactory.getEngine().getPersonResult(listOfData);
}

private CarResult resultMethodCar(List<Car> listOfData) {
  return EngineFactory.getEngine().getCarResult(listOfData);
}

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

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