开发者

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 Foos, 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜