I have to write program which should be reading file for anagrams and show word + his anagrams. Txt files is very big, after using scanner, listOfWords size is: 25000.
Output example:
word anagram1 anagram2 anagram3 ...
word2 anagram1 anagram2...
I have code, it works but very slow:
private static List<String> listOfWords = new ArrayList<String>();
private static List<ArrayList<String>> allAnagrams = new ArrayList<ArrayList<String>>();
public static void main(String[] args) throws Exception {
URL url = new URL("www.xxx.pl/textFile.txt");
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
String nextToken = scanner.next();
listOfWords.add(nextToken);
}
scanner.close();
while (listOfWords.isEmpty() == false) {
ArrayList<String> anagramy = new ArrayList<String>();
String wzor = listOfWords.remove(0);
anagramy.add(wzor);
char[] ch = wzor.toCharArray();
Arrays.sort(ch);
for (int i = 0; i < listOfWords.size(); i++) {
String slowo = listOfWords.get(i);
char[] cha = slowo.toCharArray();
Arrays.sort(cha);
if (Arrays.equals(ch, cha)) {
anagramy.add(slowo);
listOfWords.remove(i);
i--;
}
}
allAnagrams.add(anagramy);
}
for (ArrayList<String> ar : allAnagrams) {
String result = "";
if (ar.size() > 1) {
for (int i = 1; i < ar.size(); i++) {
result = ar.get(i) + " ";
}
System.out.println(ar.get(0) + " " + result);
}
}
}
I have to write it with Java 8 - streams but I don't know. It is possible to use Streams for reading from URL + searching anagrams? Could you help me with searching anagrams by Stream? Teacher told me that code should be shorter that mine with reading a whole list. Only a few lines, is that possible?
You can read the words from the file into a List or directly create a Stream of it:
try (InputStream is = new URL("http://www.someurl.pl/file.txt").openConnection().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
Stream<String> stream = reader.lines()) {
//do something with stream
}
Then just stream over the list and collect the anagrams, where all words that have the same sorted list of characters are considered anagrams:
Map<String, List<String>> anagrams =
stream.collect(Collectors.groupingBy(w -> sorted(w)));
The sorted method is just sorting the letters as you did in your example:
public static String sorted(String word) {
char[] chars = word.toCharArray();
Arrays.sort(chars);
return new String(chars);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With