• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best and efficient way way in term of performance to replace a specific string by a new one  RSS feed

 
Jade Layyne
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I am trying to find the most efficient algorithm in term of performance allowing to replace a specific string by another one. I a string "AA" is located at the index 3, I have to put my new value "AAWWW" at this index 3.
Know that my stringList is already ordered(I have to maintain it).
The problem of this code is that, I realize I still loop my list even if my oldValue is not in there.
I was thinking of adding an if test (if (stringList.contains(oldValue)) before executing my for loop but I think the call of .contains invoke list iteration )
Thank you
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to have arrived in the wrong forum; this isn't a Java8‑specific question. You shouldn't try to write code tags by hand; use the button. I shall correct both those things for you.
Start by finding the RandomAccess interface and reading it. Similarly read the methods in the List interface, too. From those two links you shou‍ld be able to find the best way to locate an element in a List. You will also know how many replace methods a List has already.
I would make some suggestions:-
  • 1: Are you replacing every instance of oldValue in the List, or only one occurrence? If only one, you want to exit the loop when you have replaced it.
  • 2: If you are passed null, don't simply return. Throw an Exception.
  • 3: There is no need to check the corner case of an empty List; the loop will never run, so nothing will happen.
  • 4: Make the method generic, so you can replace any types in Lists.
  • 5: Give the method a boolean return type: return true if you replaced anything, otherwise false.
  • This is what your method will look like changed to a generic method:-I have shortened your names, but not changed the logic.
     
    Campbell Ritchie
    Marshal
    Posts: 56534
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jade Layyne wrote:. . . I a string "AA" is located at the index 3, I have to put my new value "AAWWW" at this index 3.
    Do you only know the index for that one String?

    Know that my stringList is already ordered(I have to maintain it). . . .
    By ordered, do you mean sorted? That is a different problem from replacing elements in a List.
     
    Jade Layyne
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, I mean, sorted
     
    Campbell Ritchie
    Marshal
    Posts: 56534
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So, how do you know that "AA" is at index 3? Have you any idea how to find the index of "AA" in your List? Do you know ways to do it which work on any List, or ways which only work on sorted Lists? What will happen if you replace "AA" by "AAWWW"? What if index 4 points to "AAAAA"? Will that upset the sorting of your Lists? Does that matter?
     
    Carey Brown
    Saloon Keeper
    Posts: 3311
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:So, how do you know that "AA" is at index 3? Have you any idea how to find the index of "AA" in your List? Do you know ways to do it which work on any List, or ways which only work on sorted Lists? What will happen if you replace "AA" by "AAWWW"? What if index 4 points to "AAAAA"? Will that upset the sorting of your Lists? Does that matter?

    When you say "put", do you mean insert or do you mean replace?

    If you have "@@@AABBCC" where index of "AA" is 3 and you insert "AAWWW" you get "@@@AAWWWAABBCC" which is no longer sorted.
    If you have "@@@AABBCC" where index of "AA" is 3 and you replace "AA" with "AAWWW" you get "@@@AAWWWBBCC" which is no longer sorted.

    If you "@@@ABC...VWXYZ" where index of "A" is 3 and you insert "AA" you get "@@@AAABC...VWXYZ", and then at index N where "W" is located you insert "WWW" you get "@@@AAABC...VWWWWXYZ" which is sorted.

    Your requirements need to be much clearer.
     
    Jade Layyne
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello everyone, yes you are right. What I mean with put is replace. You are totally right, the order of the element will not be maintained anymore but it is ok.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!