Why use charsequence




















This interface does not refine the general contracts of the equals and hashCode methods. The result of testing two objects that implement CharSequence for equality is therefore, in general, undefined.

Each object may be implemented by a different class, and there is no guarantee that each class will be capable of testing its instances for equality with those of the other. It is therefore inappropriate to use arbitrary CharSequence instances as elements in a set or as keys in a map. Module java. Package java. This interface provides uniform, read-only access to many different kinds of char sequences. Refer to Unicode Character Representation for details. Returns a stream of int zero-extending the char values from this sequence.

Returns a string containing the characters in this sequence in the same order as this sequence. Method Detail length int length Returns the length of this character sequence. You generally won't be converting back and forth. A String is a CharSequence.

If your method declares that it takes a CharSequence , the calling programmer may pass a String object, or may pass something else such as a StringBuffer or StringBuilder. Your method's code will simply use whatever is passed, calling any of the CharSequence methods.

The closest you would get to converting is if your code receives a CharSequence and you know you need a String. Perhaps your are interfacing with old code written to String class rather than written to the CharSequence interface. Or perhaps your code will work intensively with the text, such as looping repeatedly or otherwise analyzing.

In that case, you want to take any possible performance hit only once, so you call toString up front. Then proceed with your work using what you know to be a single piece of text entirely in memory.

Note the comments made on the accepted Answer. Ideally CharSequence would have been in place from the beginning, but such is life. I'm not sure if all of these are present in Android, but the overall context may still prove useful to you. I believe it is best to use CharSequence.

CharSequence handles all! Python Javascript Linux Cheat sheet Contact. Methods inherited from class java. Object clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait Method Detail create public static CharSequence create char[] buf, int start, int count Creates new CharSequence instance representing the chars in the array.

The sequence contains its own copy of the array content represented in an efficient e. Parameters: buf - buffer to copy the characters from start - starting offset in the buf array count - number of characters to copy Returns: immutable char sequence create public static CharSequence create CharSequence s Creates new CharSequence instance representing the content of another sequence or String efficiently.

Returns: char sequence to represent empty sequence isCompact public static boolean isCompact CharSequence cs Predicate to check if provides char sequence is based on compact implementation. Parameters: cs - char sequence object to check Returns: true if compact implementation, false otherwise indexOf public static int indexOf CharSequence text, CharSequence seq Implementation of String.



0コメント

  • 1000 / 1000