Groovy Goodness: Removing Duplicate Elements in a CollectionMessages from mrhaki

Groovy Goodness: Removing Duplicate Elements in a CollectionMessages from mrhaki,第1张

Groovy Goodness: Removing Duplicate Elements in a Collection - Messages from mrhaki

Groovy Goodness: Removing Duplicate Elements in a Collection

We can easily remove duplicate elements in a collection with the different unique() methods. Without any parameters the default comparator for the objects is used to determine duplicate elements. But we can also use a closure to define the rules for duplicate elements. The closure can have one parameter and then we must return a value that is used for comparison, or the closure has two parameters and then we must return an Integer value where 0 means the items are not unique. Finally we can pass our own Comparator implementation to the unique() method where we define when an element is unique or not.

view sourceprint?

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/2085386.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-07-22
下一篇 2022-07-22

发表评论

登录后才能评论

评论列表(0条)

保存