英文:
How will you convert method to single line in java8
问题
以下是您要求的翻译内容:
我有以下的方法,可以将单行转换为映射。
并且希望使用Java 8将其转换为单行语句。
输入以竖线分隔的字符串,
例如:
status:MYSTATUS|data1:value1
public Map<String, String> getMap(String attributeUpdate){
Map<String, String> attr = new HashMap<>();
if(StringUtils.isNotEmpty(attributeUpdate)){
List<String> a = Arrays.asList(attributeUpdate.split("\\|"));
a.forEach(s -> {
String[] key_val = s.split(":");
if(key_val.length == 2) {
attr.put(key_val[0], key_val[1]);
}
});
}
return attr;
}
英文:
I am having following method which converts single line to map
And want to convert it to single line statement using java8
input pipe separated string
e.g.:
status:MYSTATUS|data1:value1
public Map<String, String> getMap(String attributeUpdate){
Map<String, String> attr = new HashMap<>();
if(StringUtils.isNotEmpty(attributeUpdate)){
List<String> a = Arrays.asList(attributeUpdate.split("\\|"));
for (String s : a) {
String[] key_val = s.split(":");
if(key_val.length == 2) {
attr.put(key_val[0], key_val[1]);
}
}
}
return attr;
}
答案1
得分: 6
Please try this following code. It works fine.
String s = new String("status:MYSTATUS|data1:value1");
Arrays.stream(s.split("\\|")).map(stringArray -> stringArray.split(":"))
.filter(stringArray -> stringArray.length == 2)
.collect(Collectors.toMap(stringArray ->stringArray[0], stringArray-> stringArray[1],(oldvalue,newvalue)->newvalue));
Thanks
英文:
Please try this following code. It works fine.
String s = new String("status:MYSTATUS|data1:value1");
Arrays.stream(s.split("\\|")).map(stringArray -> stringArray.split(":"))
.filter(stringArray -> stringArray.length == 2)
.collect(Collectors.toMap(stringArray ->stringArray[0], stringArray-> stringArray[1],(oldvalue,newvalue)->newvalue));
Thanks
答案2
得分: 2
以下是翻译好的代码部分:
这是一个简单的解决方案:
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
public class Test {
@org.junit.jupiter.api.Test
public void shouldParseAttributes() throws Exception {
String attributeUpdate = "status:MYSTATUS|data1:value1";
Map<String, String> attributes = Stream.of(attributeUpdate.split("\\|"))
.map(attribute -> attribute.split(":"))
.collect(Collectors.toMap(
attribute -> attribute[0],
attribute -> attribute[1]));
assertThat(attributes).containsEntry("status", "MYSTATUS");
assertThat(attributes).containsEntry("data1", "value1");
}
}
英文:
Here is a simple solution :
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
public class Test {
@org.junit.jupiter.api.Test
public void shouldParseAttributes() throws Exception {
String attributeUpdate = "status:MYSTATUS|data1:value1";
Map<String, String> attributes = Stream.of(attributeUpdate.split("\\|"))
.map(attribute -> attribute.split(":"))
.collect(Collectors.toMap(
attribute -> attribute[0],
attribute -> attribute[1]));
assertThat(attributes).containsEntry("status", "MYSTATUS");
assertThat(attributes).containsEntry("data1", "value1");
}
}
答案3
得分: 2
可以尝试如下方式:
Arrays.stream(attributeUpdate.split("\\|")) //直接转换为流
.map(e -> e.split(":"))
.filter(e -> e.length == 2)
.collect(toMap(s -> s[0], s -> s[1], (o, n) -> n)); // 收集成映射,处理重复项
英文:
You can try as below,
Arrays.stream(attributeUpdate.split("\\|")) //convert directly to stream
.map(e->e.split(":"))
.filter(e->e.length==2)
.collect(toMap(s->s[0], s->s[1], (o, n)->n)); // collect map, handle duplicates
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论