So I have an ArrayList
here:
story.add("Select the type of bread you want to use. Many prefer the taste of [Color] bread, while others prefer [Noun1] bread because it is healthy.");
story.add("Choose the flavor of Jam/Jelly. I personally prefer [Food] jam, but you can use whatever you want.");
story.add(" Choose the type of peanut butter - either [Adjective1] or [Adjective2].");
story.add("Take out [Number] slice(s) of bread.");
story.add(" Use a [Noun2] to [Verb1] the jam all over on of the pieces of bread.");
story.add(" Now [Verb2] the peanut butter on the other piece of bread.");
story.add("Put them together, and you have a PB&J [Verb3].");
I am trying to replace an individual part of the ArrayList
. For example, I want to replace [Color]
with an actual color without replacing the whole ArrayList
. Is this possible?
Thanks.
Use a ListIterator
, so you can replace the value while iterating:
for (ListIterator<String> iter = story.listIterator(); iter.hasNext(); ) {
String line = iter.next();
line = line.replace("[Color]", "BLUE");
iter.set(line);
}
story.forEach(System.out::println);
Output
Select the type of bread you want to use. Many prefer the taste of BLUE bread, while others prefer [Noun1] bread because it is healthy.
Choose the flavor of Jam/Jelly. I personally prefer [Food] jam, but you can use whatever you want.
Choose the type of peanut butter - either [Adjective1] or [Adjective2].
Take out [Number] slice(s) of bread.
Use a [Noun2] to [Verb1] the jam all over on of the pieces of bread.
Now [Verb2] the peanut butter on the other piece of bread.
Put them together, and you have a PB&J [Verb3].
If you want to replace many of the placeholders in a single iteration, then do it like this (Java 9+):
Map<String, String> map = new HashMap<>();
map.put("Color", "BLUE");
map.put("Adjective1", "SMOOTH");
map.put("Adjective2", "CHUNKY");
map.put("Number", "THREE");
Pattern p = Pattern.compile("\\[([^\\]]+)\\]");
for (ListIterator<String> iter = story.listIterator(); iter.hasNext(); ) {
String line = iter.next();
line = p.matcher(line).replaceAll(mr -> map.getOrDefault(mr.group(1), mr.group(0)));
iter.set(line);
}
Output
Select the type of bread you want to use. Many prefer the taste of BLUE bread, while others prefer [Noun1] bread because it is healthy.
Choose the flavor of Jam/Jelly. I personally prefer [Food] jam, but you can use whatever you want.
Choose the type of peanut butter - either SMOOTH or CHUNKY.
Take out THREE slice(s) of bread.
Use a [Noun2] to [Verb1] the jam all over on of the pieces of bread.
Now [Verb2] the peanut butter on the other piece of bread.
Put them together, and you have a PB&J [Verb3].
I am assuming this is an ArrayList of String
's like so:
ArrayList<String> story = new ArrayList<String>();
story.add("Select the type of bread you want to use. Many prefer the taste of [Color] bread, while others prefer [Noun1] bread because it is healthy.");
...
In this case, you need to replace the CharSequence [Color]
with your new value:
story.get(0).replace("[Color]", "yellow");
If you want to overwrite the original ArrayList with the replaced value you would do...
story.set(0, story.get(0).replace("[Color]", "yellow"));
Well,you don't have to iterate over Array.It can also be done by using stream . You can change any value in the collection using one line of code with .stream() method.
story.stream().map( n ->n.replace("VALUE_YOU_WANT_TO_REPLACE","VALUE_YOU_WANT_TO_BE_REPLACED")).collect(Collectors.toList());
For example if you want to replace "[Color]" with "Blue"
story = (ArrayList<String>) story.stream().
map(n ->n.replace("[Color]","BLUE")).collect(Collectors.toList());
You can do it in one short line...
Given a map of your desired values:
Map<String, String> map = Map.of("[Color]", "green", "[Food]", "fries"); // etc
This does the transform:
map.forEach((k, v) -> story.replaceAll(s -> s.replace(k, v)));
See javadoc for List#replaceAll
Note that this solution does not make use of regex matching.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.