1

I have two objects of two different types :

Class1 A
List<Class2> B

And we have -

Class2 C = new Class2();
B.add(C);

I want to get elements of B such as A.fieldX == C.fieldY.

Is it possible to do so without iterating on the list ?

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
merours
  • 4,076
  • 7
  • 37
  • 69
  • I dont think so. The only way to really get around to iteration is to have a mapping, which would mean changing your underlying data structure. Don't know if you want/can do that. – David says Reinstate Monica Jun 17 '13 at 16:03
  • Data structure is not fixed, but I don't see how a mapping would change the problem. Could you be more specific ? – merours Jun 17 '13 at 16:07
  • Ill put it in an answer, one min. – David says Reinstate Monica Jun 17 '13 at 16:09
  • 1
    If the key field remains the same (that is, the value of fieldY is always used to cull objects from the list) then use a `Map` (where FieldY is the type of fieldY) so you can simply extract from the map all Class2 objects which have the desired fieldY value. – Bobulous Jun 17 '13 at 16:10
  • 1
    You might want to take a look at this question: http://stackoverflow.com/questions/122105/java-what-is-the-best-way-to-filter-a-collection – Alan Jun 17 '13 at 16:15
  • So why do you not want to iterate over the list, anyway? – trutheality Jun 17 '13 at 16:30

2 Answers2

1

It is not possible to do this in a list without some form of iteration, but if you change to a mapping you can get around that with the following code:

Map<FieldYType, Class2> B= new HashMap();
B.put(c.fieldY, C);
Class2 D = B.get(A.fieldX);
D==C;//true
D.fieldY==A.fieldX;//true

So here you dont iterate at all, you just use a single get function. You also might want to use a different map type, but thats up to you and your code design.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
  • @anubhava Uniqueness seemed to be the assumption based on the OPs question, but if it is not unique you could just have Map>; Then when you do B.get() you get a list back and can use something else as the unique identifier. – David says Reinstate Monica Jun 17 '13 at 16:16
  • If you get a List back from map then wouldn't you be in same situation as what OP is trying to avoid i.e. iterating the list. – anubhava Jun 17 '13 at 16:18
  • @anubhava At the very least you would likely have a significantly small amount of stuff to iterate by, which would help the OP a lot. But if that really concerns you you can just put another map. So you would have Map>; – David says Reinstate Monica Jun 17 '13 at 16:20
1

If the fields don't change for the objects while they're in your list, you can use a map of collections (or lists or sets, depending on what you need) to facilitate this comparison:

Map<Field, List<Class2>> map = new HashMap<>();

To insert:

Class2 c = new Class2();

List<Class2> bucket = map.get(c.fieldY);
if( null == bucket ){
    bucket = new ArrayList<>();
    map.put( c.fieldY, bucket );
}
bucket.add( c );

To look up:

List<Class2> result = map.get( a.fieldX );

This only works if the fields don't change while the objects are in the map of lists, and it only makes sense to do if you are making these lookups a lot.

trutheality
  • 23,114
  • 6
  • 54
  • 68