简体   繁体   English

将两个通配符哈希图合并为一个通配符哈希图

[英]Combine two wild card hashmaps into a wild card hashmap

I want to combine two hash maps with wild cards into a single hash map which still contains dynamic values. 我想将两个带有通配符的哈希图组合成一个仍包含动态值的哈希图。

I have SharedPreferences in two different places in my Android application - I need to combine the entire set into a single hash map to pass to a function. 我的Android应用程序中的两个不同位置都有SharedPreferences-我需要将整个集合组合到单个哈希映射中才能传递给函数。

I have tried putAll() and merge functions, but the wild cards keep causing issues. 我尝试了putAll()和merge函数,但是通配符不断引起问题。

Currently I have two functions which return the values, but I really need to combine them into a single function. 当前,我有两个返回值的函数,但是我确实需要将它们组合成一个函数。

private HashMap<String, ?> getAppPrefs() {
   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
   return (HashMap<String, ?>) prefs.getAll();
}

private HashMap<String, ?> getScorePrefs() {
   SharedPreferences scoreprefs= this.getSharedPreferences("scoreprefs", 0);
   return (HashMap<String, ?>) scoreprefs.getAll();
}

My expected result is something like: 我的预期结果是:

private HashMap<String, ?> getAllPrefs() {
   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
   HashMap<String, ?> map1 = (HashMap<String, ?>) prefs.getAll();   
   SharedPreferences scoreprefs= this.getSharedPreferences("scoreprefs", 0);
   HashMap<String, ?> map2 = (HashMap<String, ?>) prefs.getAll();
   HashMap<String, ?> map3 = map1.merge(map2);  // <-- Or something else
   return map3;
}

If I use merge, I get: 如果使用合并,则会得到:

error: no suitable method found for merge(HashMap<String,CAP#1>)
  HashMap<String, ?> map3 = map1.merge(map2);
                                ^
method Map.merge(String,CAP#2,BiFunction<? super CAP#2,? super CAP#2,? extends CAP#2>) is not applicable
  (actual and formal argument lists differ in length)
method HashMap.merge(String,CAP#2,BiFunction<? super CAP#2,? super CAP#2,? extends CAP#2>) is not applicable
  (actual and formal argument lists differ in length)
  where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends Object from capture of ?
CAP#2 extends Object from capture of ?

With the unknown wildcard ( <?> ), the only assumption that can safely be made is that instances are a subtype of Object. 使用未知的通配符( <?> ),可以安全地做出的唯一假设是实例是Object的子类型。

We cannot take objects of an unknown type and put them in another collection of another unknown type because the two types might be incompatible. 我们不能将未知类型的对象放入另一个未知类型的另一个集合中,因为这两种类型可能不兼容。 However, we can put them in a collection of <Object> as this is always safe, and then declare that this collection is of a third unknown type. 但是,我们可以将它们放在<Object>的集合中,因为这总是很安全的,然后声明该集合是第三个未知类型。

For instance, with the type of your example: 例如,使用您的示例类型:

public static HashMap<String, ?> merge(HashMap<String ,?> map1, HashMap<String, ?> map2) {
    HashMap<String, Object> merged = new HashMap<>();
    merged.putAll(map1);
    merged.putAll(map2);
    return merged;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM