jeudi 16 janvier 2020

Java Streams - How can I use Java streams to check and replace string if there is a match?

I have if condition where I am checking for values, if they match I store them in Set. After storing them in Set I am looping through Set to check if a particular string is present. If it is present I replace that particular string with value I want to pass. For example if there are 3 strings a,b and c I am looking for if they are one or all present I will store them in set. Then if string c is present I want to replace it with d. As of now I am using if and for loops. I am looking for something with use of streams for this functionality.

Here is my code


Set<String> s = new HashSet<>;
while(itr.hasNext()){
Value v = itr.next();

If(value == "a" ||value == "b" ||value == "c"){
s.add(value);

For(String vname : s){
If(value == "c"){
vname = "d";
s.add(vname);
s.remove("c");
}
}
}
}

Aucun commentaire:

Enregistrer un commentaire