简体   繁体   中英

How to find non unique elements in an array of class objects in Java

I'm not sure how to say what I need so here's an example:

class Foo{
int x;
int y;
int z;
}
ArrayList<Foo> a;

The array has objects <a,b,c>,<a,c,d>,<b,c,e>,<b,e,f>,<c,e,f>,<g,h,i> etc

How can I retrieve all objects that have a common x value. In the above example I would like to retrieve <a,b,c>,<a,c,d> and <b,c,e>,<b,e,f> only.

I'm looking for an efficient way to do it.

您可以遍历所有Foo ,将它们添加到Map<Integer, Collection<Foo>> ,其中键是Foo的x值,而值是Foos的集合。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM