使用Java流从HashMap中删除作为值使用的ArrayList中的条目。

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

Deleting an entry from Arraylist using java stream which is used as value in hashmap

问题

以下是翻译好的部分:

Map<String, List<String>> HOSTING = new HashMap<String, List<String>>();
HOSTING.put("1", new ArrayList<String>(Arrays.asList("23", "45", "54")));
HOSTING.put("2", new ArrayList<String>(Arrays.asList("23", "62", "54")));
HOSTING.put("3", new ArrayList<String>(Arrays.asList("23", "45", "64")));
HOSTING.put("4", new ArrayList<String>(Arrays.asList("37", "45", "54")));

// 我想从键 "2" 中删除 "62"。

// 预期输出

HOSTING={1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}

我知道如何做到这一点但我想知道如何使用 Java 流来实现

Collection<List<String>> result = HOSTING.entrySet().stream()
    .filter(e -> "2".equals(e.getKey()))    
    .map(Map.Entry::getValue)    
    .collect(Collectors.toCollection(ArrayList::new));

这部分代码已经按照您的要求进行了翻译。如果您需要进一步的帮助或有其他问题,请随时提出。

英文:
Map&lt;String, List&lt;String&gt;&gt; HOSTING = new HashMap&lt;String, List&lt;String&gt;&gt;();
HOSTING.put(&quot;1&quot;, new ArrayList&lt;String&gt;(Arrays.asList(&quot;23&quot;,&quot;45&quot;,&quot;54&quot;)));
HOSTING.put(&quot;2&quot;, new ArrayList&lt;String&gt;(Arrays.asList(&quot;23&quot;,&quot;62&quot;,&quot;54&quot;)));
HOSTING.put(&quot;3&quot;, new ArrayList&lt;String&gt;(Arrays.asList(&quot;23&quot;,&quot;45&quot;,&quot;64&quot;)));
HOSTING.put(&quot;4&quot;, new ArrayList&lt;String&gt;(Arrays.asList(&quot;37&quot;,&quot;45&quot;,&quot;54&quot;)));

I want to delete "62" from key "2".

Expected output

HOSTING={1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}

I know how to do this but i want to know how to do it using java stream.

Collection&lt;List&lt;String&gt;&gt; result = HOSTING.entrySet().stream()
    .filter(e -&gt; &quot;2&quot;.equals(e.getKey()))    
    .map(Map.Entry::getValue)    
    .collect(Collectors.toCollection(ArrayList::new));

This will just give me arraylist if that key is present in hashmap. I also thought of using nested stream and predicates.

答案1

得分: 1

你可以这样做。

Map<String, List<String>> HOSTING = new HashMap<String, List<String>>();
HOSTING.put("1", new ArrayList<String>(Arrays.asList("23","45","54")));
HOSTING.put("2", new ArrayList<String>(Arrays.asList("23","62","54")));
HOSTING.put("3", new ArrayList<String>(Arrays.asList("23","45","64")));
HOSTING.put("4", new ArrayList<String>(Arrays.asList("37","45","54")));

System.out.println(HOSTING);
HOSTING.get("2").remove("62");
System.out.println(HOSTING);

输出

{1=[23, 45, 54], 2=[23, 62, 54], 3=[23, 45, 64], 4=[37, 45, 54]}
{1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}

或者使用流。注意:这只是一个使用映射访问适当条目并更改列表中适当值的折中方法。remove 方法返回一个布尔值,指示是否删除了该值,因此在这两种情况下,三元运算符返回 entry

HOSTING = HOSTING.entrySet().stream()
        .map(e -> (e.getKey().equals("2")
                && e.getValue().remove("62")) ? e : e)
        .collect(Collectors.toMap(Entry::getKey,
                Entry::getValue));

System.out.println(HOSTING);

输出

{1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}

但我不建议使用流来做这个。

英文:

You can do it like this.

Map&lt;String, List&lt;String&gt;&gt; HOSTING = new HashMap&lt;String, List&lt;String&gt;&gt;();
HOSTING.put(&quot;1&quot;, new ArrayList&lt;String&gt;(Arrays.asList(&quot;23&quot;,&quot;45&quot;,&quot;54&quot;)));
HOSTING.put(&quot;2&quot;, new ArrayList&lt;String&gt;(Arrays.asList(&quot;23&quot;,&quot;62&quot;,&quot;54&quot;)));
HOSTING.put(&quot;3&quot;, new ArrayList&lt;String&gt;(Arrays.asList(&quot;23&quot;,&quot;45&quot;,&quot;64&quot;)));
HOSTING.put(&quot;4&quot;, new ArrayList&lt;String&gt;(Arrays.asList(&quot;37&quot;,&quot;45&quot;,&quot;54&quot;)));

System.out.println(HOSTING);
HOSTING.get(&quot;2&quot;).remove(&quot;62&quot;);
System.out.println(HOSTING);

Prints

{1=[23, 45, 54], 2=[23, 62, 54], 3=[23, 45, 64], 4=[37, 45, 54]}
{1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}

Or with a stream. Note: This is a kludge that simply uses a map to access the appropriate entry and alter the appropriate value in the list. The remove method returns a boolean as to whether the value was removed so in both cases the ternary returns the entry.

HOSTING = HOSTING.entrySet().stream()
		.map(e -&gt; (e.getKey().equals(&quot;2&quot;)
				&amp;&amp; e.getValue().remove(&quot;62&quot;)) ? e : e)
		.collect(Collectors.toMap(Entry::getKey,
				Entry::getValue));
		
System.out.println(HOSTING);

Prints

{1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}

But I do not recommend using streams to do this.

huangapple
  • 本文由 发表于 2020年8月9日 05:10:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63320266.html
匿名

发表评论

匿名网友

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

确定