HashMap 流与条件 Math.abs(double)

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

HashMap stream with criteria Math.abs(double)

问题

请帮助我学习在地图之间导航。我有一个 Map<String, List> 的哈希映射。

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>,根据以下标准:

如果第一个位置的绝对值大于1,则将键和值保存到新的 queryPositions 哈希映射中。非常感谢!

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
英文:

Please help me learn navigating between maps. I've a hashmap of Map<String, List<Double>>

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

得分: 0

你很接近了,但是将条目排序然后收集到不保留插入顺序的默认映射(HashMap)中是没有意义的。另外,你为什么在使用 .distinct() 呢?

我会这样做:

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)));

这假设你想要每个列表的第一个项目作为新映射的每个条目的值。

如果你需要按键排序的条目,你可能想要创建一个 TreeMap

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));

这使用了 Collectors.toMap 的重载版本,它期望一个用于创建映射的工厂函数。

英文:

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.

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

发表评论

匿名网友

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

确定