Monday, 9 February 2009

Back to Basics: The java.util.List, java.lang.StringBuffer and java.lang.StringBuilder

java.util.List is an interface that extends Collection. Lists allow duplicates, unlike sets.

Fundamental methods:
add(Object)
add(index, Object)
get(index)

index goes from 0 to size-1. List also implements indexOf, which gives the index of the first occurence of some object.

java.lang.StringBuffer is a mutable sequence of characters. The binary string concatenation operator + is implemented with StringBuffers by the compiler. Key methods on SBuffers are append, insert, reverse (which returns a new StringBuffer) and toString. The thing about StringBuffer is you can't do stuff like sb+="some string", you NEED to use sb.append!

As of Java 1.5, StringBuffer implements the Appendable interface, designating an object to which char sequences and values can be appended. StringBuffer is closely related to the (java.util.) Formatter interface.

StringBuilder is a drop-in replacement for StringBuffer when no synchronisation is required.

No comments:

Blog Archive