gpt4 book ai didi

java - 如何使用 Java 中的 HashMap 将两个文件中的困惑单词与字典单词进行比较?

转载 作者:行者123 更新时间:2023-12-01 11:39:31 24 4
gpt4 key购买 nike

所以我构建了一些简单的代码来比较两个文本文件。一个包含困惑的单词列表的单词,这些单词应该与字典文件中的单词匹配。基本上是找到哪些困惑的单词与字典中的单词相匹配。有些单词有一些与之匹配的困惑单词,有些则没有任何匹配。我希望使用 HashMap 将此代码更改得更简单,使程序更简单、更快,但我根本不擅长 HashMap,可以使用帮助。

这是我当前非 HashMap 版本的代码(如果有帮助的话):

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;
}
}

它产生以下输出,我希望保持完全相同(减去耗时的打印):

output

最佳答案

您想要定义一个 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);
}
}

关于java - 如何使用 Java 中的 HashMap 将两个文件中的困惑单词与字典单词进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29654828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com