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

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

问题描述

如何借助 Guava api 从 List 中删除重复项?

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

目前我正在关注这个:

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

推荐答案

可能最有效的方法是 ImmutableSet.copyOf(list).asList(),它消除重复并保留迭代顺序.

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

(但是您使用 LinkedHashSet 的实现将几乎同样有效,并且不会抛出空值,万一您实际上想要在您的集合中使用空值.)

(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.)

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

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