Find all anagrams to a given word from a text file
Use more functions

The current implementation is all in the main method.
Break the problem down to smaller pieces,
so that each function has a single responsibility, for example:


Read words from a text file into a List and return it
Convert a word to "normalized" form (sorted and lowercased)
Convert a list of words to a map of normalized form to list of anagrams
Read input and respond with the list of anagrams


Use interface types instead implementations

Instead of these:


ArrayList&lt;String&gt; words = new ArrayList&lt;String&gt;();
// ...

HashMap&lt;String, ArrayList&lt;String&gt;&gt; map = new HashMap&lt;&gt;();



It's better to use interface types in declarations, like this:

List&lt;String&gt; words = new ArrayList&lt;&gt;();
// ...

Map&lt;String, List&lt;String&gt;&gt; map = new HashMap&lt;&gt;();


And on the right-hand side, use the diamond operator &lt;&gt; when possible.

Use try-with-resources when working with resources

Instead of this:


Scanner s = new Scanner(new File("bg.txt"));

ArrayList&lt;String&gt; words = new ArrayList&lt;String&gt;();
while (s.hasNext()) {
    words.add(s.next());
}

// much much later...
s.close();



Write like this:

List&lt;String&gt; words = new ArrayList&lt;&gt;();
try (Scanner s = new Scanner(new File("bg.txt"))) {
    while (s.hasNext()) {
        words.add(s.next());
    }
}


Notice that s.close() is no longer necessary.
That also means you cannot "forget" to close a resource manually.

You can read more about this here: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html