Java 使用比较器调试对(列表、比较器)进行排序

Java 使用比较器调试对(列表、比较器)进行排序,java,sorting,comparator,Java,Sorting,Comparator,我正在尝试使用集合.sort(list,comparator)对对象进行排序。代码编译正确,但在运行时出现以下错误。这是我第一次使用comparator和collections.sort,但似乎无法在代码中找到导致问题的原因。错误是说Collections.sort(list,newwordcountcomparator())是问题的原因。我是否正确定义了comparator类?我是否正确使用了>泛型类型 最初,我在CLI上使用以下命令运行代码,以编译、创建jar并运行: javac MP1.j

我正在尝试使用
集合.sort(list,comparator)
对象进行排序。代码编译正确,但在运行时出现以下错误。这是我第一次使用comparator和collections.sort,但似乎无法在代码中找到导致问题的原因。错误是说
Collections.sort(list,newwordcountcomparator())是问题的原因。我是否正确定义了comparator类?我是否正确使用了>泛型类型

最初,我在CLI上使用以下命令运行代码,以编译、创建jar并运行:

javac MP1.java
jar cfe MP1.jar MP1 MP1.class
java -jar MP1.jar 1
并收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: MP1$WordCountComparator
    at MP1.process(MP1.java:112)
    at MP1.main(MP1.java:132)
Caused by: java.lang.ClassNotFoundException: MP1$WordCountComparator
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 2 more
我尝试使用以下命令在不创建jar文件的情况下运行代码:

javac MP1.java
java MP1 1
这就是出现的错误

Exception in thread "main" java.lang.NullPointerException
at java.lang.Integer.compareTo(Integer.java:1003)
at MP1$WordCountComparator.compare(MP1.java:15)
at MP1$WordCountComparator.compare(MP1.java:12)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:325)
at java.util.TimSort.sort(TimSort.java:203)
at java.util.TimSort.sort(TimSort.java:173)
at java.util.Arrays.sort(Arrays.java:659)
at java.util.Collections.sort(Collections.java:217)
at MP1.process(MP1.java:112)
at MP1.main(MP1.java:132)
整个MP1.java文件的代码为:

import java.lang.reflect.Array;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.io.*;


public class MP1 {


private static class WordCountComparator implements Comparator<Map.Entry<String, Integer>> {
    public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
    {
        if(o2.getValue().compareTo( o1.getValue()) != 0) {
            return o2.getValue().compareTo(o1.getValue());
        } 
        else {
            return o1.getKey().compareTo(o2.getKey());
        }
    }
}

Random generator;
String userName;
String inputFileName;
String delimiters = " \t,;.?!-:@[](){}_*/";
String[] stopWordsArray = {"i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your", "yours",
        "yourself", "yourselves", "he", "him", "his", "himself", "she", "her", "hers", "herself", "it", "its",
        "itself", "they", "them", "their", "theirs", "themselves", "what", "which", "who", "whom", "this", "that",
        "these", "those", "am", "is", "are", "was", "were", "be", "been", "being", "have", "has", "had", "having",
        "do", "does", "did", "doing", "a", "an", "the", "and", "but", "if", "or", "because", "as", "until", "while",
        "of", "at", "by", "for", "with", "about", "against", "between", "into", "through", "during", "before",
        "after", "above", "below", "to", "from", "up", "down", "in", "out", "on", "off", "over", "under", "again",
        "further", "then", "once", "here", "there", "when", "where", "why", "how", "all", "any", "both", "each",
        "few", "more", "most", "other", "some", "such", "no", "nor", "not", "only", "own", "same", "so", "than",
        "too", "very", "s", "t", "can", "will", "just", "don", "should", "now"};

void initialRandomGenerator(String seed) throws NoSuchAlgorithmException {
    MessageDigest messageDigest = MessageDigest.getInstance("SHA");
    messageDigest.update(seed.toLowerCase().trim().getBytes());
    byte[] seedMD5 = messageDigest.digest();

    long longSeed = 0;
    for (int i = 0; i < seedMD5.length; i++) {
        longSeed += ((long) seedMD5[i] & 0xffL) << (8 * i);
    }

    this.generator = new Random(longSeed);
}

Integer[] getIndexes() throws NoSuchAlgorithmException {
    Integer n = 10000;
    Integer number_of_lines = 50000;
    Integer[] ret = new Integer[n];
    this.initialRandomGenerator(this.userName);
    for (int i = 0; i < n; i++) {
        ret[i] = generator.nextInt(number_of_lines);
    }
    return ret;
}

public MP1(String userName, String inputFileName) {
    this.userName = userName;
    this.inputFileName = inputFileName;
}

public String[] process() throws Exception {
    String[] ret = new String[20];

    File file = new File(this.inputFileName);
    Scanner scanner = new Scanner(file);
    String[] lines = new String[50000];

    int i = 0;
    while(scanner.hasNextLine()){
        lines[i] = scanner.nextLine();
        i++;
    }
    Integer[] indices = getIndexes();

    String[] records = new String[10000];

    //ArrayList<String> words = new ArrayList<String>();
    Map<String, Integer> wordCount = new HashMap<String, Integer>();

    i = 0;
    for(Integer index:indices){

        records[i] = lines[index].toLowerCase().trim();
        StringTokenizer tokenOfString = new StringTokenizer(records[i], this.delimiters);
        i++;

        while(tokenOfString.hasMoreTokens()){

            String token = tokenOfString.nextToken();

            if(!Arrays.asList(stopWordsArray).contains(token)){

                if(wordCount.get(token) == null) {

                    wordCount.put(token,1);
                }
                else{
                    wordCount.put(token, wordCount.get(token + 1));
                }

                }
            }
        }
    List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(wordCount.entrySet());
    Collections.sort(list, new WordCountComparator() );

    for (i = 0; i < 20; i++ ){

        ret[i] = list.get(i).getKey() + "\t" + Integer.toString(list.get(i).getValue());

    }

    return ret;

}

public static void main(String[] args) throws Exception {
    if (args.length < 1){
        System.out.println("MP1 <User ID>");
    }
    else {
        String userName = args[0];
        String inputFileName = "/home/unknown/git/cloudapp-mp1/input.txt";
        MP1 mp = new MP1(userName, inputFileName);
        String[] topItems = mp.process();
        for (String item: topItems){
            System.out.println(item);
        }
    }
}
import java.lang.reflect.Array;
导入java.security.MessageDigest;
导入java.security.NoSuchAlgorithmException;
导入java.util.*;
导入java.io.*;
公共类MP1{
私有静态类WordCountComparator实现Comparator{
公共整数比较(映射项o1,映射项o2)
{
如果(o2.getValue().compareTo(o1.getValue())!=0){
返回o2.getValue().compareTo(o1.getValue());
} 
否则{
返回o1.getKey().compareTo(o2.getKey());
}
}
}
随机发生器;
字符串用户名;
字符串输入文件名;
字符串分隔符=“\t,;.?!-:@[](){}\u*/”;
String[]stopWordsArray={“我”、“我”、“我的”、“我自己”、“我们”、“我们的”、“我们的”、“我们自己”、“你”、“你的”、“你的”,
“你自己”、“你自己”、“他”、“他”、“他”、“他自己”、“她”、“她的”、“她自己”、“它”、“它”,
“自身”、“他们”、“他们”、“他们的”、“他们的”、“他们自己”、“什么”、“哪个”、“谁”、“谁”、“这个”、“那个”,
“这些”、“那些”、“我”、“是”、“是”、“是”、“是”、“是”、“是”、“是”、“是”、“有”、“有”、“有”、“有”、“有”、“有”,
“do”、“does”、“did”、“doing”、“a”、“an”、“the”、“and”、“but”、“if”、“or”、“because”、“as”、“until”、“while”,
“of”、“at”、“by”、“for”、“with”、“about”、“offer”、“between”、“into”、“through”、“during”、“before”,
“后”、“上”、“下”、“到”、“从”、“上”、“下”、“进”、“出”、“开”、“关”、“过”、“下”、“再”,
“进一步”、“然后”、“一次”、“这里”、“那里”、“何时”、“何地”、“为什么”、“如何”、“全部”、“任何”、“两者”、“各自”,
“少数”、“更多”、“大多数”、“其他”、“一些”、“这样”、“不”、“也”、“不”、“仅”、“拥有”、“相同”、“所以”、“比”,
“太”、“非常”、“s”、“t”、“can”、“will”、“just”、“don”、“should”、“now”};
void initialRandomGenerator(字符串种子)抛出NoSuchAlgorithmException{
MessageDigest=MessageDigest.getInstance(“SHA”);
update(seed.toLowerCase().trim().getBytes());
字节[]seedMD5=messageDigest.digest();
长长籽=0;
for(int i=0;ilongSeed+=((long)seedMD5[i]&0xffL)我猜在构建jar文件时,您错过了
MP1$WordCountComparator.class

jar cfe MP1.jar MP1 MP1.class
你必须使用

jar cfe MP1.jar MP1 MP1.class MP1\$WordCountComparator.class

您有多少个
.java
源文件以及如何构建代码?这看起来像是一个构建问题。我将所有代码都放在一个.java文件中,并使用以下命令在命令行上运行:javac MP1.java、jar cfe MP1.jar MP1.class、java-jar MP1.jar 1。它在最后一个命令时中断。MP1$WordCountComparator.class和w是什么我该怎么办呢?代码中有一个名为
WordCountComparator
的内部类,您在
排序()中将其作为comparator传递
call。不知何故,您没有复制该类文件,或者在创建jar时没有包含该类文件。您所说的在创建jar时没有复制该类文件和没有包含该类文件是什么意思?我对java不太熟悉。我的内部类与MP1.java中程序的其余代码在同一个文件中。即使它在MP1中。java,但内部类的类文件总是单独创建的。检查MP1.class文件所在的位置,您会发现
MP1$WordCountComparator.class
也在那里。我确实在我的目录中看到了“MP1$WordCountComparator.class”。那么我在创建jar时如何指定内部类呢?