我在Lookup值中查看Long值并返回对应值。为了不获取任何与值相关的错误,我将所有值都设置为Long类型。
这是我的代码
package RestClient;
public class Range {
public static Long upper;
public static Long value;
public Range(Long i, Long j) {
Range.upper = i;
Range.value = j;
}
}
package RestClient;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
public class Rangelookup {
public static String Lookup(Long key) {
NavigableMap<Long, Range> map = new TreeMap<Long, Range>();
map.put(5045076263000000000L, new Range(5045076263249899999L, 507866L));
map.put(5045076263249900000L, new Range(5045076263249999999L, 507866L));
map.put(5045076263250000000L, new Range(5045076263499899999L, 507868L));
map.put(5045076263499900000L, new Range(5045076263499999999L, 507868L));
map.put(5045076263500000000L, new Range(5045076263749899999L, 507867L));
map.put(5045076263749900000L, new Range(5045076263749999999L, 507867L));
map.put(5045076263750000000L, new Range(5045076263999899999L, 507869L));
map.put(5045076263999900000L, new Range(5045076263999999999L, 507869L));
map.put(5045075892250000000L, new Range(5045075892499899999L, 507867L));
map.put(5045075892499900000L, new Range(5045075892499999999L, 507867L));
map.put(5045075892000000000L, new Range(5045075892249899999L, 507869L));
map.put(5045075892249900000L, new Range(5045075892249999999L, 507869L));
String output = "";
Map.Entry<Long, Range> entry = map.floorEntry(key);
if (entry == null) {
output = "No Entry Found";
} else if (key <= entry.getValue().upper) {
output = entry.getValue().value.toString();
} else {
output = "No Entry Found";
}
System.out.println(output);
return output;
}
public static void main(String[] args) {
Lookup(5045076263249899985L);
}
}
我可以知道为什么它没有返回任何价值?为什么要进入“没有进入”案例?
我可以知道我做错了吗?
答案 0 :(得分:1)
找到的条目是:
map.put(5045076263000000000L, new Range(5045076263249899999L, 507866L));
但是,上限值5045075892249999999
小于查找键5045076263249899985
:
5045075892249999999
<
5045076263249899985
因此,您获得"No Entry Found"
。