• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Too many java strings with many replace, concat operations ?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/

 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you looked at StringBuffer class?
 
Ranch Hand
Posts: 91
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17316
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 179
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David Jason,
Your post was moved to a new topic.
 
Campbell Ritchie
Marshal
Posts: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic