关于CharSequence,字符串等相等问题

来源:互联网 发布:origin数据分析软件 编辑:程序博客网 时间:2024/05/17 08:45

转自:《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


原创粉丝点击