Java HashSet-如何搜索

Java HashSet-如何搜索,java,hashmap,hashset,Java,Hashmap,Hashset,我有一个HashSet of(一个hashmap of(string)和一个列表of(hashmap of(两个string))) HashSet 现在,我需要使用字符串输入(StrA和StrB)进行搜索,这应该在 StrA-->字符串1 StrB-->字符串2 它应该返回字符串3和字符串4的hashMap 这就是我试过的 HashSet<HashMap<String,List<HashMap<String,HashMap<String,String>&

我有一个HashSet of(一个hashmap of(string)和一个列表of(hashmap of(两个string)))

HashSet
现在,我需要使用字符串输入(StrA和StrB)进行搜索,这应该在

  • StrA-->字符串1
  • StrB-->字符串2
它应该返回字符串3和字符串4的hashMap

这就是我试过的

HashSet<HashMap<String,List<HashMap<String,HashMap<String,String>>>>> ObjList;

public void getElement(String strA, String strB) {
    if(ObjList.contains(strA) && ObjList.contains(strB))
        System.out.println("Yes");
}
HashSet对象列表;
public void getElement(字符串strA、字符串strB){
如果(对象列表包含(strA)和对象列表包含(strB))
System.out.println(“是”);
}

除了迭代外部
集合
和内部
列表
(未测试)之外,别无选择:

HashSet-outer=/。。。
哈希映射结果=
outer.stream()
.findFirst(map1->map1.containsKey(string1))
.get()
.stream()
.findFirst(map2->map2.containsKey(string2))
.get();

使用
流的解决方案是:

HashSet<HashMap<String, List<HashMap<String, HashMap<String, String>>>>> fooSet = //;
String string1 = "string1";
String string2 = "string2";
HashMap<String, String> mapFound;


mapFound = fooSet.stream()                       // iterate over HashSet
       .filter(map -> map.containsKey(string1))  // keep maps that contains string1
       .findFirst()                              // take first map that match
       .orElseGet(HashMap::new)                  // take it really (or create new Map)
       .getOrDefault(string1, new ArrayList<>()) // take the List associated as value,or new List if not exists
       .stream()                                 // iterate over the list
       .filter(map -> map.containsKey(string2))  // keep maps that contains string2
       .findFirst()                              // take first map that match
       .orElseGet(HashMap::new)                  // take it really (or create new Map)
       .getOrDefault(string2, new HashMap<>());  // take HashMap associated as value, or new Map if not found

你能分享你已经尝试过的代码吗?谢谢@azro。我用的是foreach。当我的HashSet中的外部HashMaps上有1000个字符时,对搜索性能有何评论。@Itsmagokul okok,如果找不到,我已将第一个更改为添加安全性
HashSet<HashMap<String1,List<HashMap<String2,HashMap<String3,String4>>>>> outer = //...

HashMap<String3,String4> result =
   outer.stream()
        .findFirst(map1-> map1.containsKey(string1))
        .get()
        .stream()
        .findFirst(map2-> map2.containsKey(string2))
        .get();
HashSet<HashMap<String, List<HashMap<String, HashMap<String, String>>>>> fooSet = //;
String string1 = "string1";
String string2 = "string2";
HashMap<String, String> mapFound;


mapFound = fooSet.stream()                       // iterate over HashSet
       .filter(map -> map.containsKey(string1))  // keep maps that contains string1
       .findFirst()                              // take first map that match
       .orElseGet(HashMap::new)                  // take it really (or create new Map)
       .getOrDefault(string1, new ArrayList<>()) // take the List associated as value,or new List if not exists
       .stream()                                 // iterate over the list
       .filter(map -> map.containsKey(string2))  // keep maps that contains string2
       .findFirst()                              // take first map that match
       .orElseGet(HashMap::new)                  // take it really (or create new Map)
       .getOrDefault(string2, new HashMap<>());  // take HashMap associated as value, or new Map if not found
HashMap<String, String> mapFound;

for (HashMap<String, List<HashMap<String, HashMap<String, String>>>> map : fooSet) {
    if (map.containsKey(string1)) {
        List<HashMap<String, HashMap<String, String>>> list = map.get(string1);
        for(HashMap<String, HashMap<String, String>> map2 : list){
            if(map2.containsKey(string2)){
                mapFound = map2.get(string2);
            }
        }
    }
}