- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个 TreeMap
第一张 map 是:
Map<String, Double> m1 = new TreeMap();
第二个是:
Map<String,double []> m2 = new TreeMap();
我想在第一个映射中搜索第二个映射中的键,然后将第一个映射的值乘以第二个映射值的列表(对于相似的键)。
下面的代码对我来说效果很好,但是当TreeMaps
很大时,搜索需要更多时间,我想提高速度,我该如何进行二分搜索。另一个问题,最快的搜索方法是 TreeMap
还是 HashMap
?
double[] finalSum = new double[N];
for ( Map.Entry<String,Double> entry : m1.entrySet() ) {
if ( m2.containsKey(entry.getKey()) ) {
//if the key is common in map1 and map2, compare the values
double y=entry.getValue();
double j[]=m2.get(entry.getKey());
for (int u=0;u<j.length;u++){
finalSum[u] += y * j[u];
}}}
提前致谢:)
最佳答案
HashMap 和 TreeMap 的 Big-O 表示法:
get containsKey next Notes
HashMap O(1) O(1) O(h/n) h is the table capacity
TreeMap O(log n) O(log n) O(log n)
ConcurrentHashMap O(1) O(1) O(h/n) h is the table capacity
如果您想要从 TreeMap 中检索其值的键已知。我建议宁愿使用 HashMap,因为 Hashmap 的 Big-O 是 O(1) 来获取元素,与 TreeMap 相比,它是 O(log n)。
I want to search the Key in first map to the second one, and then multiply the value of the first map to the list of second map values (for the similar keys).
通过使用 HashMap,您可以使用键从两个 HashMap 中检索值,并返回第一个值与第二个 hashMap 中的值的乘积。
关于java - TreeMap 中的二分查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43424992/
我正在尝试编写一个程序,在名为 items 的数组中进行顺序搜索和二分搜索,该数组具有 10000 个已排序的随机 int 值。第二个名为 targets 的数组加载了 1000 个 int 值(50
当我尝试使用图表并为其编写一些代码但没有成功时,我遇到了一个问题:/!! 我想创建一些东西来获取图形数据并检查它是否:1- 连接2-二分法3-有循环4-是一棵树 所以我想知道,例如,是否可以将其写入以
我是一名优秀的程序员,十分优秀!