Java映射与要列出的对象列表

时间:2020-04-06 17:04:11

标签: java

这是我的代码:

public class CarShop {
    private final HashMap<Brand, List<CarPrototype>> catalog;

    public List<CarPrototype> allPrototypes() {
        List<CarPrototype> prototypes = catalog.values().stream().collect(Collectors.toList())
        return prototypes ;
    } 

我想用这种方法来做的是,我要获得汽车商店目录中给出的所有不同汽车原型的列表。我不应该使用for循环,所以流开始起作用。我的解决方案是仅添加原型列表,而我很难找到如何更改它的方法,因此它自己添加了特定原型。

1 个答案:

答案 0 :(得分:3)

如果您有Time DateFormat Event Type ---- ---------- ---------- 3/31/2020 11:59:35 PM 31/03/2020 AGENT_RECONNECTED 3/31/2020 4:01:56 PM 31/03/2020 AGENT_DISCONNECTED 3/31/2020 4:14:19 PM 31/03/2020 AGENT_CONNECTED 3/31/2020 4:24:12 PM 31/03/2020 AGENT_CONNECTED 3/31/2020 4:30:31 PM 31/03/2020 AGENT_DISCONNECTED 3/31/2020 4:36:08 PM 31/03/2020 AGENT_RECONNECTED 3/31/2020 5:12:49 PM 31/03/2020 AGENT_DISCONNECTED 3/31/2020 6:25:39 PM 31/03/2020 AGENT_RECONNECTED 3/31/2020 7:09:08 PM 31/03/2020 AGENT_DISCONNECTED 3/31/2020 7:38:53 PM 31/03/2020 AGENT_RECONNECTED 3/31/2020 7:44:44 PM 31/03/2020 AGENT_CONNECTED 3/31/2020 8:23:48 PM 31/03/2020 AGENT_DISCONNECTED 3/31/2020 9:33:36 PM 31/03/2020 AGENT_RECONNECTED 3/31/2020 9:35:58 PM 31/03/2020 AGENT_DISCONNECTED 3/31/2020 10:46:54 PM 31/03/2020 AGENT_RECONNECTED 3/31/2020 10:50:54 PM 31/03/2020 AGENT_DISCONNECTED 3/31/2020 10:53:10 PM 31/03/2020 AGENT_RECONNECTED 3/31/2020 11:05:56 PM 31/03/2020 AGENT_DISCONNECTED 3/31/2020 11:13:47 PM 31/03/2020 AGENT_RECONNECTED 3/31/2020 11:32:41 PM 31/03/2020 AGENT_DISCONNECTED 3/31/2020 11:34:27 PM 31/03/2020 AGENT_RECONNECTED 3/31/2020 11:35:55 PM 31/03/2020 AGENT_DISCONNECTED 3/31/2020 11:38:21 PM 31/03/2020 AGENT_RECONNECTED 3/31/2020 11:50:52 PM 31/03/2020 AGENT_DISCONNECTED 3/31/2020 1:15:02 PM 31/03/2020 AGENT_CONNECTED 3/31/2020 1:04:17 PM 31/03/2020 AGENT_DISCONNECTED 并将其转换为Stream<List<Foo>>,则可以使用List<Foo>

flatMap

对于您的具体示例:

Stream<List<Foo>> stream = ...;
Stream<Foo> flatStream = stream.flatMap(List::stream);
List<Foo> list = flatStream.collect(Collectors.toList());
相关问题