【问题标题】:Lookups on range of dates查找日期范围
【发布时间】:2017-11-22 13:36:58
【问题描述】:

我有一个表,我想在 1 个或多个列上查找

|Caller         | Receiver       |  Classification |
| 0060          |   001          |    International|
| 006017        |   001          |    Maxis-US     |
| 0060175559138 |   001323212232 |    Free         |

所以说我想要完全匹配,然后我可以将查找键存储在哈希图中

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;
}

Best Matches 也一样,但我不使用 hashmap 而是 trie

所以从马来西亚到美国的任何呼叫(除非是 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 的索引。

是否有任何类型的数据结构有助于进行范围检查?

【问题讨论】:

  • 你考虑过IntervalTree。有一个实现here
  • 正是我想要解决我的问题的数据结构。谢谢

标签: java indexing lookup


【解决方案1】:

TreeMap 是你的朋友。它具有 subMap、headMap 和 tailMap 方法,它们将返回范围。

它被实现为引擎盖下的红黑树。

【讨论】:

  • 我正在用 C++ 编写一个区间树,一旦完成,将检查 TreeMap。
猜你喜欢
  • 2015-10-05
  • 2016-09-22
  • 2016-10-08
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
相关资源
最近更新 更多