【问题标题】:Combining items from custom class组合自定义类中的项目
【发布时间】:2020-04-08 15:19:20
【问题描述】:

我有以下

List<String> namesList = aList.stream()
                                   .map(rollingD::getSettDate)
                                   .collect(Collectors.toList());

List<String> namesList1 = aList.stream()
                                   .map(rollingD::getPublishingPeriodCommencingTime)
                                   .collect(Collectors.toList());

PublishingPeriodCommencingTime 和 SettDate 都存在于同一个列表中。

如何将我的两个项目组合起来,以便获得 getSettDate +" "+ getPublishingPeriodCommencingTime

getSettDate 看起来像例如 01/01/2020,getPublishingPeriodCommencingTime 看起来像例如 01:10:33

我想获取日期时间戳,例如 dd/mm/yyyy hh:MM:ss

【问题讨论】:

  • 对于日期和时间格式,您可以使用 simpledatetimefomatter

标签: java list java-stream


【解决方案1】:
List<Strings> stamps = IntStream.range(0, nameList.size())
    .map(i->nameList.get(i) + " " + nameList1.get(i))
    .collect(Collectors.toList())

但是为什么不使用原始列表呢?

List<String> stamps = aList.stream()
     .map(x->x.getSettDate() + " " + x.getPublishingPeriodCommencingTime())
     .collect(Collectors.toList());

【讨论】:

  • 这正是我想要的。谢谢!:)
【解决方案2】:
     List<String> namesList = aList.stream()
                               .map(rollingD -> rollingD.getSettDate() + " " + 
                                    rollingD.getPublishingPeriodCommencingTime())
                               .collect(Collectors.toList());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    相关资源
    最近更新 更多