private static void hashQuery( LinkedList<LinkedList<Anagram>>[] dictList, Scanner queryScan, PrintWriter outputWriter) { Anagram queryItem; int index; LinkedList<Anagram> printPoint; while (queryScan.hasNext()) { printPoint = null; queryItem = new Anagram(queryScan.nextLine()); index = queryItem.cantorHash(); if (dictList[index] != null) { for (LinkedList<Anagram> a : dictList[index]) { if (queryItem.getKey().compareTo(a.peek().getKey()) == 0) { printPoint = a; break; } } } writeAnagramsToFile(printPoint, outputWriter, queryItem); } }
private static int hashStash(String input, LinkedList<LinkedList<Anagram>>[] dictList) { int collCount = 0; Anagram addition = new Anagram(input); // int index = Math.abs(addition.getKey().hashCode()) % 199967; int index = addition.cantorHash(); // int index = Math.abs(input.hashCode()) % 199967; if (dictList[index] == null) { dictList[index] = new LinkedList<LinkedList<Anagram>>(); } for (LinkedList<Anagram> a : dictList[index]) { if (a.peek() != null && addition.getKey().equals(a.element().getKey())) { a.addFirst(addition); return collCount; } collCount++; } dictList[index].addFirst(new LinkedList<Anagram>()); dictList[index].element().addFirst(addition); return collCount; }
private static void writeAnagramsToFile( LinkedList<Anagram> anagramList, PrintWriter outputWriter, Anagram queryItem) { if (anagramList == null) { outputWriter.println(queryItem.getWord() + " 0"); return; } int count = 0; outputWriter.print(queryItem.getWord()); for (Anagram a : anagramList) { if (a.compareTo(queryItem) != 0) { count++; } } outputWriter.print(" " + count); for (Anagram a : anagramList) { if (a.compareTo(queryItem) != 0) { outputWriter.print(" " + a.getWord()); } } outputWriter.println(); }