假设我有一个字符串
String test = "This is a test string and I have some stopwords in here";
我想看看下面数组中的单词与我的字符串匹配了多少次
伪代码
array = a,and,the,them,they,I
所以答案是“3”
只是好奇在 Java 中最有效的方法是什么?
最佳答案
是这样的吗?不确定“最有效”,但足够简单。
Set<String> s1 = new HashSet<String>(Arrays.asList("This is a test string and I have some stopwords in here".split("\\s")));
Set<String> s2 = new HashSet<String>(Arrays.asList("a", "and", "the", "them", "they", "I"));
s1.retainAll(s2);
System.out.println(s1.size());
只是两组词的交集。
关于java - 查找字符串与单词数组的匹配次数的最有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3208971/