Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking equality of three or more Strings in Java [duplicate]

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();
}
like image 801
Robert Krupp Avatar asked May 31 '15 17:05

Robert Krupp


People also ask

How to check if two strings are equal in Java?

How to check if two strings are equal in Java? You can check the equality of two Strings in Java using the equals () method. This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

How to check for equality between two strings ignoring the case?

Use equalsIgnoreCase () in Java to check for equality between two strings ignoring the case. Let’s say the following are our two strings. Both are equal, but the case is different. Since the method ignore case, both of these strings would be considered equal.

How do you do an equality comparison in Java?

The second main way of performing an equality comparison in Java is by using the equals () method. How does this differ from the operator? To answer that question, let’s go back to our first example, but replacing the operator with the method. The Person class itself will remain the same, at least for now:

Why can’t we use equals() method in Java?

That’s not quite true, actually. In Java, every class has the Object class as a parent. And Object’s implementation of equals () defaults to ==. In other words: if neither your class nor its ancestors provide a custom implementation of the equals () method, you’ll end-up performing a reference comparison, perhaps inadvertently.


2 Answers

There is no simple way to chain your equals() commands like this. In order to be able to chain them this way, equals() would have to return a reference to the String itself. Then, however, it can't return a boolean value that represents the outcome of the comparison anymore.

Also, I see it as not particularly problematic to compare three strings separately as in your first example. Make sure you keep your code well formatted, and it will remain easy to understand, even for longer variable names:

if(myValueAWithALongName.equals(myValueBWithALongName) &&
   myValueBWithALongName.equals(myValueCWithALongName) &&
   myValueCWithALongName.equals(myValueDWithALongName) &&
   ... ) {

Alternatively, you could use one of the other solutions proposed in this thread and wrap your values into a Collection and write a helper method. Note however that this might have a negative impact on memory usage, performance and possibly readablity.


On a side note, you should never create Strings using the Constructor new String(). Simply assign the string literal directly to your variable:

String a = "a";
like image 122
TimoStaudinger Avatar answered Sep 23 '22 12:09

TimoStaudinger


If you have several objects of the same type, it's a good idea to organize them in the data structure like array or list. So suppose you have a List of String objects:

List<String> strings = Arrays.asList("a", "b", "a", ...);

You want to know whether all the strings in the list equal to each other. This can be done in a number of methods. Probably the shortest one is:

if(new HashSet<>(strings).size() == 1) {
    // all strings are equal
}

Longer, but more optimal solution is proposed by @niceguy. If you are using Java-8, you can also do this way:

if(strings.stream().distinct().count() == 1) {
    // all strings are equal
}
like image 42
Tagir Valeev Avatar answered Sep 24 '22 12:09

Tagir Valeev