Java,Lambda:如何从不同类型的列表集合中查找列表?
Java, Lambda: How to find a List from a Collection of Lists with different Types?
在 Java 8 中,从列表列表 ->(记录){List<Type1>, List<Type2>, List<Type3>, ...}
中获取包含 Type1
元素的列表的最佳方法是什么?
记录有多个不同类型的列表 -> {List<Type1>, List<Type2>, List<Type3>, ...}
List<T> getList(T t) {
// t is instance of Type1
return Records -> List<t>;
}
非常感谢您的帮助。
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());
}
}
在 Java 8 中,从列表列表 ->(记录){List<Type1>, List<Type2>, List<Type3>, ...}
中获取包含 Type1
元素的列表的最佳方法是什么?
记录有多个不同类型的列表 -> {List<Type1>, List<Type2>, List<Type3>, ...}
List<T> getList(T t) {
// t is instance of Type1
return Records -> List<t>;
}
非常感谢您的帮助。
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());
}
}