Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Too many java strings with many replace, concat operations ?

 
David Jason
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I see that there is a Ropes class. I don't know if that is good.
http://ahmadsoft.org/ropes/

 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at StringBuffer class?
 
Komal Arora
Ranch Hand
Posts: 91
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Bartender
Pie
Posts: 7319
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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().

Now you have to choose.

Vishal.
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Jason,
Your post was moved to a new topic.
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The forum software, on my behalf wrote:David Jason,
Your post was moved to a new topic.
The last question seemed more suitable for our databases forum, so I moved it as a new topic.
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I see that there is a Ropes class. I don't know if that is good.
http://ahmadsoft.org/ropes/

I may be putting the poor chap down, but I'm not sure how far I'd trust a class from a site called 'ahmadsoft.org' without a LOT of testing.

Winston
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:I may be putting the poor chap down, but I'm not sure how far I'd trust a class from a site called 'ahmadsoft.org' without a LOT of testing.

Update: It would appear I am. Just found his paper about Ropes. Interesting.

I now have a dim recollection of having heard something about them before, but it slipped my mind.

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic