4

在这里,我有两个哈希图datazscreen_dataz. 我想复制screen_datazdataz.

我正在尝试这样但它不起作用:

Object[]  obj = new Object[5];
String[] strArray = new String[]{"Obj1","Array1","Converted1","To1","List1"};
String[] strArray1 = new String[]{"Obj2","Array2","Converted2","To2","List2"};
dataz.put(0,(Object[]) strArray);
dataz.put(1,(Object[]) strArray1);
// String dataString = (String) dataz;
System.out.println(dataz);


Object[]  obj1= new Object[5];
String[] strArray2 = new String[]{"Obj3","Array3","Converted3","To3","List3"};
String[] strArray3 = new String[]{"Obj4","Array4","Converted4","To4","List4"};
screen_dataz.put(0,(Object[]) strArray2);
screen_dataz.put(1,(Object[]) strArray3);
System.out.println("copying screen dataz to dataz");
dataz.putAll(screen_dataz);
4

7 回答 7

17

使用构造函数和 Shallow 它。

dataz = new HashMap<Key,val>(screen_dataz);
于 2013-06-27T07:46:04.483 回答
8

您可以简单地构建一个新的:

dataz = new HashMap<Integer,Object>(screen_dataz);
于 2013-06-27T07:46:14.903 回答
6
Map tmp = new HashMap(patch);
tmp.keySet().removeAll(target.keySet());
target.putAll(tmp);

如需详细说明

于 2013-06-27T07:45:24.110 回答
3

已经在这里发布了

Map tmp = new HashMap(patch);
tmp.keySet().removeAll(target.keySet());
target.putAll(tmp);
于 2013-06-27T07:45:46.583 回答
2

看起来它不起作用,因为您在 indataz和 in 中使用相同的键(0 和 1) screen_dataz

根据官方 javadocputAll“将替换此映射对当前指定映射中的任何键的任何映射。”,因此您现在丢失了包含在dataz.

于 2013-06-27T07:46:19.903 回答
1

尝试这个

    HashMap<Integer,String> myMap=new HashMap<>();
    myMap.put(1,"A");
    myMap.put(2,"B");
    HashMap<Integer,String> newMap=new HashMap<>();
    newMap.putAll(myMap);
于 2013-06-27T07:56:35.183 回答
0
HashMap<String, String> hash1 = new HashMap();
    hash1.put("one", "the firs one");
    hash1.put("two", "the second one");
    hash1.put("three", "the third one");
    HashMap<String, String> hash2 = new HashMap<>();
    hash2.putAll(hash1);
于 2013-06-27T07:55:52.693 回答