Not directly, but you can go over the entries and search for it:
public static BigDecimal findPartialKey(Map<String, BigDecimal> map, String search) {
return map.entrySet()
.stream()
.filter(e -> e.getKey().startsWith(search))
.map(Map.Entry::getValue)
.findFirst()
.orElse(null);
}
Note the using a stream like this, while (questionably) elegant, doesn't take advantage of the fact that the keys in a TreeMap
are sorted, and may waste time looking for a matching key in a region that can't contain it. Using a good old-fashioned loop may be a bit clunkier, but in the general case, should perform a bit better:
public static BigDecimalfindPartialKey(SortedMap<String, BigDecimal> map, String search) {
Iterator<Map.Entry<String, Double>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Double> entry = iter.next();
String key = entry.getKey();
if (key.startsWith(search)) {
return entry.getValue();
}
if (key.compareTo(search) > 0) {
return null;
}
}
return null;
}