验证Java中的字符串层次结构
我有一个地图,可以有键:验证Java中的字符串层次结构,java,predicate,Java,Predicate,我有一个地图,可以有键:a,B,C,D,E,F或它的子集 其中,A、B、C和D是一组按相同顺序属于某个层次结构的字符串:A->B->C->D 我需要在HashMap中检查映射是否包含有效的层次结构。地图存储数据的顺序无关紧要。我不在乎顺序,也不检查地图上的顺序 如果映射包含以下任一项,则将其视为有效的层次结构: A or A, B or // A and B can be in any order A, B, E or // as E is not in the group, so doesn'
a,B,C,D,E,F
或它的子集
其中,A、B、C和D
是一组按相同顺序属于某个层次结构的字符串:A->B->C->D
我需要在HashMap
中检查映射是否包含有效的层次结构。地图存储数据的顺序无关紧要。我不在乎顺序,也不检查地图上的顺序
如果映射包含以下任一项,则将其视为有效的层次结构:
A or
A, B or // A and B can be in any order
A, B, E or // as E is not in the group, so doesn't matter
A, B, C or // these can be in any order in map.
A, B, C, D or
如果map
具有A、B和D
,但不具有C
,则它将被视为无效
我可以添加多个if-else
check,但这会使代码变得凌乱。有什么优雅的方式来做这个检查吗
编辑:
我实现了以下逻辑:
// order contains strings in order: { A, B, C, D}
private boolean isHierarchyComplete(Map<String, Object> map, String[] order) {
// lastIndexFound, this will give the last index of string found in hierarchy that is continous and breaks out once the path is broken
int lastIndexFound = -1;
for (String str : order) {
if (map.containsKey(str)) {
lastIndexFound++;
} else {
break;
}
}
// if nothing found from path
if (lastIndexFound == -1) {
return false;
}
// if all found from path
if (lastIndexFound == order.length - 1) {
return true;
}
// now check after lastIndexFound if there are any values from hierarchy,
// if present return false
for (int index = lastIndexFound + 1; index < order.length; index++) {
if (map.containsKey(order[index])) {
return false;
}
}
return true;
}
//order包含的字符串的顺序为:{A,B,C,D}
私有布尔值IsherArchyComplete(映射映射,字符串[]顺序){
//lastIndexFound,这将给出在层次结构中找到的字符串的最后一个索引,该索引是连续的,并且在路径断开后断开
int lastIndexFound=-1;
for(字符串str:order){
if(地图容器(str)){
lastIndexFound++;
}否则{
打破
}
}
//如果在路径中找不到任何内容
如果(lastIndexFound==-1){
返回false;
}
//如果从路径中找到所有
if(lastIndexFound==order.length-1){
返回true;
}
//现在在lastIndexFound之后检查是否有来自层次结构的值,
//如果存在,则返回false
对于(int index=lastIndexFound+1;index
因此,本质上,您有某些键,除非它们的“父”键也在那里,否则它们不能出现:
String[] keyHierarchy = "A B C D".split(" ");
您正在实现类似于
boolean hasValidKeysAsPerHierarchy(Set<String> keySet, ArrayList<String> hierarchy) { ... }
如果层次结构的大小(我们称之为
h
)非常大,可以通过构建一个辅助Map
将层次结构键映射到它们在层次结构中的位置,将第一个for循环从O(n*h)
加速到O(n*log(h))
(工作示范)
在上面的示例(3)中,请注意真值是如何从TRUE变为FALSE,然后又变为TRUE的。在所有这些情况下,我们都会说map键集不符合我们的层次结构
时间复杂度也应该是
O(n)
,其中n是层次结构数组的长度。请注意,常规JavaHashMap
的键没有顺序,这意味着如果键a
、B
和C
存在,则迭代键可以按任何顺序返回它们。类似这样的情况?如果(!map.contains(“A”)| |!map.contains(“B”)|!map.contains(“C”)| |!map.contains(“D”))else valid无效
@TimBiegeleisen yes Map不保留顺序。我不在乎Map order Map保留数据的是什么。我想检查Map中的所有值是否都来自我提到的字符串组。甚至我都不理解否决票的原因?因为我觉得问题很清楚。UpVoting如果需要支持有限的、固定的并考虑已知的一组密钥,考虑到<代码> eNUM <代码>,并使用<代码>枚举< /COD>。这将阻止您使用不正确的密钥。然后,密钥的正确性将得到保证。谢谢,类似于我所写的。我已经编辑了我的答案,我的实现。您的外观更干净。但是失败了一个案例。如果没有字符串是PAS。来自层次结构组的sed。它打印为true。应为false。
boolean hasValidKeysAsPerHierarchy(Set<String> keySet, List<String> hierarchy) {
int lastHKPos = -1;
for (String k : keySet) {
lastHKPos= Math.max(hierarchy.indexOf(k), lastHKPos);
}
// now, we need to find all hierarchy-keys from 0 to lastHKPos, exclusive
for (int i=0; i<lastHKPos; i++) {
String hKey = hierarchy.get(i);
if ( ! keySet.contains(hKey)) return false; // missing hierarchy-key!
}
return true; // no missing hierarchy-keys
}
import java.util.HashMap;
public class MyClass {
public static void main(String args[]) {
String[] hierarchy = {"A","B","C","D"};
//Test case 1: false
HashMap<String,String> map = new HashMap<String,String>();
map.put("A","#");
map.put("D","#");
isValidMap(map,hierarchy);
map = new HashMap<String,String>();
//Test case 2: true
map = new HashMap<String,String>();
map.put("A","#");
map.put("B","#");
isValidMap(map,hierarchy);
map = new HashMap<String,String>();
//Test case 3: true
map = new HashMap<String,String>();
map.put("E","#");
map.put("F","#");
isValidMap(map,hierarchy);
map = new HashMap<String,String>();
//Test case 4: true
map = new HashMap<String,String>();
map.put("A","#");
map.put("E","#");
isValidMap(map,hierarchy);
map = new HashMap<String,String>();
//Test case 5: true
map = new HashMap<String,String>();
map.put("A","#");
map.put("B","#");
map.put("C","#");
isValidMap(map,hierarchy);
map = new HashMap<String,String>();
//Test case 6: true
map = new HashMap<String,String>();
map.put("A","#");
map.put("B","#");
map.put("E","#");
isValidMap(map,hierarchy);
map = new HashMap<String,String>();
//Test case 7: false
map = new HashMap<String,String>();
map.put("A","#");
map.put("D","#");
map.put("E","#");
isValidMap(map,hierarchy);
map = new HashMap<String,String>();
}
public static void isValidMap(HashMap<String,String> map, String[] hierarchy){
boolean checkShouldBePresent = true;
boolean finalAns = true;
boolean changed = false;
boolean checked = false;
for(int i=0;i<hierarchy.length;i++){
String s = hierarchy[i];
boolean finalAnsPrev = finalAns;
finalAns = finalAns && !changed?map.keySet().contains(s):!map.keySet().contains(s);
if(finalAnsPrev!=finalAns && !checked){
changed = true;
finalAns = true;
checked = true;
}
}
System.out.println(finalAns);
}
}
String[] hierarchy = {"A","B","C","D"};
//keyset = set of {"A", "B", "E"}. The truth list of the hierarchy elements pertaining to whether they are present in the keyset or not is,
[A:TRUE,B:TRUE,C:FALSE,D:FALSE] ... (1)
//keyset = set of {"G", "F", "E"}. The truth list of the hierarchy elements pertaining to whether they are present in the keyset or not is,
[A:FALSE,B:FALSE,C:FALSE,D:FALSE]. ... (2)
//keyset = set of {"A", "B", "D"}. The truth list of the hierarchy elements pertaining to whether they are present in the keyset or not is,
[A:TRUE,B:TRUE,C:FALSE,D:TRUE]. ... (3)