How to Check if Two Strings Are Anagrams in Java 8

Learn how to check if two strings are anagrams in Java 8 using the Stream API and Collectors.
An anagram is a word or phrase formed by rearranging the letters of another word using all the original characters exactly once.

Code Explanation (Step-by-Step)

  • Using split(“”), both strings are split into individual characters.
  • The map(a->a.toUpperCase()) method converts all characters to uppercase.
  • The sorted() method sorts the characters in ascending order.
  • The collect(Collectors.joining()) method joins the sorted characters into a single string .
  • The equals() method compares both resulting strings.
  • If both strings are equal, they are anagrams; otherwise, they are not.
				
					import java.util.Arrays;
import java.util.stream.Collectors;

public class CheckAnagram {

    public static void main(String[] args) {

        String str1 = "listen";
        String str2 = "silent";

        String r1 = Arrays.stream(str1.split(""))
                          .map(a -> a.toUpperCase())
                          .sorted()
                          .collect(Collectors.joining());

        String r2 = Arrays.stream(str2.split(""))
                          .map(a -> a.toUpperCase())
                          .sorted()
                          .collect(Collectors.joining());

        if (r1.equals(r2)) {
            System.out.println("It is an anagram string.");
        } else {
            System.out.println("It is not an anagram string.");
        }
    }
}
				
			

Output :-
It is an anagram string.

Check if Two Strings Are Anagrams in Java 8

				
					import java.util.stream.Collectors;

public class CheckAnagram {

    public static void main(String[] args) {

        String str1 = "listen";
        String str2 = "silent";

        String sortedStr1 = str1.toUpperCase()
                                .chars()
                                .sorted()
                                .mapToObj(c -> String.valueOf((char) c))
                                .collect(Collectors.joining());

        String sortedStr2 = str2.toUpperCase()
                                .chars()
                                .sorted()
                                .mapToObj(c -> String.valueOf((char) c))
                                .collect(Collectors.joining());

        if (sortedStr1.equals(sortedStr2)) {
            System.out.println("The strings are anagrams.");
        } else {
            System.out.println("The strings are not anagrams.");
        }
    }
}
				
			

FAQ
What is an anagram string?
An anagram is a word formed by rearranging the letters of another word while using all original characters exactly once.
How do you check if two strings are anagrams in Java 8?
You can split the strings into characters, sort them using Stream API, and compare the resulting strings.
Why use Stream API for anagram checking?
Stream API provides a concise and readable way to transform, sort, and compare string characters.
Is the comparison case-sensitive?
No. In this example, all characters are converted to uppercase before comparison.
What is the time complexity of this approach?
The time complexity is approximately O(n log n) because sorting is required.