aspose file tools*
The moose likes Beginning Java and the fly likes Too many java strings with many replace, concat operations ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Too many java strings with many replace, concat operations ? " Watch "Too many java strings with many replace, concat operations ? " New topic
Author

Too many java strings with many replace, concat operations ?

David Jason
Greenhorn

Joined: Aug 21, 2012
Posts: 27
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

Joined: Oct 31, 2011
Posts: 498

Have you looked at StringBuffer class?


PM Me If You Want to get Urgent Help on Java Programming
Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

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.


OCPJP
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4745
    
    7

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


Junilu - [How to Ask Questions] [How to Answer Questions]
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
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.


Programming is about thinking, NOT coding
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
David Jason,
Your post was moved to a new topic.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
 
 
subject: Too many java strings with many replace, concat operations ?