英文:
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("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>
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<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.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<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
:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论