list复制 迭代
private static <T> List<T> deepCopy(List<T> srcList) {
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(srcList);
out.close();
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
return (List<T>) in.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("2345sdf");
list.add("41abc");
list.add("1");
list.add("1");
//list复制
List<String> list2 = deepCopy(list.stream().filter(l -> !Objects.equals("1", l)).collect(Collectors.toList()));
System.out.println(JSON.toJSONString(list2));
//list迭代
Iterator<String> iteratorList = list.iterator();
while (iteratorList.hasNext()) {
String s = iteratorList.next();
if (s.equals("1")) {
iteratorList.remove();
}
}
System.out.println(JSON.toJSONString(list));
}