Navigablemap. Java Equivalent in C sharp of floorentry, ceilingentry
I use the navigablemap interface many times in Java, which is very convenient
Specifically, I like to use its floorentry and ceilingentry methods, which provide you with the next lowest or highest map entry, respectively
I tried to find their equivalents in c#, but I was brief The following is an example I want to get
I've seen c#sorteddictionary and the extension method. Although it looks like it's on the court, I haven't found what I'm looking for
thank you! large size
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)); } }
The output is:
Solution
Unfortunately, this solution requires you to write custom extensions So I've finished it and uploaded it as a point: sorteddictionaryextensions cs.
It utilizes list < T > by converting the dictionary's key set into a list Binarysearch method Then, with the help of the answer here, we determine whether the key exists. If it does not exist, we take the floor and ceiling values as the bitwise complement, and then select the method we need
Please note that I haven't tested the efficiency of this algorithm, but it seems good at first glance
You can test it like this:
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));