Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace String With hashmap value using java 8 stream

I have String and HashMap like below codes:

Map<String, String> map = new HashMap<>();
    map.put("ABC", "123");
    String test = "helloABC";
    map.forEach((key, value) -> {
        test = test.replaceAll(key, value);
    });

and I try to replace the string with the HashMap values, but this doesn't work because test is final and cannot be reassigned in the body of forEach.

So are there any solutions to replace String with HashMap using Java 8 Stream API?

like image 960
Chhea Vanrin Avatar asked Mar 23 '16 07:03

Chhea Vanrin


People also ask

Can we use map stream in Java 8?

Java 8 Stream's map method is intermediate operation and consumes single element forom input Stream and produces single element to output Stream. It simply used to convert Stream of one type to another. Let's see method signature of Stream's map method.

How do I change the value of a key in a HashMap?

The merge Method. The merge method updates the value of a key in the HashMap using the BiFunction if there is such a key. Otherwise, it will add a new (key, value) pair, with the value set to the value provided as the second argument to the method.

What is stream () method in Java?

A stream consists of source followed by zero or more intermediate methods combined together (pipelined) and a terminal method to process the objects obtained from the source as per the methods described. Stream is used to compute elements as per the pipelined methods without altering the original value of the object.

Can we use stream with map in Java?

Converting only the Value of the Map<Key, Value> into Stream: This can be done with the help of Map. values() method which returns a Set view of the values contained in this map. In Java 8, this returned set can be easily converted into a Stream of key-value pairs using Set. stream() method.


2 Answers

As this cannot be made using only forEach() (message must be effectively final), workaround could be to create a final container (e. g. List) which stores a single String that is re-written:

final List<String> msg = Arrays.asList("helloABC");
map.forEach((key, value) -> msg.set(0, msg.get(0).replace(key, value)));
String test = msg.get(0);

Note that I changed replaceAll() to replace() because former works with regex, but judging by your code seems you need replacement by string itself (don't worry, despite of confusing name it also replaces all occurrences).

If you want exactly Stream API, you may use reduce() operation:

String test = map.entrySet()
                 .stream()
                 .reduce("helloABC", 
                         (s, e) -> s.replace(e.getKey(), e.getValue()), 
                         (s1, s2) -> null);

But take into account, that such reduction will work properly only in serial (not parallel) stream, where combiner function is never called (thus may be any).

like image 198
Alex Salauyou Avatar answered Oct 22 '22 20:10

Alex Salauyou


This sort of a problem is not well-suited for Streams API. The current incarnation of Streams is mainly directed at tasks that can be made parallel. Perhaps support for this type of operation will be added in the future (see https://bugs.openjdk.java.net/browse/JDK-8133680).

One streams-based approach that you might find interesting is to reduce on functions rather than on the string:

Function<String, String> combined = map.entrySet().stream()
    .reduce(
        Function.identity(),
        (f, e) -> s -> f.apply(s).replaceAll(e.getKey(), e.getValue()),
        Function::andThen
    );

String result = combined.apply(test);
like image 4
Misha Avatar answered Oct 22 '22 19:10

Misha