Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
两个字符串文件的交集-java_Java_String_Intersection - Fatal编程技术网

两个字符串文件的交集-java

两个字符串文件的交集-java,java,string,intersection,Java,String,Intersection,我有两个带字符串行的文件,我必须找到属于这两个文件的行数 我怎么做 例如— 文件1: 文件2: bbb eee aaa 结果应该是2您将文件1中的所有行读取到集合中,然后检查它是否已经包含文件2中的行: Set<String> linesFile1 = ... read in your lines for (String line : file2) { // add each line from file2 and check if it was already in the

我有两个带字符串行的文件,我必须找到属于这两个文件的行数

我怎么做

例如— 文件1:

文件2:

bbb
eee
aaa

结果应该是2

您将文件1中的所有行读取到
集合中,然后检查它是否已经包含文件2中的行:

 Set<String> linesFile1 = ... read in your lines
 for (String line : file2) {  // add each line from file2 and check if it was already in the set
    if (linesFile1.contains(line)) {
         counter++;
    }
 }
设置行文件1=。。。读你的台词
对于(字符串行:file2){//添加file2中的每一行,并检查它是否已在集合中
if(行文件1.contains(行)){
计数器++;
}
}
使用
保留(..)
交叉口操作

 Set<String> aSet=new HashSet(Arrays.asList("aaa", "bbb", "ccc"));
 Set<String> bSet=new HashSet(Arrays.asList( "bbb", "eee","aaa"));

 aSet.retainAll(bSet);//This will do the intersection operation between two Set

 System.out.println(aSet.size());
setaset=newhashset(Arrays.asList(“aaa”、“bbb”、“ccc”);
Set bSet=newhashset(Arrays.asList(“bbb”、“eee”、“aaa”);
aSet.保留(bSet)//这将在两个集合之间执行相交操作
System.out.println(aSet.size());
基本算法

1. Read all lines from file1 and add it into Set1
2. Read all lines from file2 and add it into Set2
3. Then use Guava's intersection method to obtain a new intersection Set
4. Print out the size of the intersection set.

.

运行第一个文件,将所有行放入列表中。然后运行第二个文件并使用containsfunction@GroundZero如果文件的行数超过100行,则
的性能将显著提高。无论他使用什么方法保存行。在运行第二个文件count时,我主要是指contains函数。恕我直言,用于包含行的内容并不重要。对于1000行文件,它的重要性至少是200倍。为什么要添加()而不是包含()?如果他使用add()并且文件2中有重复的行,IMHO将给出不正确的结果。你是对的!我修好了;-)你能给我解释一下这行吗:Set lines文件1=。。。“读入您的行”是指您读入第一个文件中的所有行,并将其放入
集中!linesFile1.包含(行)
?它不应该是
行文件1.contains(行)
?在交集中,我们正在考虑公共元素,不是吗?只有一组第一个文件更有效。
1. Read all lines from file1 and add it into Set1
2. Read all lines from file2 and add it into Set2
3. Then use Guava's intersection method to obtain a new intersection Set
4. Print out the size of the intersection set.