如何使用Java中的HashMaps将混乱的单词与两个文件中的字典单词进行比较?

时间:2015-04-15 15:46:15

标签: java hashmap hashset

所以我构造了一些简单的代码来比较两个文本文件。一个有混乱的单词列表,应该匹配字典文件中的单词。基本上找到哪些混乱的单词与他们的字典单词匹配。有些单词有几个与他们匹配的混乱单词,有些单词没有任何匹配。我希望将此代码更改为更简单,使用HashMaps使程序更简单,更快,但我对HashMaps不太好,可以使用帮助。

以下是我目前为非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

1 个答案:

答案 0 :(得分:1)

你想要的是定义一个HashMap,这样无论字符串字符的顺序和大小写,key的hash和equals方法都会相同。以下采用String并将其转换为小写并对字符进行排序。

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