gpt4 book ai didi

java - 将泛型类型类列表转换为对象列表

转载 作者:行者123 更新时间:2023-12-01 11:09:40 24 4
gpt4 key购买 nike

嗨,我的泛型类型方法返回列表,如下

 List<Class<Department>> departmentsByAppId = commonDao.getDaoData(new Object[]{appStateObject.getAppId()}, new String[]{"appId"}, new String[]{"eq"}, Department.class);

那么我们如何将这些数据转换为普通列表,如 List<Department>无需迭代。

最佳答案

假设您的泛型类型方法是在泛型接口(interface)中声明的,并且该部门有一个扩展了最后一个接口(interface)的 DAO 接口(interface),您可以执行类似的操作:

YourGenericDaoInterface.java:

public interface YourGenericDaoInterface<E> {

List<E> getDaoData(Object[] objArray, String[] appId, String[] eq, Class clazz);

}

YourGenericDaoImplementation.java:

@Override
List<E> getDaoData(Object[] objArray, String[] appId, String[] eq, Class clazz) {
// do your stuff here
}

YourDepartmentDaoInterface.java:

public interface YourDepartmentDaoInterface extends YourGenericDaoInterface<Department> {

}

所以基本上它会执行以下操作:

  • 通过制作 YourDepartmentDaoInterface继承YourGenericDaoInterface<Department>您获取方法 getDaoData 并告知返回类型是 Department 类型,然后当您使用您的方法时,您将直接调用:

List<Department> dep = commonDao.getDaoData(new Object[] appStateObject.getAppId()}, new String[]{"appId"}, new String[]{"eq"}, Department.class);

关于java - 将泛型类型类列表转换为对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32520363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com