import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
String arr[] = {"abcd", "java", "dcba", "ajav", "xyz", "epam", "pame", "aepm"};
//Stream.of(arr).map(s -> s.split("")).sorted().map(String::joining);
Map<String, List<String>> occurrences = new HashMap<>();
int length = arr.length;
for (int i = 0; i < length - 1; i++) {
String currentWord = arr[i];
String[] currentWordArray = currentWord.split("");
String currentWordSorted = Stream.of(currentWordArray).sorted().collect(Collectors.joining());
if (occurrences.containsKey(currentWordSorted)) {
List<String> wordOcurrences = occurrences.get(currentWordSorted);
wordOcurrences.add(currentWord);
} else {
List<String> newOccurrences = new ArrayList<>();
newOccurrences.add(currentWord);
occurrences.put(currentWordSorted, newOccurrences);
}
}
System.out.println("Ocurrences found:" + occurrences);
}
}