Java, Lambda: How to find a List from a Collection

2019-03-05 22:41发布

问题:

Which is the best way in Java 8 to get a List with elements of Type1 from a List of Lists -> (Records) {List<Type1>, List<Type2>, List<Type3>, ...} ?

Records has several Lists with different Types -> {List<Type1>, List<Type2>, List<Type3>, ...}

List<T> getList(T t) {
  // t is instance of Type1
  return Records -> List<t>;
}

Thanks so much for your help.

回答1:

class Utils<T> {

    List<T> getList(T t, List<List> list) {
        return list.stream().filter(i -> t.getClass().isInstance(i.get(0))).flatMap(List<T>::stream).collect(Collectors.toList());
    }
}