Java中List高效去重

版权声明:本文为博主原创文章,转载请注明【转载自皓月如我的CSDN博客】,并贴出原始链接地址。 https://blog.csdn.net/fm0517/article/details/82049605

直接上代码:

private static void DuplicateRemoval(List<Integer> ioList)
{
    LinkedHashSet<Integer> tmpSet = new LinkedHashSet<Integer>(ioList.size());
    tmpSet.addAll(ioList);
    ioList.clear();
    ioList.addAll(tmpSet);
}

原理是利用了LinkedHashSet不能添加重复的数据。
当两个List需要去重合并的时候,可以类似的先addList,再DuplicateRemoval。
这种方法比使用List的contains效率高几十倍。

猜你喜欢

转载自blog.csdn.net/fm0517/article/details/82049605
今日推荐