I gave it a try, my "quick" solution was 15 lines and despite thinking I have it and tests will pass, i ran tests twice during that and they failed, only on the 3rd try it was passing. Were just minor tweaks that were necessary like adding a space between the words but still, unexpected test failures.
In case this comment understands markdown, and someone is interested to look at my solution:
```java
if (text.contains(" ")) {
String[] strings = text.split(" ");
Map<String, Integer> map = new HashMap<>();
for (String string : strings) {
if (map.containsKey(string)) {
Integer integer = map.get(string);
map.put(string, integer + 1);
} else {
map.put(string, 1);
}
}
String result = "";
for (Entry<String, Integer> entry : map.entrySet()) {
result += entry.getKey() + "=" + entry.getValue() + " ";
I gave it a try, my "quick" solution was 15 lines and despite thinking I have it and tests will pass, i ran tests twice during that and they failed, only on the 3rd try it was passing. Were just minor tweaks that were necessary like adding a space between the words but still, unexpected test failures.
In case this comment understands markdown, and someone is interested to look at my solution:
```java
if (text.contains(" ")) {
String[] strings = text.split(" ");
Map<String, Integer> map = new HashMap<>();
for (String string : strings) {
if (map.containsKey(string)) {
Integer integer = map.get(string);
map.put(string, integer + 1);
} else {
map.put(string, 1);
}
}
String result = "";
for (Entry<String, Integer> entry : map.entrySet()) {
result += entry.getKey() + "=" + entry.getValue() + " ";
}
return result.trim();
}
```
and now refactored using streams:
```java
if (text.contains(" ")) {
return Arrays.stream(text.split(" "))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
.map(c -> c.getKey() + "=" + c.getValue())
.collect(Collectors.joining(" "));
}
```