트리플 중첩 된 해시 맵이 될 수 이것을 달성하는 방법입니다. 또는 해시 맵의 해시 맵 해시 맵.
//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]
}
은 분명히 여러 값을 반환 할 수있는 경우 정의해야하고 어떻게이 문제를 해결하려면 :
질의에 대한 사이비 - 코드는 다음의 라인을 따라 뭔가 될 것입니다. 또한 param 3이 nullable인지 확인해야합니까? 문제 성명서는 매우 모호하지만, 귀하의 문제가 무엇이라고 생각하는지 대답하기 위해 최선을 다했습니다.
해시 맵에 다음 값을 추가 한 경우를 예로들 수 있습니다.
key1, key2, key3 = value1
key1, key2, key4 = value2
key1, *, key3을 검색하면 value1이 반환됩니다.
key1, *, *을 검색하면 value1이 반환되고 value2가 반환됩니다.
업데이트 :
당신이 전화를 쿼리 ("키 1", "", "KEY3");
param1이 유효하므로 (와일드 카드가 아님) subquery1 (hashmap [ "key1"], "", "key3")을 호출합니다.
하지만 하위 쿼리 1에 도달하기 전에 hashMap [ "key1"]이 평가되지만 다른 해시 맵을 반환합니다.
이 해시 맵 hashmap2 []를 호출 할 수 있습니다. 따라서 subquery1은 실제로 (hashmap2 [], "*", "key3")와 함께 호출됩니다.
이제 우리는 하위 쿼리 1에 있습니다.
param2가 "*"이므로 hashmap2 []의 각 hashmap3 []에 대해 hashmap2 [],
의 모든 값을 반복하고 subquery3 (hashmap3 [], "key3")을 호출합니다.
이 시점에서 param3이 유효하기 때문에 hashmap3 [ "key3"]을 호출하고 value1이 반환됩니다.
이 절차를 정확하게 구현해야합니까, 아니면 일반 메커니즘의 샘플입니까?후자라면 데이터베이스에 대한 작업처럼 보입니다.이 작업을 자바에서 엄격하게 재발견 할 필요는 없습니다. – Rogach
안녕하세요, Rogach는 일반적인 메커니즘의 샘플 일뿐입니다. 내 유일한 관심사는 데이터베이스에 연결해야하며 Java 자체에서 더 간단한 방법이 있는지 알아 내야한다는 것입니다. –