Java 按近似ID值从hashmap获取消息,但不相同
假设我有这样一个HashMap:Java 按近似ID值从hashmap获取消息,但不相同,java,hashmap,Java,Hashmap,假设我有这样一个HashMap: message.put(10, "Message 1"); message.put(20, "Message 2"); message.put(30, "Message 3"); message.put(40, "Message 4"); 我会收到一条比较给定ID的消息: if( message.containsKey(sampleValue) ) { return message.get(sampleValue); } 但是如果消息hashmap中
message.put(10, "Message 1");
message.put(20, "Message 2");
message.put(30, "Message 3");
message.put(40, "Message 4");
我会收到一条比较给定ID的消息:
if( message.containsKey(sampleValue) ) {
return message.get(sampleValue);
}
但是如果消息hashmap中不包含sampleValue,那么这是没有用的。有没有一种方法或函数可以通过近似的ID值获得它?例如,如果sampleValue为19,它将返回“消息2”。如何实现这一点?我相信您正在尝试获取与
输入最接近的值。然后我建议首先从键集
中获取最接近的整数。一旦你得到这个值,你只需要使用map.get(壁橱int)
来检索这个值
HashMap<Integer, String> message = new HashMap<>();
message.put(10, "Message 1");
message.put(20, "Message 2");
message.put(30, "Message 3");
message.put(40, "Message 4");
int input = 19; // change the input as you want, I have set it 19 for testing
Integer c = message.keySet().stream()
.min(Comparator.comparingInt(i -> Math.abs(i - input))).get(); // find the closest number to the input
String result = message.get(c);
System.out.println("result : "+result);
您可以扩展HashMap
,以添加所需的特定行为:
public class ApproximateKeyMap<K, V> extends HashMap<K, V> {
@Override
public V get(Object key) {
// Map your key -> approximated key
K approximatedKey = ...
return super.get(approximatedKey);
}
}
您可以使用TreeMap
执行任务。它包含从右/左返回最近键的ceilingKey
/floorKey
方法。因此,这些方法可用于查找最近的键并在O(Log(N))
中检索其对应值
class ClosestKeyTreeMap extends TreeMap<Integer, Object> {
public Object getClosestValue(int key) {
Integer leftKey = this.floorKey(key);
Integer rightKey = this.ceilingKey(key);
if (leftKey == null && rightKey == null) {
return null;
} else if (rightKey == null) {
return this.get(leftKey);
} else if (leftKey == null) {
return this.get(rightKey);
}
int leftDiff = key - leftKey;
int rightDiff = rightKey - key;
if (leftDiff < rightDiff) {
return this.get(leftKey);
} else {
return this.get(rightKey);
}
}
}
类ClosestKeyTreeMap扩展了TreeMap{
公共对象getClosestValue(int键){
整数leftKey=this.floorKey(key);
整数rightKey=this.ceilingKey(key);
if(leftKey==null&&righkey==null){
返回null;
}else if(rightKey==null){
返回这个。get(leftKey);
}else if(leftKey==null){
返回此。获取(右键);
}
int leftDiff=键-leftKey;
int rightDiff=rightKey-key;
if(leftDiff
通过操作整数除法,您可以更容易地四舍五入到最接近的10:((输入+5)/10)*10
最好使用实现此逻辑的方法getClosest()
创建一个近似hashmap
类。它的构造函数还可以接受一个参数,允许创建者指定逻辑,以确定对于任何给定实例,was是“最接近的”。为什么假设映射不是空的?
public class ApproximateKeyMap<K, V> extends HashMap<K, V> {
@Override
public V get(Object key) {
// Map your key -> approximated key
K approximatedKey = ...
return super.get(approximatedKey);
}
}
public class ApproximateKeyMap<K, V> extends HashMap<K, V> {
public V getApproximate(Object key) {
// Map your key -> approximated key
K approximatedKey = ...
return super.get(approximatedKey);
}
}
class ClosestKeyTreeMap extends TreeMap<Integer, Object> {
public Object getClosestValue(int key) {
Integer leftKey = this.floorKey(key);
Integer rightKey = this.ceilingKey(key);
if (leftKey == null && rightKey == null) {
return null;
} else if (rightKey == null) {
return this.get(leftKey);
} else if (leftKey == null) {
return this.get(rightKey);
}
int leftDiff = key - leftKey;
int rightDiff = rightKey - key;
if (leftDiff < rightDiff) {
return this.get(leftKey);
} else {
return this.get(rightKey);
}
}
}