# HashMap 流与条件 Math.abs(double)

go评论59阅读模式

HashMap stream with criteria Math.abs(double)

# 问题

sourceHashMapFind.put("AAA", Arrays.asList(-5.6, 7.9, 5.7, 6.3));
sourceHashMapFind.put("BBB", Arrays.asList(0.6, 5.8, 6.9, 8.0));
sourceHashMapFind.put("CCC", Arrays.asList(0.5, 5.6, 6.9, 8.0));

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class NewClass {

public static void main(String[] args) {

Map<String, List<Double>> sourceHashMapFind = new HashMap<>();

sourceHashMapFind.put("AAA", Arrays.asList(-5.6, 7.9, 5.7, 6.3));
sourceHashMapFind.put("BBB", Arrays.asList(0.6, 5.8, 6.9, 8.0));
sourceHashMapFind.put("CCC", Arrays.asList(0.5, 5.6, 6.9, 8.0));

Map<String, Double> queryPositions = sourceHashMapFind.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.filter(entry -> Math.abs(entry.getValue().get(0)) > 1.0)
.distinct()
.collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue().get(0)));
}
}

run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: inference variable R has incompatible bounds
equality constraints: java.util.Map<K,U>
upper bounds: java.util.HashMap<java.lang.String,java.lang.Double>,java.lang.Object
at P2020_0928_stackOverflow_MyQuestion.NewClass.main(NewClass.java:21)
C:\Users\User1\AppData\Local\NetBeans\Cache.0\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\User1\AppData\Local\NetBeans\Cache.0\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 1 second)

https://i.stack.imgur.com/iaK7j.jpg

sourceHashMapFind=.put(&quot;AAA&quot;, Arrays.asList(-5.6, 7.9, 5.7, 6.3));
sourceHashMapFind=.put(&quot;BBB&quot;, Arrays.asList(0.6, 5.8, 6.9, 8.0));
sourceHashMapFind=.put(&quot;CCC&quot;, Arrays.asList(0.5, 5.6, 6.9, 8.0));

And I would like to generate another map of ==> HashMap<String, Double>

Here's my criteria. If absolute value of the 0 position is greater than 1. --> then save the Key and Value into the new queryPositions hashmap. Thank you in advance!

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class NewClass {

public static void main(String[] args) {

Map&lt;String, List&lt;Double&gt;&gt; sourceHashMapFind = new HashMap&lt;&gt;();

sourceHashMapFind.put(&quot;AAA&quot;, Arrays.asList(-5.6, 7.9, 5.7, 6.3));
sourceHashMapFind.put(&quot;BBB&quot;, Arrays.asList(0.6, 5.8, 6.9, 8.0));
sourceHashMapFind.put(&quot;CCC&quot;, Arrays.asList(0.5, 5.6, 6.9, 8.0));

HashMap&lt;String, Double&gt; queryPositions = sourceHashMapFind.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.filter(entry -&gt; Math.abs(entry.getValue().get(0)) &gt; 1.0)
.distinct()
.collect(Collectors.toMap(entry -&gt; entry.getKey(), entry -&gt; entry.getValue()));
}

}

Here's run output

run:
Exception in thread &quot;main&quot; java.lang.RuntimeException: Uncompilable source code - incompatible types: inference variable R has incompatible bounds
equality constraints: java.util.Map&lt;K,U&gt;
upper bounds: java.util.HashMap&lt;java.lang.String,java.lang.Double&gt;,java.lang.Object
at P2020_0928_stackOverflow_MyQuestion.NewClass.main(NewClass.java:21)
C:\Users\User1\AppData\Local\NetBeans\Cache.0\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\User1\AppData\Local\NetBeans\Cache.0\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 1 second)

And please see message. Thank you guys!
https://i.stack.imgur.com/iaK7j.jpg

# 答案1

Map<String, Double> queryPositions = sourceHashMapFind.entrySet().stream()
.filter(e -> Math.abs(e.getValue().get(0)) > 1.0)
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().get(0)));

Map<String, Double> queryPositions = sourceHashMapFind.entrySet().stream()
.filter(e -> Math.abs(e.getValue().get(0)) > 1.0)
.collect(Collectors.toMap(
e -> e.getKey(),
e -> e.getValue().get(0),
(oldValue, newValue) -> newValue,
TreeMap::new));

You were close, but there's no point in sorting the entries and then collecting to a default map (HashMap), which does not preserve insertion order. Also, why are you using .distinct()?

This is how I would do it:

Map&lt;String, Double&gt; queryPositions = sourceHashMapFind.entrySet().stream()
.filter(e -&gt; Math.abs(e.getValue().get(0)) &gt; 1.0)
.collect(Collectors.toMap(e -&gt; e.getKey(), e -&gt; e.getValue().get(0)));

This assumes that you want the first item of each list as the value of each entry of the new map.

If you need the entries sorted by key, you might want to create a TreeMap:

Map&lt;String, Double&gt; queryPositions = sourceHashMapFind.entrySet().stream()
.filter(e -&gt; Math.abs(e.getValue().get(0)) &gt; 1.0)
.collect(Collectors.toMap(
e -&gt; e.getKey(),
e -&gt; e.getValue().get(0),
(oldValue, newValue) -&gt; newValue,
TreeMap::new)));

This uses the overloaded version of Collectors.toMap that expects a factory for the map.

• 本文由 发表于 2020年9月29日 09:12:08
• 转载请务必保留本文链接：https://go.coder-hub.com/64111543.html
• hashmap
• java
• java-8
• java-stream
• key-value

go 47

go 56

go 58