java - 寻找类似 contains 的方法
static boolean contains(Iterable<String> haystack, String needle) {
for (String s : haystack) {
if (s.contains(needle)) {
return true;
}
}
return false;
}
static void containsAll() throws IOException {
List<String> words = loadLines("opacial.txt");
List<String> tocheck = loadLines("queries0.txt");
System.out.println(words.size());
System.out.println(tocheck.size());
int index2 = 0;
for (String s : tocheck) {
if (contains(words, s)) {
index2++;
//return false;
}
}
System.out.println(index2);
//return true;
}
我正在寻找像 contains (上面的代码)这样的方法,它可以执行以下操作: 它将检查 haystack 中是否存在 Needle,或者 haystack 中的 Needle 是否是字符串的一部分。 在这种情况下(上面的代码),如果我反转进入 haystack 的文件和提供针的文件,结果是相同的。但我不想那样。例如:
File 1:
i love beers
i like travelling
stackoverflow
beers
And File2 :
beers
i love stackoverflow
如果 haystack 来自文件 1,needle 来自文件 2,我希望结果为 2,因为单词 beers 只与 haystack 的两个字符串部分或相同。 (啤酒 ---> 我喜欢啤酒和啤酒)-我喜欢 stackoverflow 没有任何反应) 但是当 haystack 来自文件 2 并且 Needle 来自文件 1 时,我希望结果为 2。(我喜欢啤酒与文件 2 的任何内容都不相同,我也喜欢旅行,stackoverflow 是我喜欢 stackoverflow -1 的一部分) - 最后啤酒和啤酒是一样的 -2-) 正确的方法是什么? 正如我之前所说,无论什么文件是干草堆或给出了针的字符串,包含都会给我相同的结果。
PS 在我的示例中,结果是相同的,但我认为这是随机的。
我怎样才能做到这一点?
static boolean contains(Iterable<String> haystack, String needle) {
for (String s : haystack) {
if (s.contains(needle)) {
return true;
}
}
return false;
}
static void containsAll() throws IOException {
List<String> words = loadLines("opacial.txt");
List<String> tocheck = loadLines("queries0.txt");
System.out.println(words.size());
System.out.println(tocheck.size());
int index2 = 0;
for (String s : tocheck) {
if (contains(words, s)) {
index2++;
//return false;
}
}
System.out.println(index2);
//return true;
}
i am looking a method like contains (code above) that will do this:
it will check if needle exists in the haystack, or if needle is part of a string in haystack.
In that case (the code above) if i reverse the file that goes to haystack, and the file that gives the needle, the result is the same. but i dont want that. for example:
File 1:
i love beers
i like travelling
stackoverflow
beers
And File2 :
beers
i love stackoverflow
then if haystack comes from file 1 and needle comes from file2, i want the result to be 2 because the word beers is part-or the same only with two strings of haystack. (beers ---> i love beers and beers) - nothing happens with i love stackoverflow)
BUT when haystack comes from file2 and needle comes from file1, i want the result to be 2. (i love beers is not part or same with anything of file 2, i like travelling the same, stackoverflow is part of i love stackoverflow -1- and finally beers is same with beers -2-)
what is the correct method for that?
As i said before contains gives me the same result no matter what file is haystack or gives the needle's strings.
PS in my example the result is the same, but i think that is random.
how can i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的意思是这两种情况的值可能应该不同?你表现出它们是一样的。
如果要在另一个字符串中查找一个字符串,请使用 String 对象的 indexOf 方法。例如:
将返回 1。如果该值不存在,则该方法返回 -1。
因此,如果你想大海捞针,就意味着检查一个文件中的每一行是否存在于另一个文件中。请记住,如果文件(以及其中的行)很大,这意味着需要进行大量字符串处理,这可能会很慢。而且你必须在两个方向上都这样做。首先,获取文件 1 中的一行,并将其与文件 2 中的每一行进行比较(除非找到匹配项,在这种情况下,您可以停止在文件 1 中查找该行)。然后移至文件 1 中的下一行,依此类推。
相反,并在文件 1 中查找文件 2 中的第 1 行。
我不会描述所有逻辑,但这部分应该不难理解,假设您知道如何打开文件和编写循环。
I think that you meant that the values should probably be different for the two cases? You show them as being the same.
If you want to find a string within another string, use the String object's indexOf method. For example:
will return 1. If the value is not present, the method returns -1.
So if you want to find a needle in a haystack, it means checking every line one file for the existence of a line in another file. Keep in mind that if the files (and the lines in them) are large, this means a lot of string processing, which can be slow. And you would have to do it in both directions. First, get a line in file 1, and compare it to every line in file 2 (unless you find a match, in which case you can stop looking for the line from file 1). Then move to the next line in file 1, etc.
The reverse, and look for line 1 from file 2 in file 1.
I won't describe all the logic, but that part shouldn't be too hard to figure out, assuming you know how to open files and write loops.