Gio D
Gio D

Reputation: 124

How can I read a list of lists from the config?

Say that I had, in my config.yml file, a list of lists of integers that looked like this:

numbers: [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

How would i go about reading this, or assigning it to a variable?

Upvotes: 0

Views: 856

Answers (3)

770grappenmaker
770grappenmaker

Reputation: 302

Since you are getting a List, you should use the ConfigurationSection#getList method. This takes in the path, in your case numbers. This returns a list with captures of ?, so you should cast elements to integer arrays.

getConfig.getList("numbers").stream().filter(o -> o instanceof List).map(o -> (List) o);

would result in a list of List<?>, so you should transform it to List. You can use the same way of filtering and casting I used above.

Upvotes: 1

Kyun
Kyun

Reputation: 61

you can use jyaml:

<dependency>
    <groupId>org.jyaml</groupId>
    <artifactId>jyaml</artifactId>
    <version>1.3</version>
</dependency>

java:

Map ymlFile = (Map) Yaml.load(new File("src/main/resources/config.yml"));
List<List> numberLists = (List) ymlFile.get("numbers");
for (List numberList : numberLists) {
    for (Object number : numberList) {
        System.out.println(number);
    }
}

Upvotes: 0

vivi17
vivi17

Reputation: 130

I suggest using .toArray() function in Java. As a result, you will get an array of all outer List elements, so in that case an Array of Lists. You can use that to further assign values to the variables.

Hope this helps.

Upvotes: 0

Related Questions