2

I am using LambdaJ and hamcrest in Java. I am wondering if there is a way to check if any item in one array is equal to any item of another array?

I have tried some configurations but I can not seem to get it right.

like this one:

arrayOne, hasItemInArray(isIn(arrayTwo));

... Does not work because it will try to match every item in arrayOne.

Jonas.z
  • 415
  • 4
  • 18
  • Is it at the same index? If yes, just test that the lists are `.equals()` – fge Jul 01 '13 at 15:03
  • Thanks, but not really what I was looking for. This matching should assert that arrayOne contains at least one item equal to an item in arrayTwo regardless of index. – Jonas.z Jul 02 '13 at 06:07
  • Then just do `boolean found = false; for (final X item: arrayOne) if (arrayTwo.contains(item)) found = true; assertTrue(found);`. LambdaJ and all this are fine, but good "old fashioned" loops always work ;) – fge Jul 02 '13 at 07:33
  • Yes, that would work. However I realise I forgot to mention that this matcher is just one in a series of optional matchers in a multi-filter. These matchers will be combined using `.and()` to filter through a List. So good old loops will not work. Are there no explicit matchers for this? Seems like an easy enough task for Hamcrest and LambdaJ – Jonas.z Jul 02 '13 at 08:30
  • Well, you can create your own matcher if you use fest-assert. I don't know much about Hamcrest, but I guess you can create your own matchers as well – fge Jul 02 '13 at 08:33
  • Yes, that would be an option. I think I solved it though, will accept as soon as I'm allowed. Thank you for the support :) – Jonas.z Jul 02 '13 at 13:09

1 Answers1

3

I think I solved it!

The problem was not that Hamcrest does not match the way I described in the question. It was that I had a List of Long:s.

I was refering to hasItemInArray which uses hasValue (comparing primitive types) rather than hasItem (comparing objects).

So the code above works with a minor modification:

arrayOne, hasItem(isIn(arrayTwo));

This is acually a somewhat confusing naming convention.

Jonas.z
  • 415
  • 4
  • 18