简体   繁体   English

具有标准 Math.abs(double) 的 HashMap 流

[英]HashMap stream with criteria Math.abs(double)

Please help me learn navigating between maps.请帮助我学习在地图之间导航。 I've a hashmap of Map<String, List>我有 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));

And I would like to generate another map of ==> HashMap<String, Double>我想生成另一个地图 ==> 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.如果 0 位置的绝对值大于 1。 --> 然后将 Key 和 Value 保存到新的 queryPositions 哈希图中。 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<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));

        
        
         HashMap<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()));
    }
    
    
}

Here's run output这是运行输出

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\9.0\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\User1\AppData\Local\NetBeans\Cache\9.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 https://i.stack.imgur.com/iaK7j.jpg

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.您已经接近了,但是对条目进行排序然后收集到默认映射 ( HashMap ) 是没有意义的,该映射不保留插入顺序。 Also, why are you using .distinct() ?另外,你为什么使用.distinct()

This is how I would do it:这就是我将如何做到的:

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

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 :如果您需要按键排序的条目,您可能需要创建一个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)));

This uses the overloaded version of Collectors.toMap that expects a factory for the map.这使用了期望地图工厂的Collectors.toMap的重载版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM