Implement a simaple version of matching (does not follow one-one constraint) in Java