Best way to merge and remove duplicates from multiple lists in Java

前端 未结 3 652
清歌不尽
清歌不尽 2021-02-14 16:00

I have a situation where I will be receiving 2+ ArrayList and I need to be able to merge all the lists and remove any duplicate Widget so

3条回答
  •  半阙折子戏
    2021-02-14 16:11

    Use Set Collection Class,

    ArrayList mergeList = new ArrayList();
    mergeList.addAll(widgets1);
    mergeList.addAll(widgets2);
    Set set  = new HashSet(mergeList);
    ArrayList mergeListWithoutDuplicates = new ArrayList();
    mergeListWithoutDuplicates .addAll(set);
    return mergeListWithoutDuplicates;
    

    Now here Set will remove all duplicates values from your ArrayList.

提交回复
热议问题