Java 定义通用列表

Java 定义通用列表,java,generics,java-8,Java,Generics,Java 8,我对泛型相当陌生,非常感谢您的帮助。我有一个方法 1 public static List<classA> convertA(List<List<Object>> dataframe) throws Exception { 2 List<classA> objA = new ArrayList<classA>(); 3 for (List<Object> objs : dataframe) { 4

我对泛型相当陌生,非常感谢您的帮助。我有一个方法

1 public static List<classA> convertA(List<List<Object>> dataframe) throws Exception {
2       List<classA> objA = new ArrayList<classA>();
3       for (List<Object> objs : dataframe) {
4           if (objs != null) {
5               classA a = new classA();

6               //generic logic to set value from objs to object `a` irrespective of type of `a`. 

7               objA.add(a);
8           }
9       }

10      return objA;
11  }
因此,我尝试将这两个方法组合成一个方法,编写一个通用的泛型方法,返回类型为
List
,参数包括类型为T(可以是classA、classB,…),类似这样

public static List<T> convertGeneric(List<List<Object>> dataframe, T t) 
公共静态列表转换器通用(列表数据帧,T)
但我不知道如何用一般的方式写第2、5、7行

有人能给我指出正确的方向吗

请注意:

两种方法
convertA
convertB
彼此独立,用于处理两种不同的场景。这两者之间没有关系,只是两者都有相同的逻辑将
列表对象
转换为其各自的对象,即
a(classA)
b(classB)

我并没有维护两个独立的方法,比如
convertA()
convertB()
,我只是想把它变成一个单独的方法,我可以通过传递如下参数来调用它

List ls=convertGeneric(数据帧,ClassA.class)而不是
convertA()


List ls1=convertGeneric(数据帧,ClassB.class)
而不是
convertB()

如果我理解正确,您需要创建接口并在
classA
classB
中实现它。然后将此接口用作通用占位符。例如:

public class ClassA implements  Interface {
}

public class ClassB implements  Interface {
}



 ArrayList<Interface> objA = new ArrayList<>();
        objA.add(new ClassA());
        objA.add(new ClassB());
        for (Interface obj : objA) {
            //logic
            }
        }
公共类ClassA实现接口{
}
公共类ClassB实现接口{
}
ArrayList objA=新的ArrayList();
add(新的ClassA());
add(新类b());
用于(接口obj:objA){
//逻辑
}
}
使用通用方法的解决方案 将泛型方法定义为

public static <T> List<T> convertGeneric(List<List<Object>> dataframe, Function<Object, T> converter)
或者使用流的另一个:

public static <T> List<T> convertGeneric(List<List<Object>> dataframe, Function<Object, T> converter) {

  return dataframe.stream()
          .flatMap(Collection::stream)    // get the objects from the inner list
          .map(converter)                 // convert object to T
          .collect(Collectors.toList());  // put everything in a new list
}
为每个目标类添加一个实现,例如,将
对象
转换为
Foo

public class FooConverter extends GenericConverter<Foo> {

    @Override
    protected Foo convert(Object obj) {
        Foo foo = new Foo();

        // logic for mapping obj to foo

        return foo;
    }
}

@拉文德拉兰瓦拉:没有关系。两个独立的实体两个构造函数是否具有相同的参数集?我指的是相同数量和类型的参数?不。但我不明白这在这里有什么关系,因为我已经编写了将
List objs
转换为
a
b
@ravindranwala;请看一下我的编辑。希望我已经说清楚了。如果你有任何疑问,一定要告诉我。我想我的问题不清楚。类别A和类别B之间没有关系。它们是两个独立的实体。此外,我不需要将ClassA和ClassB的元素存储到同一个列表中。我将编辑我的问题。这正是我想要的。非常感谢,伙计。很高兴我能帮忙。这是一个有趣的问题。
public static <T> List<T> convertGeneric(List<List<Object>> dataframe, Function<Object, T> converter) {

  return dataframe.stream()
          .flatMap(Collection::stream)    // get the objects from the inner list
          .map(converter)                 // convert object to T
          .collect(Collectors.toList());  // put everything in a new list
}
Function<Object, Foo> fooLambda = object -> {
  Foo foo = new Foo();
  // logic for mapping obj to foo
  return foo;
};
List<Foo> fooList = GenericMethodConverter.convertGeneric(dataFrame, fooLambda);
public abstract class GenericConverter<T> {

  public List<T> convertGenerically(List<List<Object>> dataFrame) {

    List<T> tList = new ArrayList<>();

    for (List<Object> objectList : dataFrame) {
      for (Object obj : objectList) {
         T t = convert(obj);
         tList.add(t);
       }
    }
    return tList;
  }

  protected abstract T convert(Object obj);
}
public class FooConverter extends GenericConverter<Foo> {

    @Override
    protected Foo convert(Object obj) {
        Foo foo = new Foo();

        // logic for mapping obj to foo

        return foo;
    }
}
List<Foo> foos = fooConverter.convertGenerically(dataFrame);