Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyWord中compareTo排序算法不兼容JDK7+ #121

Open
qiyankai opened this issue Jan 5, 2021 · 0 comments
Open

KeyWord中compareTo排序算法不兼容JDK7+ #121

qiyankai opened this issue Jan 5, 2021 · 0 comments

Comments

@qiyankai
Copy link

qiyankai commented Jan 5, 2021

老哥,JDK7+以上排序算法不是快排,是timsort算法,需要考虑两个值相等的情况
否则会抛出Comparison method violates its general contract!异常

将tfidfvalue换为包装类型,使用包装类型的compareTo方法
解决方案如下:
public Keyword(String name, Double tfidfvalue) {
this.name = name;
// tfidf值只保留3位小数
this.tfidfvalue = Double.valueOf((double)Math.round(tfidfvalue * 10000) / 10000);
}

/**
 * 为了在返回tdidf分析结果时,可以按照值的从大到小顺序返回,故实现Comparable接口
 */
@Override
public int compareTo(Keyword o) {
    return o.tfidfvalue.compareTo(this.tfidfvalue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant