使用番石榴列表中删除重复项 [英] Remove duplicates from List using Guava

查看:127
本文介绍了使用番石榴列表中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何从列表中删除重复的帮助下番石榴API

目前我下面这样:

 私人列表< T> removeDuplicate(列表< T>名单){
返回新的ArrayList< T>(新LinkedHashSet< T>(列表));
}


解决方案

可能是最有效的方法是 ImmutableSet.copyOf(名单).asList(),从而消除重复和preserves的迭代顺序。

(但 LinkedHashSet 的实施将是几乎一样有效,并且不会扔了对空,在你实际上在您的收藏想空万一)。

How can we remove duplicates from List with the help of Guava api?

Currently I am following this:

private List<T> removeDuplicate(List<T> list){
return new ArrayList<T>(new LinkedHashSet<T>(list));
}

解决方案

Probably the most efficient way is ImmutableSet.copyOf(list).asList(), which eliminates duplicates and preserves iteration order.

(But your implementation with LinkedHashSet would be nearly as efficient, and wouldn't throw up on nulls, in the unlikely event you actually wanted nulls in your collection.)

这篇关于使用番石榴列表中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆