Reputation: 124
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
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
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
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