关于CharSequence,字符串等相等问题
2013-05-23 15:10
204 查看
转自:《http://feifei-lee.iteye.com/blog/1163029》
判断两个东东是否相同,习惯性的用“==”,在付出了惨重的代价后才明白:
1,“==”在java中,对比的是对象的内存地址,只有int,short,long等数值型类型可以用。当然,判断是否为null也可以用。(像字符串是对象,就不能用“==”,编译也可以通过,但是结果是错误的)。
2,CharSequence不能直接进行相等的判断,要转换成String类型。通过CharSequence.toString():
3,String类型的判断用equals()方法。 String1.equals(String2);
CharSequence VS String in Java?
转自:《http://stackoverflow.com/questions/1049228/charsequence-vs-string-in-java》
Programming in Android, most of the text values are expected in CharSequence.
Why is that ? What is the benefit and what are the main impacts of using CharSequence over String ?
What are the main differences, and what issues are expected, while using them, and converting from one to another ?
Answer:
1. Strings are CharSequences, so you can just use Strings and not worry. Android is merely trying to be helpful by allowing you to also specify other CharSequence objects, like StringBuffers.
Except when Android passes me a CharSequence in a callback and I need a String - call charSeq.toString(). – Martin Konicek Jul 7 '11 at 11:09
But keep in mind this caveat from the CharSequence javadoc: This interface does not refine the general contracts of the equals and hashCode methods. The result of comparing two objects that implement CharSequence 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. – Trevor Robinson Feb 10 '12 at 23:39
2. In general using an interface allows you to vary the implementation with minimal collateral damage. Although java.lang.String are super popular it may be possible that in certain contexts one may want to use another implementation. By building the API around
CharSequences rather than Strings the code gives one the opportunity to do that.
3. I believe it is best to use CharSequence. The reason is that String implements CharSequence, therefore you can pass a String into a CharSequence, HOWEVER you cannot pass a CharSequence into a String, as CharSequence doesn't not implement String. ALSO, in
Android the EditText.getText() method returns an Editable, which also implements CharSequence and can be passed easily into one, while not easily into a String. CharSequence handles all!
4. This is almost certainly performance reasons. For example, imagine a parser that goes through a 500k ByteBuffer containing strings.
There are 3 approaches to returning the string content: 1. Build a String[] at parse time, one character at a time. This will take a noticeable amount of time. We can use == instead of .equals to compare cached references.
Build a int[] with offsets at parse time, then dynamically build String when a get() happens. Each String will be a new object, so no caching returned values and using ==
Build a CharSequence[] at parse time. Since no new data is stored (other than offsets into the byte buffer), the parsing is much lower that #1. At get time, we don't need to build a String, so get performance is equal to #1 (much better than #2), as we're only
returning a reference to an existing object.
In addition to the processing gains you get using CharSequence, you also reduce the memory footprint by not duplicating data. For example, if you have a buffer containing 3 paragraphs of text, and want to return either all 3 or a single paragraph, you need
4 Strings to represent this. Using CharSequence you only need 1 buffer with the data, and 4 instances of a CharSequence implementation that tracks the start and length.
Phil Lello
判断两个东东是否相同,习惯性的用“==”,在付出了惨重的代价后才明白:
1,“==”在java中,对比的是对象的内存地址,只有int,short,long等数值型类型可以用。当然,判断是否为null也可以用。(像字符串是对象,就不能用“==”,编译也可以通过,但是结果是错误的)。
2,CharSequence不能直接进行相等的判断,要转换成String类型。通过CharSequence.toString():
3,String类型的判断用equals()方法。 String1.equals(String2);
CharSequence VS String in Java?
转自:《http://stackoverflow.com/questions/1049228/charsequence-vs-string-in-java》
Programming in Android, most of the text values are expected in CharSequence.
Why is that ? What is the benefit and what are the main impacts of using CharSequence over String ?
What are the main differences, and what issues are expected, while using them, and converting from one to another ?
Answer:
1. Strings are CharSequences, so you can just use Strings and not worry. Android is merely trying to be helpful by allowing you to also specify other CharSequence objects, like StringBuffers.
Except when Android passes me a CharSequence in a callback and I need a String - call charSeq.toString(). – Martin Konicek Jul 7 '11 at 11:09
But keep in mind this caveat from the CharSequence javadoc: This interface does not refine the general contracts of the equals and hashCode methods. The result of comparing two objects that implement CharSequence 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. – Trevor Robinson Feb 10 '12 at 23:39
2. In general using an interface allows you to vary the implementation with minimal collateral damage. Although java.lang.String are super popular it may be possible that in certain contexts one may want to use another implementation. By building the API around
CharSequences rather than Strings the code gives one the opportunity to do that.
3. I believe it is best to use CharSequence. The reason is that String implements CharSequence, therefore you can pass a String into a CharSequence, HOWEVER you cannot pass a CharSequence into a String, as CharSequence doesn't not implement String. ALSO, in
Android the EditText.getText() method returns an Editable, which also implements CharSequence and can be passed easily into one, while not easily into a String. CharSequence handles all!
4. This is almost certainly performance reasons. For example, imagine a parser that goes through a 500k ByteBuffer containing strings.
There are 3 approaches to returning the string content: 1. Build a String[] at parse time, one character at a time. This will take a noticeable amount of time. We can use == instead of .equals to compare cached references.
Build a int[] with offsets at parse time, then dynamically build String when a get() happens. Each String will be a new object, so no caching returned values and using ==
Build a CharSequence[] at parse time. Since no new data is stored (other than offsets into the byte buffer), the parsing is much lower that #1. At get time, we don't need to build a String, so get performance is equal to #1 (much better than #2), as we're only
returning a reference to an existing object.
In addition to the processing gains you get using CharSequence, you also reduce the memory footprint by not duplicating data. For example, if you have a buffer containing 3 paragraphs of text, and want to return either all 3 or a single paragraph, you need
4 Strings to represent this. Using CharSequence you only need 1 buffer with the data, and 4 instances of a CharSequence implementation that tracks the start and length.
Phil Lello
相关文章推荐
- .net(关于字符串的相等问题[比较重要])
- .net(关于字符串的相等问题[比较重要])
- 关于字符串是否相等的问题?
- 关于XML字符串中出现非法字符导致解析出错的问题
- 4,关于判断两个变量相等的问题
- 关于 字符串的赋值 char *strcpy(char *dest ,char *src) 中越界问题
- 关于字符串指针不可修改的问题
- 关于Java的谣言之字符串+的性能问题
- 关于SpringMVC返回数据带斜杠字符串问题之解决方案
- 关于JSON中字符串与java对象的相互转换问题
- java关于split分割字符串,空的字符串不能得到的问题
- 关于split分割字符串,空结果不能得到的问题
- 关于字符串指针不可修改的问题
- 关于Ajax返回字符串的问题
- 面试题1:字符串相等问题
- 关于数据库连接字符串问题(第五部分连接access-2007)
- cocos2dx中使用的tolua关于字符串处理的一个问题
- 关于字符串的存储和输出问题
- cocos2dx使用tolua关于字符串处理的一个问题
- 关于php查询字符串中带点号的问题