在Java中实现某种键值对(本质上不是HashMap) [英] Implementing a sort of key-value pair (not essentially a HashMap) in Java

查看:63
本文介绍了在Java中实现某种键值对(本质上不是HashMap)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the best way to implement the following scenario in Java:

一种键值对机制,但看起来像这样:

A sort of key-value pair mechanism, but it looks like this:

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中可能有更好的方法。

In my view, a HashMap makes it difficult to implement this type of mapping. What is the best way to implement some mapping like this ?
I was also considering putting these in an Oracle table and trying to write a procedure, but there might be a better way in Java.

推荐答案

一种实现此目的的方法可以是三层嵌套的哈希映射。

A way of achieving this could be a triple nested hash-map. Or a hashmap of hashmaps of hashmaps.

用于查询的伪代码类似于:

The psuedo-code for querying would be something along the lines of this:

//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是否可为空?问题陈述含糊不清,但我已尽力回答我认为您的问题所在。

Obviously you need to define if multiple values are allowed to be returned and how you wish to resolve this. Also you need to determine if param 3 is nullable? The problem statement is pretty vague but I've done my best to answer what i think your problem is.

例如,如果您将以下值添加到您的hashmap。

key1,key2,key3 = value1

key1,key2,key4 = value2

如果搜索key1,*,key3,则将返回value1 。

如果搜索key1,*,*,则将得到value1,并返回value2。

An example would be if you have added the following values to your hashmap.
key1, key2, key3 = value1
key1, key2, key4 = value2
If you searched key1, *, key3 you would get value1 returned.
If you searched key1, *, * you would get value1, and value2 returned.

更新:

调用query( key1, , key3) ;

因为param1有效(不是通配符),所以我们调用subquery1(hashmap [ key1],
, key3);

虽然subquery1会计算hashMap [ key1],但它会返回另一个哈希图,

将此哈希图称为hashmap2 []。因此,实际上使用(hashmap2 [], *, key3)来调用subquery1;

Update:
When you call query("key1", "", "key3");
Since param1 is valid (not wildcard) we call subquery1(hashmap["key1"], "
", "key3");
Before we get to subquery1 though, hashMap["key1"] is evaluated, but it returns another hashmap,
lets call this hashmap hashmap2[]. So subquery1 is actually called with (hashmap2[], "*", "key3");

现在我们在子查询1中。

由于param2为 *,因此我们遍历hashmap2 []的所有值,

对于hashmap2 []中的每个hashmap3 [],我们将其称为subquery3(hashmap3 [], key3);

Now we are in subquery1.
Since param2 is "*", we then iterate through all the values of hashmap2[],
for each hashmap3[] in hashmap2[], we call subquery3(hashmap3[], "key3");

这时由于param3有效,我们调用hashmap3 [ key3]并返回传回value1;

At this point since param3 is valid, we call hashmap3["key3"] and we get value1 returned;

这篇关于在Java中实现某种键值对(本质上不是HashMap)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆