gpt4 book ai didi

java - 在 Java 中将 List<> 内容过滤到字典中

转载 作者:行者123 更新时间:2023-12-01 12:46:53 24 4
gpt4 key购买 nike

我有一个特定类型对象的列表,如下所示:

List<Code>

Code类的属性如下;

private Integer id;
private Amp amp;
private FF ff;
private Imp imp;
private Board b;
private Line line;
private int interface;
private String interfaceType;
private boolean chained;

我想根据界面过滤此列表。接口(interface)可以从 0-7 变化,因此我想创建一个字典作为

Dictionary<int, List<Code>>

如何根据创建词典的接口(interface)过滤列表?

提前感谢您的回答。

最佳答案

我认为您正在寻找这样的东西:

List<Code> codes = ...;
Map<Integer, List<Code>> grouped = new HashMap<>();
for (Code code : codes) {
if (! grouped.containsKey(code.getInterface()) {
grouped.put(code.getInterface(), new ArrayList<Code>());
}
grouped.get(code.getInterface()).add(code);
}

关于java - 在 Java 中将 List<> 内容过滤到字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24611054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com