将字符串拆分为键值对 [英] Split string into key-value pairs
问题描述
我有这样一个字符串:
pet:cat :: car:honda :: location:location :: Japan :: food :sushi
现在:
表示键值而 ::
则将这两对分开。
我想将键值对添加到地图中。
我可以使用以下方法来实现这一点:
地图< String,String> map = new HashMap< String,String>();
String test =pet:cat :: car:honda :: location:Japan :: food:sushi;
String [] test1 = test.split(::);
for(String s:test1){
String [] t = s.split(:);
map.put(t [0],t [1]);
(String s:map.keySet()){
System.out.println(s +is+ map.get(s));
}
但有没有这样做的有效方法?
我觉得代码效率低下,因为我已经使用了2 String []
对象并且调用分割
函数两次。
此外,我正在使用 t [0]
和 t [1]
,这可能会抛出<$如果没有值,c $ c> ArrayIndexOutOfBoundsException 你可以做一次调用使用下面的代码对字符串进行split()和单个传递。但它当然假定String首先是有效的:
Map< String,String> map = new HashMap< String,String>();
String test =pet:cat :: car:honda :: location:Japan :: food:sushi;
//分割':'和'::'
String [] parts = test.split(::?);
for(int i = 0; i< parts.length; i + = 2){
map.put(parts [i],parts [i + 1]);
(String s:map.keySet()){
System.out.println(s +is+ map.get(s));
以上是可能效率稍高一点而不是你的解决方案,但是如果你发现你的代码更加清晰,那么就保留它,因为这样的优化几乎没有机会对性能产生重大影响,除非你做了那么多次。无论如何,如果它是如此重要,那么你应该衡量和比较。
编辑:对于那些想知道的人来说,
在上面的代码中 ::?
意味着什么:String.split()需要一个正则表达式作为参数。分隔符是与正则表达式匹配的子字符串。 ::?
是一个正则表达式,意思是:1个冒号,后跟0或1个冒号。因此它允许考虑 ::
和:
作为分隔符。
I have a string like this:
pet:cat::car:honda::location:Japan::food:sushi
Now :
indicates key-value pairs while ::
separates the pairs.
I want to add the key-value pairs to a map.
I can achieve this using:
Map<String, String> map = new HashMap<String, String>();
String test = "pet:cat::car:honda::location:Japan::food:sushi";
String[] test1 = test.split("::");
for (String s : test1) {
String[] t = s.split(":");
map.put(t[0], t[1]);
}
for (String s : map.keySet()) {
System.out.println(s + " is " + map.get(s));
}
But is there an efficient way of doing this?
I feel the code is inefficient because I have used 2 String[]
objects and called the split
function twice.
Also, I am using t[0]
and t[1]
which might throw an ArrayIndexOutOfBoundsException
if there are no values.
You could do a single call to split() and a single pass on the String using the following code. But it of course assumes the String is valid in the first place:
Map<String, String> map = new HashMap<String, String>();
String test = "pet:cat::car:honda::location:Japan::food:sushi";
// split on ':' and on '::'
String[] parts = test.split("::?");
for (int i = 0; i < parts.length; i += 2) {
map.put(parts[i], parts[i + 1]);
}
for (String s : map.keySet()) {
System.out.println(s + " is " + map.get(s));
}
The above is probably a little bit more efficient than your solution, but if you find your code clearer, then keep it, because there is almost zero chance such an optimization has a significant impact on performance, unless you do that millions of times. Anyway, if it's so important, then you should measure and compare.
EDIT:
for those who wonder what ::?
means in the above code: String.split() takes a regular expression as argument. A separator is a substring that matches the regular expression. ::?
is a regular expression which means: 1 colon, followed by 0 or 1 colon. It thus allows considering ::
and :
as separators.
这篇关于将字符串拆分为键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!