import java.io.BufferedReader; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; public class FileHandling { public List findPhrase(final Path file) { HashMap phrases = new HashMap(); try (BufferedReader br = Files.newBufferedReader(file, Charset.forName("ISO-8859-1"))) { while (br.ready()) { String line = br.readLine(); String[] linePhrases = line.split("\\|"); for (String phrase : linePhrases) { phrase = phrase.trim(); if (phrase.length() <= 0) continue; if (!phrases.containsKey(phrase)) { phrases.put(phrase, 1); } else { phrases.put(phrase, phrases.get(phrase) + 1); } } } br.close(); } catch (IOException e) { System.out.println("Please make sure you have the right txt file."); e.printStackTrace(); } return appendList(phrases); } private List appendList(final Map phraseMap) { TreeMap sPhrases = new TreeMap( (key, value) -> phraseMap.get(key) > phraseMap.get(value) ? -1 : 1); sPhrases.putAll(phraseMap); List listOfPhrase = new ArrayList(); int counter = 0; for (Map.Entry entry : sPhrases.entrySet()) { listOfPhrase.add(entry.getKey()); sPhrases.remove(entry.getKey()); counter++; if (counter >= 100000) break; } return listOfPhrase; } }