I have two collections, that have almost similar attributes:
HashSet<BuyerUser>
HashSet<SellerUser>
I want to write a method that serializes the objects as JSON and sends it to a web API. My problem is, however, that i'm not able to write a method that is generic enough so that I don't have to repeat the code twice.
public void addToMetadata (Object users) {
if (users instanceof BuyerUser) {
// Do this
}
if (users instanceof SellerUser) {
// Do that
}
}
My problem is the instanceOf check that doesn't work the way I outlined it. I would have to do something like
if (users instanceof HashSet<BuyerUser>)
but that gives me an error:
llegal generic type for instanceof
Can that be solved in any way?