Compare strings with peers and compareTo - which is faster?

I need to compare 2 Strings . I have the following methods that I could think of:

  • equalsIgnoreCase - I heard that it is the fastest, but I can not use it, because my string is case sensitive.
  • matches - Probably the slowest
  • equals
  • compareTo

So, in the above option, I stayed with equals and compareTo . Which one is faster among them?

Note: The number of lines in the lines is huge (about 5000 per second).

+6
source share
8 answers

Note the very important difference between compareTo and equals :

 "myString".compareTo(null); //Throws java.lang.NullPointerException "myString".equals(null); //Returns false 

Now I advise you to look at the source code of both methods to conclude that equals preferable to compareTo , which includes some Math .

Also note that equals does == first! This can be a big advantage when the objects are identical. Specially, when you mentioned that you have a huge number of strings, because Java interns Strings, this can happen more than you thought.


Although you asked about Strings, but I want to add this note:
These methods can be very different if BigDecimal involved. For example, see docs :

equals compares this BigDecimal with the specified object for equality. Unlike compareTo , this method considers two BigDecimal objects to be equal only if they are equal in value and scale (so 2.0 is not 2.00 when compared with this method).

+12
source

I stayed with equals and compared.

Both serve for different purposes.

 CompareTo 

value 0 if the argument string is equal to this string; value less than 0 if this string is lexicographically less than the string argument; and the value is greater than 0 if this string is lexicographically larger than the string argument.

Where

 equals 

true if this object represents a string equivalent to this string, false otherwise

Consequently, compareTo() needs more calculations so that equals()

You can see the proof of this in the source code of both.

compareTo ()

equals () ----- prefer to use this.

+6
source

equalsIgnoreCase

Usually you use this method to compare strings.

coincidences

Slow because it is based on RegEx.

equally

I don't think this is slower than equalsIgnoreCase

CompareTo

Returns an integer or 0. This is used, for example, when sorting. Do not use it for equality.

+1
source

Two main differences are as follows:

 equals will take any Object as a parameter, but compareTo will only take Strings. equals only tells you whether they're equal or not, but compareTo gives information on how the Strings compare lexicographically. 

ANd is the url class code http://www.docjar.com/html/api/java/lang/String.java.html . U can also see this post if you want Java Strings: compareTo () vs. equals ()

+1
source

Equal values ​​-

1- Override the GetHashCode method to allow the type to work correctly in the hash table.

2 Do not throw an exception in the implementation of the Equals method. Instead, return false for the null argument.

3 -

  x.Equals(x) returns true. x.Equals(y) returns the same value as y.Equals(x). (x.Equals(y) && y.Equals(z)) returns true if and only if x.Equals(z) returns true. 

Successive calls to x.Equals (y) return the same value if the object referenced by x and y does not change.

  x.Equals(null) returns false. 

4- For some types of objects, it is desirable to have an Equals test for equality of values ​​instead of reference equality. Such Equals implementations return true if both objects have the same value, even if they are not the same instance.

Example -

  Object obj1 = new Object(); Object obj2 = new Object(); Console.WriteLine(obj1.Equals(obj2)); obj1 = obj2; Console.WriteLine(obj1.Equals(obj2)); 

Output: -

  False True 

and compareTo is

Compares the current instance with another object of the same type and returns an integer indicating whether the current instance precedes, follows or occurs at the same position in the sort order as the other object.

He returns -

Less than zero - this instance precedes obj in sort order. Zero - this instance is found in the same position in the sort order as obj. Greater than zero - this instance follows obj in sort order.

It can raise an ArgumentException if the object is not the same type as the instance.

For example, you can visit here .

Therefore, I suggest it is better to use Equals instead of compareTo.

+1
source

You can check the code yourself once and find out which is faster:

 final long start = System.currentTimeMillis(); for (int i = 0; i < input.length; i++) { // Do comparison here } final long end = System.currentTimeMillis(); System.out.println("Time to execute: " + (end - start)); 
0
source

 s1.CompareTo(s2) 

0 - both are equal

+ ve - than s2 is lexicographically lower than s1

-ve - than s2 is lexicographically higher than s1

 s1.equals(Object o1) 

true - if s1 and o1 are equal

false- if s1 and o1 are not equal

compareTo may throw a ClassCastException , but equals , if s1 and o1 are different classes, than should return false.


TreeMap depends on compareTo() , and HashMap depends on equals() . Therefore, if you are equal and compareTo do not match, than TreeMap and HashMap can behave differently. Therefore, it is good to have equals () and compareTo () consistent with each other, but this is not necessary.


I think the best or most effective way to compare strings is to compare links (but you should have used literals in the whole code). Since the string is essentially immutable and the String class implements the FlyWeight template internally with the intern() method or the compiler. Therefore, it is useful to compare the link.

If you are not sure about the literals in the code, how can you do the optimization:

 s1.hashCode() == s2.hashCode() && s1.equals(s2) 

This is because String is immutable.

0
source

equalsIgnoreCase - Heard it is the fastest

Definitely not. He must do something to advance lowercase letters to uppercase or vice versa. You cannot β€œhear” that somewhere dear.

matches - possibly the slowest

I agree.

equal CompareTo

There is no reason to expect a difference between the two.

0
source

Source: https://habr.com/ru/post/956384/


All Articles