Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使这个带有泛型的循环更通用?_Java_Loops_Generics - Fatal编程技术网

Java 如何使这个带有泛型的循环更通用?

Java 如何使这个带有泛型的循环更通用?,java,loops,generics,Java,Loops,Generics,这些是我项目的课程。 类Bean是雇员和计算机的超级类型,不可移植 abstract Class Bean{ private HashMap<String, String> fields = null; protected Bean(Map<String, Object> row) { //fill HashMap fields } } public class Employe extends Bean { public Empl

这些是我项目的课程。 类Bean是雇员和计算机的超级类型,不可移植

abstract Class Bean{
   private HashMap<String, String> fields = null;
   protected Bean(Map<String, Object> row) {
        //fill HashMap fields
   }
}
public class Employe extends Bean {
    public Employe (Map<String, Object> row) {
        super(row);
   }
}

public class Computer extends Bean {
    public Computer (Map<String, Object> row) {
        super(row);
    }
}
抽象类Bean{
私有HashMap字段=null;
受保护Bean(映射行){
//填充HashMap字段
}
}
公共类Employe扩展Bean{
公共雇员(地图行){
超级(世界其他地区);
}
}
公共类计算机扩展Bean{
公共计算机(地图行){
超级(世界其他地区);
}
}
如何使这个带有泛型的循环更通用

我将在实用方法中使用泛型编写以下两个周期

//Loop 1

List<Map<String, Object>> employeRows = //from query on database

List<Employe> employList = new ArrayList<Employe>();

for (Map<String, Object> row : employeRows) {
    Employe e = new Employe(row);
    employList .add(e);
}

//Loop 2

List<Map<String, Object>> computerRows = //from query on database

List<Computer> computerList = new ArrayList<Computer>();

for (Map<String, Object> row : computerRows ) {
    Computer c = new Computer(row);
    computerList.add(c);
}
//循环1
List employeRows=//来自对数据库的查询
List employList=新建ArrayList();
用于(地图行:EmployeeRows){
雇员=新雇员(世界其他地区);
雇员名单。添加(e);
}
//环路2
List computerRows=//来自对数据库的查询
List computerList=new ArrayList();
用于(地图行:计算机行){
计算机c=新计算机(世界其他地区);
添加(c);
}
这个伪代码是我想要实现的一个示例:

List<Employe> = Utility.toList(employeRows, ...);
List<Computer> = Utility.toList(computerList, ...);
List=Utility.toList(EmployeeRows,…);
列表=实用程序列表(计算机列表,…);

使用工厂模式,一个抽象工厂

interface ObjectCreater<T> {
  T create(Map<String, Object> row);
}
接口ObjectCreater{
T创建(地图行);
}
和两个实现,例如针对员工

class EmployeeCreater implements ObjectCreator<T> {
  T create(Map<String, Object> row) {
     return new Employee(row); // or the constructor logic inlined
  }
}
类EmployeeCreater实现ObjectCreator{
T创建(地图行){
返回新员工(行);//或内联的构造函数逻辑
}
}
定义一个方法(=Utility.toList()):

列表列表列表(列表行,ObjectCreator){
//上层循环使用工厂而不是构造函数
}

如果您使用的是Java 7或更低版本,那么factory模式就是一种选择。但是,如果您已经使用Java8,那么可以通过streams和lambda/method引用来实现这一点

以下是使用Java 8的方法:

public static <T> List<T> toList(List<Map<String, Object>> rows, Function<Map<String, Object>, T> mapper) {
    return rows.stream().map(mapper).collect(Collectors.toList());
}
publicstaticlist-toList(列表行、函数映射器){
返回rows.stream().map(mapper.collect(Collectors.toList());
}
然后通过将构造函数作为方法引用传递来调用此方法:

List<Map<String, Object>> employeRows = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> computerRows = new ArrayList<Map<String, Object>>();

List<Employe> employList = toList(employeRows, Employe::new);
List<Computer> computerList = toList(computerRows, Computer::new);
List employeRows=new ArrayList();
List computerRows=new ArrayList();
List employList=toList(employeRows,Employe::new);
List computerList=toList(computerRows,Computer::new);

你能说清楚一点吗?让它更通用是什么意思?您使用的是哪个Java版本?
List<Map<String, Object>> employeRows = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> computerRows = new ArrayList<Map<String, Object>>();

List<Employe> employList = toList(employeRows, Employe::new);
List<Computer> computerList = toList(computerRows, Computer::new);