`Map.compute` 函数在 Java 中的使用:

huangapple 未分类评论45阅读模式
英文:

Map.compute function in java

问题

import java.util.*;

public class JavaApplication32 {
    public static void main(String args[]) {
        HashMap<String, String> map = new HashMap<>();
        map.put("Name", "Aman");
        map.put("Address", "Kolkata");
        map.compute("Name", (key, val) -> (val == null) ? "Gaurav" : val = "Great");
        System.out.println(map.get("Name"));
    }
}

上述代码出现错误。但是当值的数据类型选择为 int 时,代码可以正常工作。

英文:
import java.util.*;

public class JavaApplication32 {
	public static void main(String args[]) {
		HashMap&lt;String, String&gt; map = new HashMap&lt;&gt;();
		map.put(&quot;Name&quot;, &quot;Aman&quot;);
		map.put(&quot;Address&quot;, &quot;Kolkata&quot;);
		map.compute(&quot;Name&quot;, (key, val) -&gt; (val == null) ? &quot;Gaurav&quot; : val = &quot;Great&quot;);
		System.out.println(map.get(&quot;Name&quot;));
	}
}

The above code shows an error. But when the value data type is chosen as int, the code works.

答案1

得分: 0

不需要分配 `val`

import java.util.*;

public class JavaApplication32 {

public static void main(String args[])
{
 HashMap<String, String> map = new HashMap<>(); 
 map.put("Name", "Aman"); 
 map.put("Address", "Kolkata"); 
 map.compute("Name", (key, val) -> (val==null) ? "Gaurav" : "Great"); 
 System.out.println(map.get("Name"));
}
}
英文:

There is no need to assign val

import java.util.*;

public class JavaApplication32 {

public static void main(String args[])
{
 HashMap&lt;String, String&gt; map = new HashMap&lt;&gt;(); 
 map.put(&quot;Name&quot;, &quot;Aman&quot;); 
 map.put(&quot;Address&quot;, &quot;Kolkata&quot;); 
 map.compute(&quot;Name&quot;, (key, val)  -&gt; ( val==null) ? &quot;Gaurav&quot; : &quot;Great&quot;); 
 System.out.println(map.get(&quot;Name&quot;));
}
}

答案2

得分: 0

替换

map.compute("Name", (key, val) -> (val == null) ? "Gaurav" : val = "Great");

map.compute("Name", (key, val) -> (val == null) ? "Gaurav" : "Great");

输出:

Great

解释:
三元操作符的语法如下:

变量 = 条件 ? 如果条件为真的值 : 如果条件为假的值

例如,下面的代码将x的值赋给z:

int x = 10, y = 5, z;
z = x > y ? x : y;
System.out.println(z);

然而,如果您想在语句中保留左操作数,您可以使用(),如下所示:

map.compute("Name", (key, val) -> (val == null) ? "Gaurav" : (val = "Great"));

类似地,

z = x > y ? x : (z = y);

尽管上面给出的这两个语句可以成功编译,但我建议您不要这样做,因为这会让阅读您代码的人感到困惑。

英文:

Replace

map.compute(&quot;Name&quot;, (key, val)  -&gt; ( val==null) ? &quot;Gaurav&quot; : val=&quot;Great&quot;); 

with

map.compute(&quot;Name&quot;, (key, val) -&gt; (val == null) ? &quot;Gaurav&quot; : &quot;Great&quot;);

Output:

Great

Explanation:
The syntax of ternary operator is as follows:

variable = condition ? value-if-condition-is-true : value-if-condition-is-false

e.g. the following code assigns the value of x into z

int x = 10, y = 5, z;
z = x &gt; y ? x : y;
System.out.println(z);

However, if at all you want to keep the left operand inside the statement, you can do it using () as follows:

map.compute(&quot;Name&quot;, (key, val) -&gt; (val == null) ? &quot;Gaurav&quot; : (val = &quot;Great&quot;));

Similarly,

z = x &gt; y ? x : (z = y);

Even though these two statements given above will be compiled successfully, I recommend you never do it like this as it will simply confuse anyone going through your code.

huangapple
  • 本文由 发表于 2020年4月7日 01:59:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/61065994.html
匿名

发表评论

匿名网友

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

确定