[英]Trying to get a unique version of a List of strings that is case insensitive
我有一个字符串列表,我想为其创建一个唯一的版本,以便计算类型标记率。 它必须不区分大小写才能正常运行,并且我不确定应该如何处理。 这是在Java中,我忘了在标题中提及。 这是java.util.List,而不是java.awt.List。
编辑:我决定谈论更多关于代码。 这个必需的类具有一个名为Case的对象,该对象具有原始列表。
Book Case = new Book(String x);
public float getTTRatio(){
//insert creation of unique list here.
}
该列表称为单词,并用文本文件的单词填充,因此我需要使列表唯一的调用是Case.words。 我需要使其成为方法的返回值,其中unique是仅用唯一词填充且不区分大小写的List的名称:
return unique.size/Case.words.size();
由于您只需要大小,因此可以使用一Set
大写单词来实现:
Set<String> unique = new HashSet<>();
for (String word : Case.words) {
// Adds only if the same uppercase String wasn't added before
unique.add(word.toUpperCase());
}
return (float) unique.size() / Case.words.size(); // convert one of the ints to float
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.