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.
You could iterate through all the Foo
s, adding them to a Map<Integer, Collection<Foo>>
where the key is Foo's x
value and the value is a collection of the Foos.
精彩评论