【问题标题】:Convert List<MyObject> to List<List<String>> using java8 lambdas only仅使用 java 8 lambda 将 List<Object> 转换为 List<List<String>>
【发布时间】:2015-10-22 08:16:21
【问题描述】:

我有List&lt;User&gt;,其中User 是一个具有变量id,name,date 的类。我只想用它创建一个List&lt;List&lt;String&gt;&gt;,这样它只包含第一个列表中的名称和日期。我的代码

import java.util.*;
import java.util.stream.*;
public class User
{
  int id;
  String name;
  Date date;

  public User(int id,String name,Date date){
    this.id=id;
    this.name=name;
    this.date=date;

  }

  public static void main(String[] args)
  {
    User one=new User(1,"a",new Date());
    User two=new User(2,"b",new Date());
    User three=new User(3,"c",new Date());

    List<User> userList=Arrays.asList(one,two,three);

    System.out.println(userList);

    List<List<String>> stringList = IntStream.range(0,userList.size())
                                             .maptoObj(i -> Array.asList(userList.get(i).name,userList.get(i).date))
                                             .collect(toList);
    System.out.print(stringList);

  }
}

当我使用collect()时,我似乎无法弄清楚如何实现这一点,它说在收集时找不到符号。有什么办法可以让List&lt;List&lt;String&gt;&gt; 包含来自List&lt;User&gt; 的姓名和日期列表

我也试过

List<List<String>> stringList = IntStream.range(0,userList.size())
                                         .map(i -> Arrays.asList(userList.get(i).name,userList.get(i).date.toString()))
                                         .collect(Collectors.toList());

但它给了我

 error: 
    no instance(s) of type variable(s) T exist so that List<T> conforms to int
  where T is a type-variable:
    T extends Object declared in method <T>asList(T...)incompatible types: bad return type in lambda expression
                                             .map(i -> Arrays.asList(userList.get(i).name,userList.get(i).date.toString()))
                                                                    ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

谢谢

【问题讨论】:

  • 是否要将Date 对象放入字符串列表中?
  • 你的错误在collect(toList)toList不存在,是toList()
  • @marstran 不,我想将其转换为字符串

标签: java list collections lambda java-8


【解决方案1】:

您不需要使用IntStream

List<List<String>> output = 
    userList.stream()
            .map (u -> Arrays.asList (u.name,u.date.toString()))
            .collect (Collectors.toList());

编辑:

如果您希望继续使用IntStream 解决方案:

List<List<String>> stringList = 
    IntStream.range(0,userList.size())
             .mapToObj(i -> Arrays.asList(userList.get(i).name,userList.get(i).date.toString()))
             .collect(Collectors.toList());

【讨论】:

  • 可以用Intsream 完成吗?我尝试删除collect 错误,但它不起作用(编辑了我的问题)实际上我正在尝试类似stackoverflow.com/a/22933355/3841803
  • 是的,我已经更新了我的问题。很难理解 java 8 中的这些新功能
  • @singhakash 如果你坚持使用 IntStream 解决方案,你应该使用mapToObj 而不是map
  • 很抱歉,如果这很奇怪,但你能提供一些关于学习 lambdas 和 java 8 中的新特性的提示吗?我很难理解它,就像上面你将 map 更改为 mapToObject 你怎么知道的?
  • @codegasmer 任何新的 API 都需要时间来学习。只需 Google Java 8 Lambda 表达式和 Java 8 Streams。我就是这么学的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 2021-08-08
  • 2020-03-08
相关资源
最近更新 更多