如何在Java 8中将方法转换为单行代码。

huangapple go评论99阅读模式
英文:

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&lt;String, String&gt; getMap(String attributeUpdate){
    Map&lt;String, String&gt; attr = new HashMap&lt;&gt;();
    if(StringUtils.isNotEmpty(attributeUpdate)){
        List&lt;String&gt; a = Arrays.asList(attributeUpdate.split(&quot;\\|&quot;));
        for (String s : a) {
            String[] key_val = s.split(&quot;:&quot;);
            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(&quot;status:MYSTATUS|data1:value1&quot;);
Arrays.stream(s.split(&quot;\\|&quot;)).map(stringArray -&gt; stringArray.split(&quot;:&quot;))
		.filter(stringArray -&gt; stringArray.length == 2)
		.collect(Collectors.toMap(stringArray -&gt;stringArray[0], stringArray-&gt; stringArray[1],(oldvalue,newvalue)-&gt;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 = &quot;status:MYSTATUS|data1:value1&quot;;

        Map&lt;String, String&gt; attributes = Stream.of(attributeUpdate.split(&quot;\\|&quot;))
            .map(attribute -&gt; attribute.split(&quot;:&quot;))
            .collect(Collectors.toMap(
                    attribute -&gt; attribute[0],
                    attribute -&gt; attribute[1]));

        assertThat(attributes).containsEntry(&quot;status&quot;, &quot;MYSTATUS&quot;);
        assertThat(attributes).containsEntry(&quot;data1&quot;, &quot;value1&quot;);
    }
}

答案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(&quot;\\|&quot;))   //convert directly to stream
    .map(e-&gt;e.split(&quot;:&quot;))
    .filter(e-&gt;e.length==2)
    .collect(toMap(s-&gt;s[0], s-&gt;s[1], (o, n)-&gt;n));  // collect map, handle duplicates

huangapple
  • 本文由 发表于 2020年5月29日 14:36:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/62080019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定