有没有办法在多个列表中找到公共元素? [英] Is there a way to find common elements in multiple lists?

查看:33
本文介绍了有没有办法在多个列表中找到公共元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数数组列表.我需要找到它们之间的共同点.我能想到的是两个列表中的公共元素

I have a list of integer arrays. I need to find the common elements between those. What I can think of is an extension of what is listed in Common elements in two lists

Example would be 
[1,3,5],
[1,6,7,9,3],
[1,3,10,11]

should result in [1,3]

数组中也没有重复项.

有没有直接的方法来做到这一点?

Is there a straight forward way to do this?

推荐答案

您可以将列表转换为集合,然后使用 Set.retainAll 方法进行不同集合之间的交集.将所有集合相交后,剩下的就是公共元素,您可以将结果集转换回列表.

You can transform the lists to sets, and then use Set.retainAll method for intersection between the different sets. Once you intersect all sets, you are left with the common elements, and you can transform the resulting set back to a list.

这篇关于有没有办法在多个列表中找到公共元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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