List集合addAll的总结
1. 代码
public static void main(String[] args) {
List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
Map<String, Object> contractMap = new HashMap<String, Object>();
contractMap.put("CONTRACTID", "1");
contractMap.put("CONTRACTCODE", 2);
list1.add(contractMap);
List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
Map<String, Object> contractMap2 = new HashMap<String, Object>();
contractMap2.put("CONTRACTID", "1");
contractMap2.put("CONTRACTCODE", 2);
Map<String, Object> contractMap3 = new HashMap<String, Object>();
contractMap3.put("CONTRACTID", "1");
contractMap3.put("CONTRACTCODE", 3);
list2.add(contractMap2);
list2.add(contractMap3);
// 先添加list2
list1.addAll(list2);
// 更新list2的第一个元素
list2.get(1).put("CONTRACTCODE", 5);
list2.remove(0);
// 打印list1的第二个元素
System.out.println(list1.get(1).get("CONTRACTCODE"));
System.out.println(list1.get(2).get("CONTRACTCODE"));
System.out.println(list2.get(0).get("CONTRACTCODE"));
System.out.println(list1.size());
//list1.removeAll(list2);
// list2.removeAll(list1);
System.out.println(list1.size());
System.out.println(list2.size());
if (contractMap.equals(contractMap2)) {
System.out.println("Y");
}else{
System.out.println("N");
}
/* final int len = 13;
System.out.println((len & 0xFF));
if ((len & 0x01) != 0) {
}
try {
InputStream is = new FileInputStream(new File("D:\\5w.xlsx"));
byte[] bytes = readBytes(is);
System.out.println(bytes.length);
} catch (Exception e) {
e.printStackTrace();
}*/
List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
Map<String, Object> contractMap = new HashMap<String, Object>();
contractMap.put("CONTRACTID", "1");
contractMap.put("CONTRACTCODE", 2);
list1.add(contractMap);
List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
Map<String, Object> contractMap2 = new HashMap<String, Object>();
contractMap2.put("CONTRACTID", "1");
contractMap2.put("CONTRACTCODE", 2);
Map<String, Object> contractMap3 = new HashMap<String, Object>();
contractMap3.put("CONTRACTID", "1");
contractMap3.put("CONTRACTCODE", 3);
list2.add(contractMap2);
list2.add(contractMap3);
// 先添加list2
list1.addAll(list2);
// 更新list2的第一个元素
list2.get(1).put("CONTRACTCODE", 5);
list2.remove(0);
// 打印list1的第二个元素
System.out.println(list1.get(1).get("CONTRACTCODE"));
System.out.println(list1.get(2).get("CONTRACTCODE"));
System.out.println(list2.get(0).get("CONTRACTCODE"));
System.out.println(list1.size());
//list1.removeAll(list2);
// list2.removeAll(list1);
System.out.println(list1.size());
System.out.println(list2.size());
if (contractMap.equals(contractMap2)) {
System.out.println("Y");
}else{
System.out.println("N");
}
/* final int len = 13;
System.out.println((len & 0xFF));
if ((len & 0x01) != 0) {
}
try {
InputStream is = new FileInputStream(new File("D:\\5w.xlsx"));
byte[] bytes = readBytes(is);
System.out.println(bytes.length);
} catch (Exception e) {
e.printStackTrace();
}*/
}
" com.intellij.rt.execution.application.AppMain HexTest
2
5
5
3
3
1
Y
5
5
3
3
1
Y
2. 总结
1. List1的addAlll(List2) 修改List2里的对象元素List1会跟着改变(说明List2的中的元素添加到List1是使用的对象的地址传递,修改2元素1的元素跟着修改)
2.对集合的操作(remove) 只是正对集合,比如List1添加2的元素,List2呢remove一个元素对List1来说List1仍然有List2移除的元素
3. removeAll使用的contains而contains使用的是equals比较的是值