Java - How to deep copy HashSet, HashMap

Deep copy HashSet

HashSet<Integer> set = new HashSet<Integer>();
HashSet<Integer> set2 = new HashSet<Integer>();
set2.addAll(set);

or

HashSet<Integer> set = new HashSet<Integer>();
HashSet<Integer> set2 = new HashSet<Integer>(set);

Deep copy HashMap

HashMap<Integer, Integer> map = new HashMap<>();
HashMap<Integer, Integer> map2 = new HashMap<>();
map2.putAll(map);

or

HashMap<Integer, Integer> map = new HashMap<>();
HashMap<Integer, Integer> map2 = new HashMap<>(map);

Anyway, Good luck, Richardo! — 09/27/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/6af5f581a848#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞