将 (YAML) 文件转换为任何 MAP 实现
Converting a (YAML) file to any MAP implementation
我正在做一个业余项目,我需要从 YAML 文件中读取值并将它们存储在 HashMap
中,另一个 YAML 文件必须存储在 LinkedHashMap
中。我使用 API 进行读取,在下面的代码中添加了一些解释(尽管我认为这是多余的)。只包含了 returns 一个 LinkedHashMap
的方法,因为另一个几乎相同。
目前我正在使用不同的方法来获取 HashMap
和 LinkedHashMap
,但我注意到代码非常相似。所以我想知道,是否可以编写一个通用方法,将 YAML 文件中的路径和值放入任何 Collections
实现(正在实现 Hash Table
)?如果是这样,如何才能做到这一点?
public LinkedHashMap<String, Object> fileToLinkedHashMap(File yamlFile)
{
LinkedHashMap<String, Object> fileContents = new LinkedHashMap<String, Object>();
//Part of the API I'm using, reads from YAML File and stores the contents
YamlConfiguration config = YamlConfiguration.loadConfiguration(yamlFile);
//Configuration#getKeys(true) Gets all paths within the read File
for (String path : config.getKeys(true))
{
//Gets the value of a path
if (config.get(path) != null)
fileContents.put(path, config.get(path));
}
return fileContents;
}
注意:我知道我目前没有检查给定文件是否是 YAML 文件,这在这个问题中是多余的。
您可以为此使用功能接口(在 java 8 中介绍):
public void consumeFile(File yamlFile, BiConsumer<? super String, ? super Object> consumer){
YamlConfiguration config = YamlConfiguration.loadConfiguration(yamlFile);
for (String path : config.getKeys(true)){
if (config.get(path) != null){
consumer.accept(path, config.get(path));
}
}
}
然后可以用任何字面意思调用它,您只需提供一个接受 2 个参数的 lambda:
// collect into a map
Map<String, Object> map = /* hash map, linked hash map, tree map, you decide */;
consumeFile(yamlFile, map::put);
// just print them, why not?
consumeFile(yamlFile, (key, value) -> System.out.println(key + " = " + value));
你看,用途可能是无穷无尽的。仅受您的用例和想象力的限制。
如果您不能使用 java 8(虽然您可能应该使用),但仍有希望。正如你两次 return a Map
你可以决定在调用方法时你想收集什么地图实现:
public Map<String, Object> consumeFile(File yamlFile, Map<String, Object> map){
YamlConfiguration config = YamlConfiguration.loadConfiguration(yamlFile);
for (String path : config.getKeys(true)){
if (config.get(path) != null){
map.put(path, config.get(path));
}
}
return map;
}
可以这样称呼:
Map<String, Object> map = consumeFile(yamlFile, new /*Linked*/HashMap<>());
再说你想用什么地图实现,你可以根据自己的需要决定。
我正在做一个业余项目,我需要从 YAML 文件中读取值并将它们存储在 HashMap
中,另一个 YAML 文件必须存储在 LinkedHashMap
中。我使用 API 进行读取,在下面的代码中添加了一些解释(尽管我认为这是多余的)。只包含了 returns 一个 LinkedHashMap
的方法,因为另一个几乎相同。
目前我正在使用不同的方法来获取 HashMap
和 LinkedHashMap
,但我注意到代码非常相似。所以我想知道,是否可以编写一个通用方法,将 YAML 文件中的路径和值放入任何 Collections
实现(正在实现 Hash Table
)?如果是这样,如何才能做到这一点?
public LinkedHashMap<String, Object> fileToLinkedHashMap(File yamlFile)
{
LinkedHashMap<String, Object> fileContents = new LinkedHashMap<String, Object>();
//Part of the API I'm using, reads from YAML File and stores the contents
YamlConfiguration config = YamlConfiguration.loadConfiguration(yamlFile);
//Configuration#getKeys(true) Gets all paths within the read File
for (String path : config.getKeys(true))
{
//Gets the value of a path
if (config.get(path) != null)
fileContents.put(path, config.get(path));
}
return fileContents;
}
注意:我知道我目前没有检查给定文件是否是 YAML 文件,这在这个问题中是多余的。
您可以为此使用功能接口(在 java 8 中介绍):
public void consumeFile(File yamlFile, BiConsumer<? super String, ? super Object> consumer){
YamlConfiguration config = YamlConfiguration.loadConfiguration(yamlFile);
for (String path : config.getKeys(true)){
if (config.get(path) != null){
consumer.accept(path, config.get(path));
}
}
}
然后可以用任何字面意思调用它,您只需提供一个接受 2 个参数的 lambda:
// collect into a map
Map<String, Object> map = /* hash map, linked hash map, tree map, you decide */;
consumeFile(yamlFile, map::put);
// just print them, why not?
consumeFile(yamlFile, (key, value) -> System.out.println(key + " = " + value));
你看,用途可能是无穷无尽的。仅受您的用例和想象力的限制。
如果您不能使用 java 8(虽然您可能应该使用),但仍有希望。正如你两次 return a Map
你可以决定在调用方法时你想收集什么地图实现:
public Map<String, Object> consumeFile(File yamlFile, Map<String, Object> map){
YamlConfiguration config = YamlConfiguration.loadConfiguration(yamlFile);
for (String path : config.getKeys(true)){
if (config.get(path) != null){
map.put(path, config.get(path));
}
}
return map;
}
可以这样称呼:
Map<String, Object> map = consumeFile(yamlFile, new /*Linked*/HashMap<>());
再说你想用什么地图实现,你可以根据自己的需要决定。