我如何使用 Java 中的 HashMap 将两个文件中的混乱单词与字典单词进行比较?
How would I compare jumbled words to dictionary words in two files using HashMaps in Java?
所以我构建了一些简单的代码来比较两个文本文件。一个带有混乱的单词列表,这些单词应该与字典文件中的单词匹配。基本上找到哪些混乱的单词与他们的字典单词匹配。有些单词有几个与之匹配的混乱单词,有些则没有任何匹配项。我希望将此代码更改为更简单,使用 HashMaps 使程序更简单和更快,但我对 HashMaps 不是很好,可以使用帮助。
如果有帮助,这是我目前拥有的非哈希图版本的代码:
import java.util.*;
import java.io.*;
public class Project6
{
public static void main(String[] args) throws Exception
{
if (args.length < 2 ) die( "Must give name of two input files on cmd line." );
BufferedReader dFile = new BufferedReader( new FileReader( args[0] ) );
BufferedReader jFile = new BufferedReader( new FileReader( args[1] ) );
ArrayList<String> jWordList= new ArrayList<String>();
ArrayList<String> dWordList= new ArrayList<String>();
long startTime = System.currentTimeMillis();
while (dFile.ready())
{
String word = dFile.readLine();
dWordList.add( word );
}
dFile.close();
while (jFile.ready())
{
String word = jFile.readLine();
jWordList.add( word );
}
jFile.close();
Collections.sort( dWordList );
Collections.sort( jWordList );
String[] dArray = dWordList.toArray(new String[dWordList.size()]);
String[] jArray = jWordList.toArray(new String[jWordList.size()]);
dArray = canonArray( dArray );
jArray = canonArray( jArray );
for(int i = 0 ; i < jWordList.size() ; i++)
{
String jWord = jArray[i];
System.out.print(jWordList.get(i) + " ");
for(int c = 0 ; c < dWordList.size() ; c++)
{
String dWord = dArray[c];
if(jWord.equals(dWord))
{
System.out.print(dWordList.get(c) + " ");
}
}
System.out.println();
}
long endTime = System.currentTimeMillis();
long ms = endTime-startTime;
System.out.println("Elapsed time in seconds: " + ms/1000.0 + "\n"); // 1 ms is a 1,000th of a second
}
private static void die( String errmsg )
{
System.out.println( "\nFATAL ERROR: " + errmsg + "\n" );
System.exit(0);
}
private static String toCanonical( String word )
{
char[] charArray = word.toCharArray();
Arrays.sort(charArray);
String charNewString = new String(charArray);
return charNewString;
}
private static String[] canonArray( String[] Arr )
{
String[] newArr = new String[Arr.length];
for(int i = 0 ; i < Arr.length ; i++)
{
String temp = toCanonical(Arr[i]);
newArr[i] = temp;
}
return newArr;
}
}
它产生以下输出,我希望保持完全相同(减去经过时间的打印):
您想要的是定义一个 HashMap,这样无论字符串字符的顺序和大小写如何,键的散列和 equals 方法的结果都是一样的。以下采用一个字符串并将其转换为小写并对字符进行排序。
import java.util.*;
import java.io.*;
public class Project6 {
public static void main(String[] args) throws Exception {
if (args.length < 2) die("Must give name of two input files on cmd line.");
BufferedReader dFile = new BufferedReader(new FileReader(args[0]));
BufferedReader jFile = new BufferedReader(new FileReader(args[1]));
HashMap<String, List<String>> dWordMap = new HashMap<String, List<String>>();
long startTime = System.currentTimeMillis();
while (dFile.ready()) {
String word = dFile.readLine();
if (word == null) break;
addWord(word, dWordMap);
}
dFile.close();
while (jFile.ready()) {
String jWord = jFile.readLine();
if (jWord == null) break;
List<String> dWords = dWordMap.get(createKey(jWord));
if (dWords != null) {
System.out.println(jWord + " " + dWords);
}
}
jFile.close();
long endTime = System.currentTimeMillis();
long ms = endTime - startTime;
System.out.println("Elapsed time in seconds: " + ms / 1000.0 + "\n");
}
private static void die(String errmsg) {
System.out.println("\nFATAL ERROR: " + errmsg + "\n");
System.exit(0);
}
private static String createKey(String word) {
char[] chword = word.toLowerCase().toCharArray();
Arrays.sort(chword);
return new String(chword);
}
private static void addWord(String word, Map<String, List<String>> map) {
String key = createKey(word);
List<String> list = map.get(key);
if(list==null) {
list = new ArrayList<String>();
map.put(key, list);
}
list.add(word);
}
}
所以我构建了一些简单的代码来比较两个文本文件。一个带有混乱的单词列表,这些单词应该与字典文件中的单词匹配。基本上找到哪些混乱的单词与他们的字典单词匹配。有些单词有几个与之匹配的混乱单词,有些则没有任何匹配项。我希望将此代码更改为更简单,使用 HashMaps 使程序更简单和更快,但我对 HashMaps 不是很好,可以使用帮助。
如果有帮助,这是我目前拥有的非哈希图版本的代码:
import java.util.*;
import java.io.*;
public class Project6
{
public static void main(String[] args) throws Exception
{
if (args.length < 2 ) die( "Must give name of two input files on cmd line." );
BufferedReader dFile = new BufferedReader( new FileReader( args[0] ) );
BufferedReader jFile = new BufferedReader( new FileReader( args[1] ) );
ArrayList<String> jWordList= new ArrayList<String>();
ArrayList<String> dWordList= new ArrayList<String>();
long startTime = System.currentTimeMillis();
while (dFile.ready())
{
String word = dFile.readLine();
dWordList.add( word );
}
dFile.close();
while (jFile.ready())
{
String word = jFile.readLine();
jWordList.add( word );
}
jFile.close();
Collections.sort( dWordList );
Collections.sort( jWordList );
String[] dArray = dWordList.toArray(new String[dWordList.size()]);
String[] jArray = jWordList.toArray(new String[jWordList.size()]);
dArray = canonArray( dArray );
jArray = canonArray( jArray );
for(int i = 0 ; i < jWordList.size() ; i++)
{
String jWord = jArray[i];
System.out.print(jWordList.get(i) + " ");
for(int c = 0 ; c < dWordList.size() ; c++)
{
String dWord = dArray[c];
if(jWord.equals(dWord))
{
System.out.print(dWordList.get(c) + " ");
}
}
System.out.println();
}
long endTime = System.currentTimeMillis();
long ms = endTime-startTime;
System.out.println("Elapsed time in seconds: " + ms/1000.0 + "\n"); // 1 ms is a 1,000th of a second
}
private static void die( String errmsg )
{
System.out.println( "\nFATAL ERROR: " + errmsg + "\n" );
System.exit(0);
}
private static String toCanonical( String word )
{
char[] charArray = word.toCharArray();
Arrays.sort(charArray);
String charNewString = new String(charArray);
return charNewString;
}
private static String[] canonArray( String[] Arr )
{
String[] newArr = new String[Arr.length];
for(int i = 0 ; i < Arr.length ; i++)
{
String temp = toCanonical(Arr[i]);
newArr[i] = temp;
}
return newArr;
}
}
它产生以下输出,我希望保持完全相同(减去经过时间的打印):
您想要的是定义一个 HashMap,这样无论字符串字符的顺序和大小写如何,键的散列和 equals 方法的结果都是一样的。以下采用一个字符串并将其转换为小写并对字符进行排序。
import java.util.*;
import java.io.*;
public class Project6 {
public static void main(String[] args) throws Exception {
if (args.length < 2) die("Must give name of two input files on cmd line.");
BufferedReader dFile = new BufferedReader(new FileReader(args[0]));
BufferedReader jFile = new BufferedReader(new FileReader(args[1]));
HashMap<String, List<String>> dWordMap = new HashMap<String, List<String>>();
long startTime = System.currentTimeMillis();
while (dFile.ready()) {
String word = dFile.readLine();
if (word == null) break;
addWord(word, dWordMap);
}
dFile.close();
while (jFile.ready()) {
String jWord = jFile.readLine();
if (jWord == null) break;
List<String> dWords = dWordMap.get(createKey(jWord));
if (dWords != null) {
System.out.println(jWord + " " + dWords);
}
}
jFile.close();
long endTime = System.currentTimeMillis();
long ms = endTime - startTime;
System.out.println("Elapsed time in seconds: " + ms / 1000.0 + "\n");
}
private static void die(String errmsg) {
System.out.println("\nFATAL ERROR: " + errmsg + "\n");
System.exit(0);
}
private static String createKey(String word) {
char[] chword = word.toLowerCase().toCharArray();
Arrays.sort(chword);
return new String(chword);
}
private static void addWord(String word, Map<String, List<String>> map) {
String key = createKey(word);
List<String> list = map.get(key);
if(list==null) {
list = new ArrayList<String>();
map.put(key, list);
}
list.add(word);
}
}