dimanche 31 mai 2015

Comparing Equality of Three Strings Java

I know this is a very basic question, but I am always trying to find ways to make my code a clean and concise as possible. Is there a way to compare the equality of three or more strings in a single if() statement? I am currently using the && operator to progressively compare each string. However, as you could imagine, between long variable names and methods being called, these if() statements get very cluttered very quickly. Additionally I am planning to have an unknown number of these Strings, and would like to avoid complex for loops with cluttered if()s nested inside of them. This is what my code currently looks like:

String a = new String("a");
String b = new String("b");
String c = new String("c");
if(a.equals(b) && b.equals(c)) {
    doSomething();
}

Is there a way, or a collection of some sort that I can use that would allow me to compare these values more like this:

if(a.equals(b).equals(c)) {
    doSomething();
}

Aucun commentaire:

Enregistrer un commentaire