I want to determine if a list is anagram or not using Java 8.
Example input:
"cat", "cta", "act", "atc", "tac", "tca"
I have written the following function that does the job but I am wondering if there is a better and elegant way to do this.
boolean isAnagram(String[] list) {
long count = Stream.of(list)
.map(String::toCharArray)
.map(arr -> {
Arrays.sort(arr);
return arr;
})
.map(String::valueOf)
.distinct()
.count();
return count == 1;
}
It seems I can't sort char array with Stream.sorted()
method so that's why I used a second map operator. If there is some way that I can operate directly on char stream instead of Stream of char array, that would also help.