在Java中实现一种键值对(本质上不是HashMap)

在Java中实现一种键值对(本质上不是HashMap),java,algorithm,Java,Algorithm,在Java中实现以下场景的最佳方法是什么: 一种键值对机制,但它看起来是这样的: param1 + param2 + param3 -> output1 param1 + * + !param3 -> output2 param1 + text containing value param2 + * -> output3 '*' => any parameter !param => any value other than this parame

在Java中实现以下场景的最佳方法是什么:

一种键值对机制,但它看起来是这样的:

param1 + param2 + param3 -> output1   
param1 + * + !param3 -> output2  
param1 + text containing value param2 + * -> output3  

'*' => any parameter  
!param => any value other than this parameter  
text containing value 'param' => any data which contains the value 'param'. ex: aaaaa,bbb,param,ccc or aaaparambbb  
在我看来,HashMap使实现这种类型的映射变得困难。实现这种映射的最佳方法是什么?

我也在考虑将它们放在Oracle表中,并尝试编写一个过程,但在Java中可能有更好的方法。

听起来您需要的是一个NFA,其中参数是符号


实现这一点的方法可以是三重嵌套哈希映射。或者是hashmap的hashmap或hashmaps的hashmap

用于查询的psuedo代码大致如下:

//You would call this method to search. 
string query(string param1, string param2, string param3)
{
    // The essence of the logic is, if you give a value for param3,
    // then it will do the subquery of the hashMap that param3
    // is the key of, if you don't supply a value (or provide the wildcard)
    // it will search all the different hashmaps of the parent hashmap.
    // See below for an example
    if param1 != WILDCARD
    then subquery1(hashmap[param1], string param2, string param3);
    else for each x in hashmap, subquery1(x,string param2, string param3)
}

string subquery1(hashmap[hashmap[]] maps, string param2, string param3)
{
    // The essence of the logic is, if you give a value for param2,
    // then it will do the subquery of the hashMap that param2
    // is the key of, if you don't supply a value (or provide the wildcard)
    // it will search all the different hashmaps of the parent hashmap.
    if param2 != WILDCARD
    then subquery2(maps[param2], string param3);
    else for each x in maps, subquery2(x, string param3)
}

string subquery2(hashmap[] maps, string param3)
{
    if param3 != WILDCARD
    then return maps[param3]
    else for each x in maps, return maps[param3]
}
显然,您需要定义是否允许返回多个值以及如何解决此问题。您还需要确定参数3是否可以为空?问题陈述相当含糊,但我已经尽力回答了我认为你的问题所在

例如,如果您已将以下值添加到hashmap中。
键1、键2、键3=值1
键1、键2、键4=值2
如果搜索键1、*、键3,则返回值1。
如果搜索键1、*、*将得到value1,并返回value2

更新:
当您调用查询时(“key1”、“key3”)
由于param1是有效的(不是通配符),我们称之为subquery1(hashmap[“key1”],“”和“key3”)
在进入子查询1之前,对hashMap[“key1”]进行求值,但它返回另一个hashMap,
让我们将此hashmap称为hashmap2[]。因此子查询1实际上是通过(hashmap2[],“*”,“key3”)调用的

现在我们在子问题1中。
由于param2是“*”,因此我们将遍历hashmap2[]的所有值,
对于hashmap2[]中的每个hashmap3[],我们称之为子查询3(hashmap3[],“key3”)


此时,由于param3是有效的,我们调用hashmap3[“key3”],并返回value1

您应该使用a来表示值。这将允许您快速、轻松地浏览您的结构,并快速管理您的任意和非条件。您可以找到一个。

您需要准确地执行此过程,还是它只是一般机制的一个示例?如果是后者,那么它看起来就像是一个数据库的任务,没有理由重新发明weel并严格用java来实现。嗨,Rogach,这只是一个通用机制的示例。我唯一关心的是,我需要连接到数据库,想知道Java本身是否有更简单的方法。@史蒂文:你能给代码添加更多注释吗?我正在努力理解实际发生的事情,而且很难理解。我给代码添加了一些注释,史蒂文:我仍然很难理解指令流,你能用一个例子来解释指令流吗。事实上,我想理解其中的逻辑,并从你的方法中学习。我再次更新了它。我想我已经给了你你想要的执行计划,但是问一下你是否需要更多。如果您需要更多详细信息,请告诉我,如果您有此应用程序,发布更多详细信息可能会有所帮助,我手头上没有类似的应用程序或问题,但我从未使用过多个哈希图,因此这个问题引发了我的兴趣,我想从您的方法中学习。这是我第一次尝试理解多个hashMap,因此我可能会面临一些问题,尤其是我无法理解计算了`hashMap[“key1”],但它会返回另一个hashMap,我们将此hashMap称为hashmap2[]。所以子查询1实际上是用(hashmap2[],“*”,“key3”);`行,我的查询可能听起来很愚蠢,但我只是想获得一些关于HashMapThank Cornelius的知识。但trie用于表示树的每个节点上的字符。我们也可以用琴弦吗?谢谢z5h。我去看看