Add method .equalsOr() to String class

Manuel :

I'm just starting with Java.

I wonder if there's a way to add a method so that any String can call that method, similar to how we have now string1.equals(string2) that you could use string1.equalsOr(string2, string3, string4, string5).

I know I can make my own static method, but wonder if I could make it available to any string.

dehasi :

If you want to check equality of your string1 to a set of strings string2, string3, string4, string5. It's better to use any collection (i.e. Set or List). For example, if you use

 Arrays.asList(string2, string3, string4, string5).contains(string1)

it will give you the espected result.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=72066&siteId=1