【问题标题】:Java, Lambda: How to find a List from a Collection of Lists with different Types?Java,Lambda:如何从具有不同类型的列表集合中查找列表?
【发布时间】:2017-10-23 16:17:47
【问题描述】:

在 Java 8 中,从列表列表中获取包含 Type1 元素的列表的最佳方法是 ->(记录){List<Type1>, List<Type2>, List<Type3>, ...} ?

Records 有多个不同类型的列表 -> {List<Type1>, List<Type2>, List<Type3>, ...}

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

非常感谢您的帮助。

【问题讨论】:

  • Type1、Type2等是否相关?
  • 不,这些类型完全相互独立。感谢您的评论。

标签: java lambda filter prediction


【解决方案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());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多