Java 创建一个字符串解析方法,以通用方式构建映射?
前言:这并不是我遇到的实际问题,我只是想到了一个“如果……我该怎么做?”的时尚 当我有由多个键值对组成的字符串(如Java 创建一个字符串解析方法,以通用方式构建映射?,java,generics,Java,Generics,前言:这并不是我遇到的实际问题,我只是想到了一个“如果……我该怎么做?”的时尚 当我有由多个键值对组成的字符串(如123=456;321=654;89=90)时,我可以使用这样的方法很容易地从该字符串({123=456,321=654,89=90})生成一个映射: public static Map<Integer, Integer> makeMap(String theString) { String[] chunks = theString.split(";&q
123=456;321=654;89=90
)时,我可以使用这样的方法很容易地从该字符串({123=456,321=654,89=90}
)生成一个映射:
public static Map<Integer, Integer> makeMap(String theString) {
String[] chunks = theString.split(";");
Map<Integer, Integer> result = new HashMap<>(chunks.length);
for (String chunk : chunks) {
String[] chunksChunks = chunk.split("=");
int key = Integer.parseInt(chunksChunks[0]);
int value = Integer.parseInt(chunksChunks[1]);
result.put(key, value);
}
return result;
}
公共静态映射makeMap(字符串){
String[]chunks=theString.split(“;”);
映射结果=新的HashMap(chunks.length);
for(字符串块:块){
字符串[]chunksChunks=chunk.split(“”);
int key=Integer.parseInt(chunksChunks[0]);
int value=Integer.parseInt(chunksChunks[1]);
结果。输入(键、值);
}
返回结果;
}
有没有什么优雅的方法可以“扩展”这个方法,使之成为一个泛型方法,例如接受所有(包装)基元类型
有可能写
public static <K extends Object, V extends Object> Map<K, V> makeMapGeneric(String theString) {
// ???
}
公共静态映射makeMapGeneric(字符串){
// ???
}
…但我不知道如何对键和值进行“铸造”
makeXYfromString(String…
方法,只有显式的Integer.parseInt
,Double.parseDouble
等等,它们没有一个公共的超类/接口,我可以将K
和V
限制为它们makeMapGeneric(String theString,Class keyClass,Class valueClass)
)并编写类似于K key=keyClass.cast(keyString)的内容代码>,是不可能的,因为您无法将字符串转换为例如int,只需解析它即可
有什么优雅的解决方案吗?您可以为自己提供一个
函数方法:
Map makeMapGeneric(字符串、函数键fn、函数值fn){
String key=“123”;
字符串值=“456”;
K parsedKey=keyFn.apply(key);
V parsedValue=valueFn.apply(键);
}
现在,您可以使用将字符串
转换为K
(和V
)的函数调用它:
映射结果=
makeMapGeneric(“123=456”,Integer::parseInt,Double::parseDouble);
您可以向您提供一个函数
方法:
Map makeMapGeneric(字符串、函数键fn、函数值fn){
String key=“123”;
字符串值=“456”;
K parsedKey=keyFn.apply(key);
V parsedValue=valueFn.apply(键);
}
现在,您可以使用将字符串
转换为K
(和V
)的函数调用它:
映射结果=
makeMapGeneric(“123=456”,Integer::parseInt,Double::parseDouble);
我仔细考虑了几分钟,然后想出了这个解决方案
公共静态映射makeMap(字符串输入、函数keyFunc、函数valFunc){
返回Arrays.stream(input.split(“;”))
.map(s->s.split(“=”)
.collect(Collectors.toMap(s->keyFunc.apply(s[0]),s->valFunc.apply(s[1]);
}
您需要传递两个函数,将字符串转换为正确的值。
像这样使用它:
public static Map<Integer, Integer> makeMap(String theString) {
String[] chunks = theString.split(";");
Map<Integer, Integer> result = new HashMap<>(chunks.length);
for (String chunk : chunks) {
String[] chunksChunks = chunk.split("=");
int key = Integer.parseInt(chunksChunks[0]);
int value = Integer.parseInt(chunksChunks[1]);
result.put(key, value);
}
return result;
}
Map x=makeMap(“123=456;321=654;89=90”,Integer::parseInt,Integer::parseInt);
我仔细考虑了几分钟,然后想出了这个解决方案
公共静态映射makeMap(字符串输入、函数keyFunc、函数valFunc){
返回Arrays.stream(input.split(“;”))
.map(s->s.split(“=”)
.collect(Collectors.toMap(s->keyFunc.apply(s[0]),s->valFunc.apply(s[1]);
}
您需要传递两个函数,将字符串转换为正确的值。
像这样使用它:
public static Map<Integer, Integer> makeMap(String theString) {
String[] chunks = theString.split(";");
Map<Integer, Integer> result = new HashMap<>(chunks.length);
for (String chunk : chunks) {
String[] chunksChunks = chunk.split("=");
int key = Integer.parseInt(chunksChunks[0]);
int value = Integer.parseInt(chunksChunks[1]);
result.put(key, value);
}
return result;
}
Map x=makeMap(“123=456;321=654;89=90”,Integer::parseInt,Integer::parseInt);
Sidenote:K extends Object
可以缩短为K
谢谢@Lino,我不知道。我想我不会在那里缩短它,我是“不要缩短已经很短的东西”的粉丝旁注:K扩展对象
可以缩短为K
谢谢@Lino,我不知道。我想我不会在那里缩短它,我是“不要缩短已经很短的东西”的粉丝哇!我将接受本杰明M的答案,因为我可以更容易地阅读,但我也喜欢你的方法。:-)与前面的答案相同,但使用Java streams API。哇!我将接受本杰明M的答案,因为我可以更容易地阅读,但我也喜欢你的方法。:-)与前面的答案相同,但使用Java streams API。啊,是的,似乎能够处理这样的函数还没有真正进入我的大脑…@Bowi你可能想看看函数编程。可能会打开一个全新的世界;)@哦,是的,是的。一次又一次,一次又一次…;-)啊,是的,似乎能够处理这样的函数还没有真正进入我的大脑…@Bowi你可能想看看函数编程。可能会打开一个全新的世界;)@哦,是的,是的。一次又一次,一次又一次…;-)