I have about 10,000+ strings. There are also many replace and concatenation operations on each string.
I want my code to use minimum memory. StringBuilder, does not have a convenient method like replace
of String Class.
I am looking for something like String class which has the characteristics mentioned below.
a) It is modifiable or mutable and uses minimum memory.
b) Has easy methods like concat, repace etc.
c) It is reliable and maybe even popular.
StringBuffer is thread safe. So if you are looking performace then use stringBuilder because it is usually better performance wise.
Also, I think the Methods are same in both StringBuffer and StringBuilder.
I have no idea about ropes in java and have never used them.
It doesn't look like the Ropes class fits your requirement for being mutable: "Like Strings, ropes are immutable and therefore well-suited for use in multi-threaded programming."
Also, what are your reasons for being concerned about memory usage? Is there a specific requirement or constraint in the deployment environment or are you going by programmer's intuition? Programmer's intuition is not a very reliable tool for performance tuning; it's best to use a profiler to identify performance bottlenecks.
"Premature optimization is the root of all evil." -- Donald Knuth
I did not get it, why do you want to work with 10,000+ String all at a time. If they are not going to be used all at once, try using a few ,work with them make the objects null, notify gc() , then take another bunch.
Still if you want to work with a huge bunch of String object, I would suggest you to use either StringBuffer / StringBuilder. For concat, you can use append() while for replace you can follow the given steps
1> convert the object to String using toString() ,
2> perform replace() on the converted String
3> reinitialize the Builder/Buffer object with the replaced String.
4> Make the String object/s null and notify gc().
David Jason wrote:I have about 10,000+ strings. There are also many replace and concatenation operations on each string.
I want my code to use minimum memory. StringBuilder, does not have a convenient method like replace of String Class.
I think Vishal covered most of it, but my first question would be:
Why does your code need to use minimum memory?
And it's true that StringBuilder doesn't have a replace(String, String) method (which I'm assuming is the one you want), but it does have indexOf(String, fromIndex) and replace(start, end, String) methods, which should be more than enough for you to roll your own "Stringlike replace() method"; although you might sacrifice some speed compared to String's.