Java string Equals () method possible optimization

xiaoxiao2021-03-05  24

JDK1.4, 1.5 String Class code is as follows

[code]

Public Final Class String

Implements java.io.serializable, Comparable , Charsequence

{

/ ** The value is buy for character storage. * /

Private final char value [];

/ ** The offset is the first index of the storage what is buy. * /

PRIVATE FINAL INT OFFSET

/ ** The count is the number of character of character of characters in the string. * /

PRIVATE FINAL INT COUNT

[/ code]

[code]

/ **

* Initializes a newly create string Object SO That IT

* Repesents The Same Sequence of Characters as the argument; in Other

* Words, the newly create string is a copy of the argument string. unless

* An expected is needed, use of this

* Constructor is Unnecessary Since Since Strings Are Immutable.

*

* @Param Original A string .

* /

Public String (string original) {

INT size = Original.count;

CHAR [] OriginalValue = Original.Value;

Char [] V;

IF (OriginalValue.Length> size) {

// the array representing the string is bigger Than the New

// String itself. Perhaps this constructor is being called

// in Order to Trim The Baggage, So Make a Copy of The Array.

v = new char [size];

System.Arraycopy (Original Value, Original.offset, V, 0, Size);

} else {

// the array representing the string is the same

// size as the string, so no point in making a copy.

V = OriginalValue;

}

THIS.OFFSET = 0;

THIS.COUNT = Size;

THIS.VALUE = V;

}

[/ code]

From this constructor, we can see that there may be the same char [] between the String of different Referenceence.

[code]

/ **

* 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 this represents * The Same Sequence of Characters as this object.

*

* @Param Anobject the Object to Compare this string

* Against.

* @return true if the string is equal;

* false Otherwise.

* @see java.lang.string # compareto (java.lang.string)

* @see java.lang.string # equalsignorecase (java.lang.string)

* /

Public Boolean Equals (Object AnObject) {

IF (this == anObject) {

Return True;

}

anObject instanceof string {

String annotherstring = (string) anobject;

INT n = count;

IF (n == annotherstring.count) {

CHAR V1 [] = VALUE

CHAR V2 [] = anotherstring.value;

INT i = OFFSET;

INT j = anotherstring.offset;

While (n--! = 0) {

IF (V1 [i ]! = v2 [j ])

Return False;

}

Return True;

}

}

Return False;

}

[/ code]

However, the Equals method seems to ignore this possible. There is no direct comparison of the Reference of the two CHAR [].

According to my idea, you should join such a paragraph.

[code]

anObject instanceof string {

String annotherstring = (string) anobject;

INT n = count;

IF (n == annotherstring.count) {

CHAR V1 [] = VALUE

CHAR V2 [] = anotherstring.value;

INT i = OFFSET;

INT j = anotherstring.offset;

{{

IF (i == J && v1 == V2) Return True; // Note: This Line is Added by ME

}

While (n--! = 0) {

IF (V1 [i ]! = v2 [j ])

Return False;

}

[/ code]

In this way, it is possible to speed up the comparative speed.

转载请注明原文地址:https://www.9cbs.com/read-35617.html

New Post(0)