查找第一个大于 SortedMap 的值

发布于 2024-09-19 18:52:58 字数 858 浏览 5 评论 0原文

我想知道有什么更好的方法可以在大型 SortedMap 中找到大于输入值的第一个值,而不是循环遍历下面示例中的所有值。或者如果 SortedMap 是用于此目的的最佳结构。

这可以使用 google-collections 来实现吗? 提前致谢

public class mapTest {
public static void main(String[] args) {

SortedMap<Double, Object> sortedMap = new TreeMap<Double, Object>();
    sortedMap.put(30d, "lala");     
    sortedMap.put(10d, "foo");
    sortedMap.put(25d, "bar");
    System.out.println("result: " + findFirstValueGreaterThan(sortedMap, 28d));
}

public static Object findFirstValueGreaterThan(SortedMap<Double, Object> sortedMap, Double value) {
    for (Entry<Double, Object> entry : sortedMap.entrySet()) {
        if (entry.getKey() > value) {
            // return first value with a key greater than the inputted value
            return entry.getValue();
        }
    }
    return null;
}
}

I'd like to know what is there a better way to find the first value greater than an inputted value in a large SortedMap instead of looping through all values in my example below. Or if SortedMap is a the best structure to use for this.

Could this be achieved using google-collections?
Thanks in advance

public class mapTest {
public static void main(String[] args) {

SortedMap<Double, Object> sortedMap = new TreeMap<Double, Object>();
    sortedMap.put(30d, "lala");     
    sortedMap.put(10d, "foo");
    sortedMap.put(25d, "bar");
    System.out.println("result: " + findFirstValueGreaterThan(sortedMap, 28d));
}

public static Object findFirstValueGreaterThan(SortedMap<Double, Object> sortedMap, Double value) {
    for (Entry<Double, Object> entry : sortedMap.entrySet()) {
        if (entry.getKey() > value) {
            // return first value with a key greater than the inputted value
            return entry.getValue();
        }
    }
    return null;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷雾森÷林ヴ 2024-09-26 18:52:58

这一切都在文档中:

ceilingKey( K键)
返回大于或等于给定键的最小键,如果没有这样的键,则返回 null。

因此,

findFirstValueGreaterThan(sortedMap, 28d)

应该

sortedMap.ceilingKey(28d)

注意“大于”和“大于或等于”之间的区别。

It's all in the docs:

ceilingKey(K key)
Returns the least key greater than or equal to the given key, or null if there is no such key.

So,

findFirstValueGreaterThan(sortedMap, 28d)

should be

sortedMap.ceilingKey(28d)

Pay attention at difference between "greater than" and "greater than or equal to", though.

爱本泡沫多脆弱 2024-09-26 18:52:58

这个解决方案只需要SortedMap。请注意,tailMap 通常不会创建新地图,因此速度很快。

public static <K extends Comparable<K>, V> V
        findFirstValueGreaterThan(SortedMap<K, V> map, K value) {
    Iterator<Entry<K, V>> it = map.tailMap(value).entrySet().iterator();
    if (it.hasNext()) {
        Entry<K, V> e = it.next();
        if (e.getKey().compareTo(value) > 0) {
            return e.getValue();
        } else if (it.hasNext()) {
            return it.next().getValue();
        }
    }
    return null;
}

This solution only requires SortedMap. Please note that tailMap typically doesn't create a new map, so it's fast.

public static <K extends Comparable<K>, V> V
        findFirstValueGreaterThan(SortedMap<K, V> map, K value) {
    Iterator<Entry<K, V>> it = map.tailMap(value).entrySet().iterator();
    if (it.hasNext()) {
        Entry<K, V> e = it.next();
        if (e.getKey().compareTo(value) > 0) {
            return e.getValue();
        } else if (it.hasNext()) {
            return it.next().getValue();
        }
    }
    return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文