本文介绍了在ArrayList上与对象groovy相交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组列表,它们的对象都是域类'User'

I have two array lists with objects of a domain class 'User'

a = [User : 35, User : 36, User : 37]
b = [User : 32, User : 33, User : 34, User : 35, User : 36, User : 37]

当我执行 intersect ( java.util )时,它将返回一个空列表.如何在数组列表中找到常用对象.

When I perform intersect (java.util), it returns an empty list. How can I find the common objects in array list.

推荐答案

这是因为您将地图(具有相同的键)保留在列表中.

It's because of the fact that You keep maps (with same key) on the list.

尝试:

def a = [User1 : 35, User2 : 36, User3 : 37]
def b = [User4 : 32, User5 : 33, User6 : 34, User1 : 35, User2 : 36, User3 : 37]

assert a.intersect(b) == [User1:35, User2:36, User3:37]

当您将其修改为仅包含ID时,其行为将与预期的一样:

When You modify it to contain IDs only it behaves as expected:

def a = [35, 36, 37]
def b = [32, 33, 34, 35, 36, 37]

assert a.intersect(b) == [35,36,37]

看到了吗?

这篇关于在ArrayList上与对象groovy相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 16:31