[英]how to iterate and compare over Hashtable<String, Map<String, Set<String>>> in java
我的結構如下
Key: active-csr-HA-profile & Value: {sapd-outside=[outside], sapd-extra4=[extra4], sapd-extra3=[extra3], sapd-inside=[inside]}
Key = sapd-outside, Value = [outside]
Key = sapd-extra4, Value = [extra4]
Key = sapd-extra3, Value = [extra3]
Key = sapd-inside, Value = [inside]
Key: standby-csr-HA-profile & Value: {sapd-outside=[outside], sapd-extra4=[extra4], sapd-extra3=[extra3], sapd-inside=[inside]}
Key = sapd-outside, Value = [outside]
Key = sapd-extra4, Value = [extra4]
Key = sapd-extra3, Value = [extra3]
Key = sapd-inside, Value = [inside]
上面的格式為Hashtable<String, Map<String, Set<String>>>
我想比較一下active-csr-HA-profile的sapd-out是否與standby-csr-HA-profile的鍵之一相同。 因此,將active-csr-HA配置文件的每個密鑰與standby-csr-HA配置文件的每個密鑰進行比較。
我看了一些類似的問題,但我正在解決的問題並未解決。
正如評論中已經提到的, Hashtable
被認為已過時。 替換為HashMap
。 如果希望以與Hashtable
相同的方式使HashMap
同步,請在其上使用Collections::synchronizedMap
裝飾器。
Hashtable
的結構看起來有點不清楚。 我猜下面的結構最適合您,我的解決方案基於此。
Hashtable<String, Map<String, Set<String>>> map = new Hashtable<>();
Map<String, Set<String>> activeCsrHAProfile = new HashMap<>();
activeCsrHAProfile.put("sapd-outside", new HashSet<>(Arrays.asList("outside")));
activeCsrHAProfile.put("sapd-extra4", new HashSet<>(Arrays.asList("extra4")));
activeCsrHAProfile.put("sapd-extra3", new HashSet<>(Arrays.asList("extra3")));
activeCsrHAProfile.put("sapd-inside", new HashSet<>(Arrays.asList("inside")));
Map<String, Set<String>> standbyCsrHAProfile = new HashMap<>();
standbyCsrHAProfile.put("sapd-outside", new HashSet<>(Arrays.asList("outside")));
standbyCsrHAProfile.put("sapd-extra4", new HashSet<>(Arrays.asList("extra4")));
standbyCsrHAProfile.put("sapd-extra3", new HashSet<>(Arrays.asList("extra3")));
standbyCsrHAProfile.put("sapd-inside", new HashSet<>(Arrays.asList("inside")));
map.put("active-csr-HA-profile", activeCsrHAProfile);
map.put("standby-csr-HA-profile", standbyCsrHAProfile);
如果我的結構與您的結構有些不同,則可以修改解決方案以匹配您的結構將沒有問題-原理是相同的。
Set<String> sapdOutsideOfActiveCsrHAProfile = map.get("active-csr-HA-profile")
.get("sapd-outside");
map.get("standby-csr-HA-profile").entrySet()
.stream()
.filter(i -> i.getValue().containsAll(sapdOutsideOfActiveCsrHAProfile))
.forEach(e -> System.out.println("Found at: " +
"key=" + e.getKey() + ", value=" + e.getValue()));
.filter(i -> i.getValue().containsAll(..)
過濾Set<String>
值包含所有必需的String的實體。 .forEach(..)
使使用者對所有匹配結果執行操作。 如果您需要表示匹配是否發生的boolean
,請執行以下操作:
boolean matches = map.get(..).entrySet().stream().filter(..).findFirst().isPresent();
如評論中所述, HashTable
是值得商a的選擇。 無論選擇哪種實現,都可以創建自己的類來管理雜亂的東西:
public class CustomMap extends Hashtable<String, Map<String, Set<String>>> {
public CustomMap() {
super();
}
public boolean compareEntries(String key1, String key2) {
if (!this.containsKey(key1) || !this.containsKey(key2) || this.get(key1).size() != this.get(key2).size())
return false;
for (String innerKey : this.get(key1).keySet()) {
if (!this.get(key2).containsKey(innerKey)) {
return false;
}
final Set<String> setA = this.get(key1).get(innerKey);
final Set<String> setB = this.get(key2).get(innerKey);
if (!setA.containsAll(setB) || !setB.containsAll(setA)) {
return false;
}
}
return true;
}
}
我假設您的表中可能會有更多的條目,而您想比較特定的條目。
您可以遍歷具有其條目集的地圖:
Hashtable<String, Map<String, Set<String>>> table = new Hashtable();
for (Map.Entry<String, Map<String, Set<String>>> entry : table.entrySet()) {
String key = entry.getKey();
Map<String, Set<String>> map = entry.getValue();
for (Map.Entry<String, Set<String>> mapEntry : map.entrySet()) {
String mapKey = mapEntry.getKey();
Set<String> set = mapEntry.getValue();
for (String text : set) {
// ...
}
}
}
雖然在地圖內部的地圖內部嵌套集使代碼難以閱讀,但是您可能想使用專門的對象。
正如其他人所說,與Hashtable相比,大多數情況下HashMap更可取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.