gpt4 book ai didi

java - 如何在包装器中最好地将 Map 转换为 Map

转载 作者:行者123 更新时间:2023-11-30 11:24:25 25 4
gpt4 key购买 nike

更新,添加评论中的问题:

what's the best way to get the Map into Map where Foo is an Enum. it just seems awfully complicated when, conceptually, not a complex idea.

此代码起作用,它是简单 MUD client 的一部分.基本思想是Monitor保存将在游戏过程中显示给用户的数据(生命值等),可能在辅助设备中 JPanel ;沿着这些路线的东西。此报告的可能值在 Attribs 中作为Enum .

在走得太远之前,当我查看 Monitor类以及创建单个实例所需的内容似乎有点冗长。

如果我移动convertToEnumEntry来自 Monitor那么这个类就不行了。所以,类(class)不能变小,我不认为。有没有我可以使用(或使用得更好)的模式?

此类中的所有内容都基于以下假设:如果 convertToEnumEntry返回对 Map.Entry 的空引用然后得到一个,具体String这不适合 Attribs :

enemy = stringEntry.getKey();

与其他键不同,敌人可以是任何 String .其他键仅限于 Enum Attribs 的值(属性)。

public class Monitor {

private static Logger log = Logger.getLogger(Monitor.class.getName());
private Map<Attribs, Ratio> mapOfAttributes = new HashMap<>();
private String enemy = null;

private Monitor() {
}

public Monitor(Map<String, Ratio> mapStringToRatio) {
init(mapStringToRatio);
}

private void init(Map<String, Ratio> mapStringToRatio) {
SimpleEntry<Attribs, Ratio> attribsEntry = null;
for (Entry<String, Ratio> stringEntry : mapStringToRatio.entrySet()) {
attribsEntry = null;
attribsEntry = convertToEnumEntry(stringEntry);
if (attribsEntry != null) {
mapOfAttributes.put(attribsEntry.getKey(), attribsEntry.getValue());
} else {
enemy = stringEntry.getKey(); //assumes key is enemy value
mapOfAttributes.put(Attribs.ENEMY, stringEntry.getValue());
}
}
}

private SimpleEntry<Attribs, Ratio> convertToEnumEntry(Entry<String, Ratio> stringEntry) {
Ratio ratio = stringEntry.getValue();
SimpleEntry<Attribs, Ratio> attribEntry = null;
for (Attribs attribute : Attribs.values()) {
if (stringEntry.getKey().equalsIgnoreCase(attribute.name())) {
attribEntry = new HashMap.SimpleEntry<>(attribute, ratio);
}
}
return attribEntry;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("\nfighting\t\t" + enemy + "\n");
for (Map.Entry<Attribs, Ratio> e : mapOfAttributes.entrySet()) {
sb.append("\n");
sb.append(e.getKey().name());
sb.append("\t");
sb.append(e.getValue().toString());
}
sb.append("\n");
return sb.toString();
}
}

将所有内容都塞进任一类来解析和构建 Map 是很有诱惑力的。 , 或将所有内容放入 Monitor .目前,RegexMapBuilder是一种中间立场,因为它完成了大部分艰苦的工作,但并没有完全构建完整的 Monitor就其本身而言:

public class RegexMapBuilder {

public static Map<String, Ratio> stringToRatiosMap(String input) {
Map<String, Ratio> mapOfStringsToRatios = new HashMap<>();
Map<String, String> strings = stringMap(input);
Pattern fraction = Pattern.compile("(\\d+)/(\\d+)");
Pattern wholeNumber = Pattern.compile("(\\d+)");
Pattern percent = Pattern.compile("(\\d+)%");
Matcher matcher;
int numerator, denominator;
Ratio ratio = null; //need numerator and denominator values
for (Entry<String, String> e : strings.entrySet()) {
matcher = wholeNumber.matcher(e.getValue());
while (matcher.find()) {
numerator = Integer.parseInt(matcher.group(1));
denominator = 1;
ratio = new Ratio(numerator, denominator);
}
matcher = fraction.matcher(e.getValue());
while (matcher.find()) {
numerator = Integer.parseInt(matcher.group(1));
denominator = Integer.parseInt(matcher.group(2));
ratio = new Ratio(numerator, denominator);
}
matcher = percent.matcher(e.getValue());
while (matcher.find()) {
numerator = Integer.parseInt(matcher.group(1));
denominator = 100;
ratio = new Ratio(numerator, denominator);
}
mapOfStringsToRatios.put(e.getKey(), ratio);
}
return mapOfStringsToRatios;
}

private static Map<String, String> stringMap(String input) {
Map<String, String> strings = new HashMap<>();
Pattern pattern = Pattern.compile("(\\w+): +(\\S+)");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
strings.put(matcher.group(1), matcher.group(2));
}
return strings;
}
}

为简洁起见,我省略了 Enum ,但是Attribs只是此报告打印的一些属性的列表。唯一棘手的部分是有一个值不是真正的枚举类型。

最佳答案

我认为java.util.EnumMap可能是你要找的。您可以使用 Enum#valueOf从给定的字符串中查找枚举值,就像 Sam Yonnou 提到的那样。

关于java - 如何在包装器中最好地将 Map<String,Foo> 转换为 Map<MyEnum,Foo>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20680377/

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