How to split value and save into Map java?

I am having a string which has some | delimited value

He run|running|runned quickly, then he|she goes for practice

what is the possible way to place the save the | separated value in Hashmap Map<Integer , List<String>> so that map store for first

 1 -> ["run","running","runned"]
 2 -> ["he","she"]

How can it be achieved. What is the best to split this string and save the value in Map.

Answer

Try this:

String text = "He run|running|runned quickly, then he|she goes for practice";

// match all the parts which are sequences of words delimited by |
Matcher matcher = Pattern.compile("(\w+(?:\|\w+)+)").matcher(text);

Map<Integer, List<String>> map = new HashMap<>();

// add matches as long as we find any.
while (matcher.find()) {
    map.put(map.size() + 1, 
    // split matches by | and convert them to a list
    Arrays.stream(matcher.group(1).split("\|")).collect(Collectors.toList()));
}

Output:

map.forEach((k, v) -> 
    System.out.printf("%s = %sn", k, v.stream().collect(Collectors.joining(","))));

yields:

1 = run,running,runned
2 = he,she