- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 Java 中多次使用 NavigableMap
接口(interface),它很方便。
具体来说,我喜欢使用它的 floorEntry
和 ceilingEntry
方法,它们分别为您提供下一个最低或最高的 map 条目。
我试图在 C# 中找到这些的等价物,但我做空了。下面是我想要获得的示例。
我查看了 C# SortedDictionary
和扩展方法,虽然看起来差不多,但我还没有找到我正在寻找的东西。
谢谢!大号
package com.lewis.needsanavigablemapincsharp;
import java.util.NavigableMap;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
NavigableMap<Float, String> neededMap = new TreeMap<Float, String>();
neededMap.put(1.0f, "first!");
neededMap.put(3.0f, "second!");
System.out.println("see how useful this is? (looking up indices that aren't in my map)");
System.out.println(neededMap.floorEntry(2.0f));
System.out.println(neededMap.ceilingEntry(2.0f));
}
}
输出是:
see how useful this is? (looking up indices that aren't in my map)
1.0=first!
3.0=second!
最佳答案
不幸的是,该解决方案要求您编写自定义扩展。所以,我已经完成了,并将其作为要点上传:SortedDictionaryExtensions.cs .
它利用了 List<T>.BinarySearch
通过将字典的键集合转换为列表的方法。然后,借助帮助来回答here ,我们确定 key 是否存在,如果不存在,我们得到下限值和上限值作为按位补码,然后选择我们需要的方法。
请注意,我还没有测试过该算法的效率,但乍一看似乎已经足够好了。
你可以这样测试:
SortedDictionary<float, string> neededMap = new SortedDictionary<float, string>();
neededMap.Add(1.0f, "first!");
neededMap.Add(3.0f, "second!");
Console.WriteLine("see how useful this is? (looking up indices that aren't in my map)");
Console.WriteLine(neededMap.FloorEntry(2.0f));
Console.WriteLine(neededMap.CeilingEntry(2.0f));
关于java - Java 的 NavigableMap.floorEntry、ceilingEntry 的 C Sharp 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25299386/
我正在尝试使用自定义比较器实现 NavigableMap,该比较器基于值而不是键进行比较。我对 java 和编码非常了解,所以如果我的术语不正确或代码很糟糕,请原谅我!我尝试找到类似的问题来尝试复制解
java.util.Collections 目前提供以下实用方法来创建 synchronized各种集合接口(interface)的包装器: synchronizedCollection(Collec
我希望能够根据文件输入 map 的日期对文件进行排序。键是日期,值是在相应日期添加的 n 个文件的列表。但是,我需要能够一次将一个文件添加到列表中 - 但我陷入了语法困境。如何从 Map.put()
在 Java 中,NavigableMap 有一个方法 subMap(),它返回 map 部分的 View ,其键范围从 fromKey 到到 key 。在文档中,它说返回的 map 受此 map 支
这是 my previous question 的后续问题. 我正在尝试实现 NavigableMap定义为NavigableMap 。我需要这张 map 作为我的缓存。每 5 分钟我刷新一次 Nav
有什么理由使用SortedMap而不是 NavigableMap ,除了JVM版本? (NavigableMap 从 1.6 开始才出现;SortedMap 从 1.2 开始出现) 我正在尝试找到具有
在 Java 1.6 中,NavigableMap (和 NavigableSet )接口(interface)被引入和 TreeMap已更新以实现新界面。在其他方面,NavigableMap 可用于
我不明白怎么办NavigableMap.floorEntry()和ceiling Entry()工作。 Oracle 只是简单地写了 ceilingEntry(key) : Returns a key
我有一个具有以下格式的 NavigableMap: NavigableMap mymap = new ConcurrentSkipListMap (); //.......some operati
我需要有效的ImmutableNavigableListMultimap哪里HasOffset看起来像: interface HasOffset { public int getOffset()
我是 Java 初学者。我有一个方法如下: public void time_filter(Long t1, Long t2){ NavigableMap time_map = new Tr
我需要将可导航 map 转换为二维字符串数组。下面给出的是来自 answer 的代码回答我之前的一个问题。 NavigableMap map = new TreeMap(); map.
如果我有一个已经形成的NavigableMap。 floorEntry() 操作执行所需的时间是多少?是 O(1) 还是 O(logn)? 例如: 如果我有一个具有 n 个间隔的 NavigableM
关于 Java TreeMap 方法(或 NavigableMap 接口(interface)): public SortedMap headMap(K toKey) public Navigable
有什么方法可以在 Java 中循环 NavigableMap 吗?我想访问 NavigableMap 中的所有项目。 最佳答案 与循环任何集合的方式相同,使用迭代器或 for-each 循环。 Nav
java.util.Collections 有几个 unmodifiable 方法,它们通过将集合包装在禁止突变操作的装饰器中来提供不可修改的 Collection View 。 Java 6 添加了
LinkedHashMap public class LinkedHashMap extends HashMap implements Map 1、 Map接口的哈希表和链接列表实现,具
您能解释一下这段代码吗? TreeMap myMap = new TreeMap(); myMap.put("a", 50); myMap.put("b", 60);
我已经实现了几次 NavigableMap,但它似乎总是比应该做的多一些。虽然它是一个非常大的接口(interface),但诸如 java.util.AbstractMap 和 Guava 的 For
我正在寻找一种缓存设施,可以在预定义的间隔到期时删除旧条目(如 Guava Cache),但内部使用 NavigableMap 接口(interface)。 有类似的事情吗? 最佳答案 我不相信有类似
我是一名优秀的程序员,十分优秀!