将 Map 转换为自定义 Map。

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

Convert Map to Custom Map

问题

Sure, here's the translated code:

Map<String, String> characterMap = new HashMap<>();
characterMap.put("Manikanta", "Pretty");
characterMap.put("Amulya", "VeryGood");
characterMap.put("Saroja", "Good");
characterMap.put("Vinitha", "Cool");
characterMap.put("Sravani", "Good");
characterMap.put("Sameera", "Good");

/*String key = characterMap.entrySet().stream().filter(entry -> entry.getValue().equalsIgnoreCase("Good")).map(Map.Entry::getKey).findFirst().orElse(null);*/

Map<String, String> comboMap = new HashMap<>();
String newKey = null;
String val1 = null;
String val2 = null;
for (Map.Entry<String, String> entry : characterMap.entrySet()) {
    if (entry.getKey().equalsIgnoreCase("Manikanta"))
        newKey = entry.getKey();

    if (entry.getKey().equalsIgnoreCase("Amulya"))
        val1 = entry.getValue();

    if (entry.getKey().equalsIgnoreCase("Vinitha"))
        val2 = entry.getValue();
}

comboMap.put(newKey, val1 + "_" + val2);

Regarding your question about implementing this using lambda expressions: Yes, you can use lambda expressions to achieve similar functionality. Here's an example of how you might do it:

Map<String, String> characterMap = new HashMap<>();
characterMap.put("Manikanta", "Pretty");
characterMap.put("Amulya", "VeryGood");
characterMap.put("Saroja", "Good");
characterMap.put("Vinitha", "Cool");
characterMap.put("Sravani", "Good");
characterMap.put("Sameera", "Good");

Map<String, String> comboMap = new HashMap<>();

String newKey = characterMap.keySet().stream()
        .filter(key -> key.equalsIgnoreCase("Manikanta"))
        .findFirst()
        .orElse(null);

String val1 = characterMap.entrySet().stream()
        .filter(entry -> entry.getKey().equalsIgnoreCase("Amulya"))
        .map(Map.Entry::getValue)
        .findFirst()
        .orElse(null);

String val2 = characterMap.entrySet().stream()
        .filter(entry -> entry.getKey().equalsIgnoreCase("Vinitha"))
        .map(Map.Entry::getValue)
        .findFirst()
        .orElse(null);

if (newKey != null && val1 != null && val2 != null) {
    comboMap.put(newKey, val1 + "_" + val2);
}

In this example, lambda expressions are used with the stream() operations to achieve the same result as the original code. It's possible to manipulate and work with maps using lambda expressions in Java.

英文:
Map&lt;String, String&gt; characterMap = new HashMap&lt;&gt;();
characterMap.put(&quot;Manikanta&quot;, &quot;Pretty&quot;);
characterMap.put(&quot;Amulya&quot;, &quot;VeryGood&quot;);
characterMap.put(&quot;Saroja&quot;, &quot;Good&quot;);
characterMap.put(&quot;Vinitha&quot;, &quot;Cool&quot;);
characterMap.put(&quot;Sravani&quot;, &quot;Good&quot;);
characterMap.put(&quot;Sameera&quot;, &quot;Good&quot;);
/*String key = characterMap.entrySet().stream().filter(entry -&gt; entry.getValue().equalsIgnoreCase(&quot;Good&quot;)).map(Map.Entry::getKey).findFirst().orElse(null);*/
Map&lt;String, String&gt; comboMap = new HashMap&lt;&gt;();
String newKey = null;
String val1 = null;
String val2 = null;
for (Map.Entry&lt;String, String&gt; entry : characterMap.entrySet()) {
if (entry.getKey().equalsIgnoreCase(&quot;Manikanta&quot;))
newKey = entry.getKey();
if (entry.getKey().equalsIgnoreCase(&quot;Amulya&quot;))
val1 = entry.getValue();
if (entry.getKey().equalsIgnoreCase(&quot;Vinitha&quot;))
val2 = entry.getValue();
}
comboMap.put(newKey, val1 + &quot;_&quot; + val2);

How can I implement this in a lambda expression? Is it really possible to insert existing map data to customize to add new map like the above code?

答案1

得分: 1

你无法这样做。

你正在尝试将一个包含6个元素的映射(地图)显然缩减为一个只包含单个元素的映射(地图)(如果这只是一个示例,并且结果映射(地图)除了键“Manikanta”->“VeryGood_Cool”之外,将来可能还会包含其他内容,你需要更新你的示例)。

流(我猜你所说的“在lambda表达式中”就是这个意思)在对流中的单个元素进行操作方面表现出色(因此,例如,“Saroja”->“Good”的概念),或者关于如何将整个流转换为不同的集合(例如:将整个流转换为键的列表)。

你可以编写一个自定义的收集器;它不会比这段代码更短,并且阅读起来会更困难。因此,我不认为有必要展示那种方法。

英文:

You can't.

You're attempting to reduce a map with 6 elements into, evidently, a map with a single element in it (if this is an example, and the resulting map will ever contain anything but exactly 1 key of value "Manikanta" -> "VeryGood_Cool", you're going to need to update your example).

streams (I gather that is what you mean by 'in lambda expression') excel at operating on a single element in the stream (so, say, the notion "Saroja"->"Good"), or on how to turn the entire stream into a different collection (such as: Turn the whole thing into a list of keys).

You could write a custom collector; it would not be any shorter than this code is, and would be a lot harder to read. Hence I don't see the point of showing that.

答案2

得分: 0

自从您只检查关键值,甚至不需要使用for循环:

comboMap.put(characterMap.containsKey("Manikanta") ? "Manikanta" : null,
             characterMap.get("Amulya") + "_" + characterMap.get("Vinitha"));
英文:

Since you only check on the key values, you don't even need a for loop:

comboMap.put(characterMap.containsKey(&quot;Manikanta&quot;)?&quot;Manikanta&quot;:null,
characterMap.get(&quot;Amulya&quot;)+ &quot;_&quot; + characterMap.get(&quot;Vinitha&quot;));

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

发表评论

匿名网友

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

确定