lew*_*ort 8 c# java data-structures
我NavigableMap在Java中多次使用该接口,它很方便.
具体来说,我喜欢使用它floorEntry和ceilingEntry方法,它们分别为您提供下一个最低或最高的映射条目.
我试图在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));
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
看看这有用吗?(查找不在我的地图中的索引)
1.0 =首先!
3.0 =第二!
不幸的是,该解决方案要求您编写自定义扩展。所以,我已经完成了,并将其作为要点上传:SortedDictionaryExtensions.cs。
List<T>.BinarySearch它通过将字典的键集合转换为列表来使用该方法。然后,在此处答案的帮助下,我们确定键是否存在,如果不存在,我们将下限和上限值作为按位补码,然后选择该方法需要哪个。
请注意,我尚未测试该算法的效率,但乍一看它似乎足够好。
你可以这样测试:
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));
Run Code Online (Sandbox Code Playgroud)