The word Anagrams refers to two strings such that by arranging the letters of one string another string is created. Typically all the letters from the string are used only once. To understand what anagrams are actually we need to look into an example. The letters from the word net can be written as ten. The two words are anagrams.
The detailed implementation will be depicted in this article on whether the strings that have been entered are anagrams.
How to Check/Inspect for Anagrams in Java?
In Java the anagram for strings will be implemented in such a manner that two strings will be declared as input and the code will be designed in such a way that two strings will be tested, whether the strings entered are anagrams or not. Java is considered as a case-sensitive programming language therefore, the conversion is really important to obtain correct results.
The code to check for anagrams in Java contains the following three important techniques:
- string to a character
- arrays.sort
- arrays.equals
Example 1: Checking Two Strings for Anagrams
Using the above parameters the code is depicted below to check if two strings are anagrams or not.
import java.util.*;
class Anagram {
public static void main(String[] args) {
String stri1 = "Lamb";
String stri2 = "Balm";
stri1 = stri1.toLowerCase();
stri2 = stri2.toLowerCase();
if(stri1.length() == stri2.length()) {
char[] ca1 = stri1.toCharArray();
char[] ca2 = stri2.toCharArray();
Arrays.sort(ca1);
Arrays.sort(ca2);
boolean output = Arrays.equals(ca1, ca2);
if(output) {
System.out.println(stri1 + " and " + stri2 + " are strings that are anagrams.");
}
else {
System.out.println(stri1 + " and " + stri2 + " are strings that are not anagrams.");
}
}
else {
System.out.println(stri1 + " and " + stri2 + " are the strings that are not anagrams.");
}
}
}
In the above code:
- We have declared two strings.
- The strings are converted to lowercase since Java is case-sensitive.
- The string length has been compared, and the length if found to be the same the loop continues to run, or the strings are declared non-anagrams of each other.
- The entered string has to be converted to the array of characters when the length happens to be the same.
- The char array is sorted using the sort command.
- If the sorted arrays are the same then the declared strings are anagrams.
Output

In the above output, the two strings are anagrams. Since Java is case-sensitive, therefore, two strings have been made of the lowercase and then compared.
Example 2: Checking Two Strings for Anagrams Using User Input
In this code, the only difference is that the input is taken from the user for the two strings to check whether the strings are anagrams.
import java.util.Arrays;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner i = new Scanner(System.in);
System.out.print("Enter first String: ");
String stri1 = i.nextLine();
System.out.print("Enter second String: ");
String stri2 = i.nextLine(); stri1 = stri1.toLowerCase();
stri2 = stri2.toLowerCase();
if(stri1.length() == stri2.length()) {
char[] ca1 = stri1.toCharArray();
char[] ca2 = stri2.toCharArray();
Arrays.sort(ca1);
Arrays.sort(ca2);
boolean output = Arrays.equals(ca1, ca2);
if(output) {
System.out.println(stri1 + " and " + stri2 + " are the strings that are anagrams.");
}
else {
System.out.println(stri1 + " and " + stri2 + " are the strings that are not anagrams.");
}
}
else {
System.out.println(stri1 + " and " + stri2 + " are not anagram.");
}
i.close();
}
}
Output
The strings mentioned in the output below have been declared as anagrams of each other.

Example 3: Implementing Anagrams Using Hashmap
A Java hashmap lets us store a key-value pair that can be accessed using the index. This example will let you understand how to check for anagrams in Java using the Hashmap:
import java.io.*;
import java.util.*;
class hashanagram {
public static void main(String args[]) {
String str1 = "lamb";
String str2 = "Balm"; str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
HashMap < Character,Integer > hashmap1 = new HashMap < Character, Integer > ();
HashMap < Character,Integer > hashmap2 = new HashMap < Character, Integer > ();
char array1[] = str1.toCharArray();
char array2[] = str2.toCharArray();
for (int x = 0; x < array1.length; x++) {
if (hashmap1.get(array1[x]) == null) {
hashmap1.put(array1[x], 1);
} else {
Integer h = (int) hashmap1.get(array1[x]);
hashmap1.put(array1[x], ++h);
}
}
for (int y = 0; y < array2.length; y++) {
if (hashmap2.get(array2[y]) == null)
hashmap2.put(array2[y], 1);
else {
Integer h = (int) hashmap2.get(array2[y]);
hashmap2.put(array2[y], ++h);
}
}
if (hashmap1.equals(hashmap2))
System.out.println("The two strings are anagrams");
else
System.out.println("The two strings aren’t anagrams");
}
}
In the code above:
- Two strings have been declared.
- Create two hashmaps and keep one hashmap with string 1 and the second hashmap with string 2.
- For loop is created to check each character in the string. If the character is not present add it to the hashmap; otherwise, if it is already present increase the count.
- Now check using the if-else statement, whether the two hashmaps are equal or not.
- The strings appeared to be the same, declare these strings as anagrams. The strings that are different from each other have been declared as “not anagrams”.
Output
The output using the hashmap is attached below:

That’s all about checking for anagrams in Java using different methods.
Conclusion
In Java, the anagrams can be checked by various techniques, such as arrays.sort(), arrays.equals(), etc. Anagrams are strings that contain the same and equal characters typically used once from one of the strings to form another string. This Java post has explained several methods to check for anagrams in Java.