You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从LinkedHashMap嵌套的ArrayList中获取print字段值?

如何从SnakeYAML读取的YAML数据中提取options里的print值?

现有YAML文件内容

description: this-apps-config
options:
  - customer: joe
    id: 1
    date: 2022-01-01
    print: False
  - customer: jane
    id: 2
    date: 2022-01-02
    print: True

已实现的读取代码

Yaml yaml = new Yaml();
InputStream inputStream = new FileInputStream(new File("file.yml"));
Map<String, Object> data = yaml.load(inputStream);
System.out.println(data);

读取后的数据结构为:

{description=this-apps-config, options=[{customer=joe, id=1, date=2022-01-01, print=False}, {customer=jane, id=2, date=2022-01-02, print=True}]}

当前问题

你尝试用以下代码获取options:

ArrayList<Object> al = new ArrayList<>();
al.add(data.get("options"));

但这只是把整个options列表作为单个元素放进了新的ArrayList,无法直接遍历每个option项获取print值。


解决方案

方式一:直接遍历集合与Map(快速实现)

不需要额外创建新的ArrayList,直接把data.get("options")强转为ArrayList<Map<String, Object>>,然后遍历每个Map获取print值:

// 直接获取options对应的列表
ArrayList<Map<String, Object>> optionsList = (ArrayList<Map<String, Object>>) data.get("options");

// 遍历每个option项
for (Map<String, Object> option : optionsList) {
    // YAML中的False/True会被解析为Boolean类型,直接强转即可
    Boolean printValue = (Boolean) option.get("print");
    System.out.println("print值:" + printValue);
}

方式二:用实体类映射(类型安全,推荐)

对于复杂配置,定义对应实体类可以避免频繁强转,提升代码可读性:

  1. 定义实体类:
import java.time.LocalDate;
import java.util.List;

// 单个option的实体类
class Option {
    private String customer;
    private Integer id;
    private LocalDate date;
    private Boolean print;

    // SnakeYAML要求必须有无参构造器
    public Option() {}

    // Getter和Setter方法
    public String getCustomer() { return customer; }
    public void setCustomer(String customer) { this.customer = customer; }
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id; }
    public LocalDate getDate() { return date; }
    public void setDate(LocalDate date) { this.date = date; }
    public Boolean getPrint() { return print; }
    public void setPrint(Boolean print) { this.print = print; }
}

// 根配置实体类
class AppConfig {
    private String description;
    private List<Option> options;

    public AppConfig() {}

    // Getter和Setter方法
    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }
    public List<Option> getOptions() { return options; }
    public void setOptions(List<Option> options) { this.options = options; }
}
  1. 读取并映射到实体类:
Yaml yaml = new Yaml(new Constructor(AppConfig.class));
InputStream inputStream = new FileInputStream(new File("file.yml"));
AppConfig config = yaml.load(inputStream);

// 遍历获取print值
for (Option option : config.getOptions()) {
    System.out.println("print值:" + option.getPrint());
}

内容的提问来源于stack exchange,提问作者askaquestion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 21:36:02