will it be any better with the code performance?
No, it'll not. Iterative solutions are usually more performant.
how can I do it using forEach
There's a special operation collect() in the Stream API that is meant to populate a mutable container (e.g. Collection, StringBuilder, etc.) with the contents of the stream pipeline. Usage of forEach() for that purpose is highly discouraged by the documentation. Consider utilizing forEach() only as a last resort, when there's no other way to achieve that.
To do that with collect(), first, you need to create a stream of entries.
Based on each entry, a new entry has to be created, map() operation is utilized for that purpose. Static method Map.entry() is used to instantiate a new entry.
And then apply the terminal operation collect() by passing Collectors.toMap() as parameter, which creates a collector (object responsible for placing the stream elements into a mutable container, a map in this case) based on the two provided functions (for keys and values).
main()
public static void main(String[] args) {
Map<String,List<String>> mapWithList =
Map.of("1", List.of("1", "2", "3"));
Map<String,Set<String>> result =
mapWithList.entrySet().stream()
.map(entry -> Map.entry(entry.getKey(),
new HashSet<>(entry.getValue())))
.collect(Collectors.toMap(Map.Entry::getKey,
Map.Entry::getValue));
System.out.println(result);
}
Output
{1=[1, 2, 3]}