比较两个列表并获得唯一列表(example.txt)java

比较两个列表并获得唯一列表(example.txt)java,java,Java,我在这里找到了一个很好的代码,可以帮助我完成一件事。 但我需要的是: 我有两个列表,一个列表是example1.txt,第二个列表是example2.txt,我需要得到相似和不同的结果。我不知道在哪里设置c://example1.txt和c://example2.txt 例如:Arrays.asList(c://example1.txt) import java.util.Collection; 导入java.util.HashSet; 导入java.util.array; 重复上课{ 公共静态

我在这里找到了一个很好的代码,可以帮助我完成一件事。 但我需要的是: 我有两个列表,一个列表是example1.txt,第二个列表是example2.txt,我需要得到相似和不同的结果。我不知道在哪里设置c://example1.txt和c://example2.txt

例如:Arrays.asList(c://example1.txt)

import java.util.Collection;
导入java.util.HashSet;
导入java.util.array;
重复上课{
公共静态void main(字符串[]args){
Collection listOne=Arrays.asList(“1”、“2”、“3”);
Collection listwo=Arrays.asList(“1”、“2”、“2”);
集合相似=新哈希集(listOne);
集合不同=新HashSet();
不同的.addAll(listOne);
不同的.addAll(列表二);
相似。保留(列表二);
不同。移除所有(相似);
System.out.printf(“一个:%s%n两个:%s%n相似:%s%n不同:%s%n”,列表一,列表二,相似,不同);
}
}

据我所知,您不知道如何将文本文件读取到列表中。有很多选择:

  • IOUtils.readLines(新文件输入流(“c:/example.txt”))
  • 使用
    newbufferedInputStream(newfileInputStream(..)
    然后使用
    readLine()
    方法循环
要正确处理编码,您需要使用新的InputStreamReader(inputStream,“utf-8”)

更新:

您的文件似乎只包含一行。使用Peter提醒我的
FileUtils

String str = FileUtils.readFileToString("c:/filename.txt");
String[] numbers = str.split(",");
List<String> list = Arrays.asList(numbers);
String str=FileUtils.readFileToString(“c:/filename.txt”);
字符串[]数字=str.split(“,”);
列表=数组.asList(数字);

据我所知,您不知道如何将文本文件读取到列表中。有很多选择:

  • IOUtils.readLines(新文件输入流(“c:/example.txt”))
  • 使用
    newbufferedInputStream(newfileInputStream(..)
    然后使用
    readLine()
    方法循环
要正确处理编码,您需要使用新的InputStreamReader(inputStream,“utf-8”)

更新:

您的文件似乎只包含一行。使用Peter提醒我的
FileUtils

String str = FileUtils.readFileToString("c:/filename.txt");
String[] numbers = str.split(",");
List<String> list = Arrays.asList(numbers);
String str=FileUtils.readFileToString(“c:/filename.txt”);
字符串[]数字=str.split(“,”);
列表=数组.asList(数字);

听起来像是家庭作业,但我会咬一口。我会使用FileUtils

Set<String> example1 = new LinkedHashSet<String>(
                               FileUtils.readLines(new File(args[0])));
Set<String> example2 = new LinkedHashSet<String>(
                               FileUtils.readLines(new File(args[1])));
Set<String> same = new LinkedHashSet<String>(example1);
same.retainAll(example2);
Set<String> different = new LinkedHashSet<String>(example1);
different.removeAll(example2);

听起来像是家庭作业,但我会咬人的。我会使用FileUtils

Set<String> example1 = new LinkedHashSet<String>(
                               FileUtils.readLines(new File(args[0])));
Set<String> example2 = new LinkedHashSet<String>(
                               FileUtils.readLines(new File(args[1])));
Set<String> same = new LinkedHashSet<String>(example1);
same.retainAll(example2);
Set<String> different = new LinkedHashSet<String>(example1);
different.removeAll(example2);

引用:“我不知道在哪里设置c://example1.txt和c://example2.txt,例如:Arrays.asList(c://example1.txt);”-这是我能提取的唯一问题。我在这个文件中有一个ex1.txt文件,我有1,2,3,4,5,在第二个文件中我有ex2.txt文件,我有2,3,4,5,现在我需要得到这两个文件之间的相似和不同之处。引用:“我不知道在哪里设置c://example1.txt和c://example2.txt,例如:Arrays.asList(c://example1.txt);”-这是我能提取的唯一问题。我在这个文件中有一个ex1.txt文件,我有1,2,3,4,5,在第二个文件中我有ex2.txt文件,我有2,3,4,5,现在我需要得到这两个文件之间的相似和不同之处。你有什么问题?从文件中读取列表?我的问题是File1.txt 1 2 3 4 5 File2.txt 2 3 4我需要得到相似和不同的信息相似=2,3,4不同=1,5你有什么问题?从文件中读取列表?我的问题是File1.txt 1 2 3 4 5 File2.txt 2 3 4我需要获得相似和不同的信息相似=2,3,4 different=1,5