我有一个表,我想在一列或多列上查找
说
|Caller | Receiver | Classification |
| 0060 | 001 | International|
| 006017 | 001 | Maxis-US |
| 0060175559138 | 001323212232 | Free |
所以说我想要完全匹配然后我可以将查找键存储在hashmap
中Map<String, String> map = new HashMap<>();
//key = caller, value = classification
map.set("0060","International");
map.set("0060175559138 ","001323212232");
I will do the same with the other column
//key =receiver, value = classification
map.set("001","International");
map.set("001","Maxis-US");
map.set("001323212232","Free");
so to get the correct classification on a lookup of caller = 0060175559138 and receiver = 001323212232 I do an intersection
String classfication1 = map.get("0060175559138")
Set result1 = new HashSet(1);
result1.add(classfication1);
String classfication2 = map.get("001323212232")
Set result2 = new HashSet(1);
result2.add(classfication1 );
Set<String> finalResult = intersection(result1,result2);
public Set<V> intersection(Set<V> s1, Set<V> s2) {
Set<V> set;
set = new HashSet<>(s1);
set.retainAll(s2);
if (set.isEmpty()) {
throw new NoMatchException("No match found");
}
return set;
}
最佳匹配也会有相同的效果,我希望我不会使用散列图而是使用特里
所以从马来西亚到美国的任何电话(除非是0060175559138,致电001323212232)将返回国际分类
例如0060124538738至00134646547
我现在的问题是我有一组数据,但只能在某些时间有效
|Caller | Receiver | Classification |Valid From | Valid To |
| 0060 | 001 | International| | |
| 006017 | 001 | Maxis-US | | |
| 0060175559138 | 001323212232 | Free |20170101000000|20180101000000|
所以如果0060175559138在2017年之间拨打电话20170102150000,则其分类为免费而非国际。
我无法使用Valid From和Valid To作为hashmap或trie的索引。
是否有任何类型的数据结构可以帮助进行范围检查?
答案 0 :(得分:0)
TreeMap是你的朋友。它有subMap,headMap和tailMap方法,它们将返回范围。
它被实施为引擎盖下的红黑树。