Java 当字符串中有逗号时,找不到两个字符串数组的正确交集
我有两个CSV文件:“userfeatures”和“itemfeatures”。 userfeature中的每一行都与特定的用户相关。e、 例如,userfeature文件中的第一行是:Java 当字符串中有逗号时,找不到两个字符串数组的正确交集,java,arrays,string,comma,set-intersection,Java,Arrays,String,Comma,Set Intersection,我有两个CSV文件:“userfeatures”和“itemfeatures”。 userfeature中的每一行都与特定的用户相关。e、 例如,userfeature文件中的第一行是: 005c2e08”、“行动”、“nm0000148”、“指令nm0764316”、“美国” 我需要找到这一行与第二个文件“itemfeatures”的每一行的交点。(实际上,我需要对所有用户重复这个过程,即对“userfeatures”的所有行重复这个过程) 因此,第一个比较将与第一行“itemfeatures
005c2e08”、“行动”、“nm0000148”、“指令nm0764316”、“美国”
我需要找到这一行与第二个文件“itemfeatures”的每一行的交点。(实际上,我需要对所有用户重复这个过程,即对“userfeatures”的所有行重复这个过程)
因此,第一个比较将与第一行“itemfeatures”进行比较,即:
“tt0306047”、“喜剧、动作”、“nm0267506、nm0000221、nm0356021”、“dir_uNM0001878”、“美国”
交叉点的结果应该是[“Action”,“USA]”
,但不幸的是,我的代码只找到了匹配的[“USA”]。以下是我迄今为止尝试的内容:
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader userfeatures = new BufferedReader(new FileReader("userFeatureVectorsTest.csv"));
BufferedReader itemfeatures = new BufferedReader(new FileReader("ItemFeatureVectorsTest.csv"));
ArrayList<String> userlines = new ArrayList<>();
ArrayList<String> itemlines = new ArrayList<>();
String Uline = null;
while ((Uline = userfeatures.readLine()) != null) {
for (String Iline = itemfeatures.readLine(); Iline != null; Iline = itemfeatures.readLine()) {
System.out.println(Uline);
System.out.println(Iline);
System.out.println(intersect(Uline, Iline));
System.out.println(union(Uline, Iline));
}
}
userfeatures.close();
itemfeatures.close();
}
static Set<String> intersect(String Uline, String Iline) {
Set<String> result = new HashSet<String>(Arrays.asList(Uline.split(",")));
Set<String> IlineSet = new HashSet<String>(Arrays.asList(Iline.split(",")));
result.retainAll(IlineSet);
return result;
}
static Set<String> union(String Uline, String Iline) {
Set<String> result = new HashSet<String>(Arrays.asList(Uline.split(",")));
Set<String> IlineSet = new HashSet<String>(Arrays.asList(Iline.split(",")));
result.addAll(IlineSet);
return result;
}
}
公共类主{
公共静态void main(字符串[]args)引发异常{
BufferedReader userfeatures=new BufferedReader(新文件阅读器(“userFeatureVectorsTest.csv”);
BufferedReader itemfeatures=new BufferedReader(新文件阅读器(“ItemFeatureVectorsTest.csv”);
ArrayList userlines=新的ArrayList();
ArrayList itemlines=新的ArrayList();
字符串Uline=null;
而((Uline=userfeatures.readLine())!=null){
对于(字符串Iline=itemfeatures.readLine();Iline!=null;Iline=itemfeatures.readLine()){
系统输出println(Uline);
系统输出打印(Iline);
System.out.println(intersect(Uline,Iline));
系统输出println(union(Uline,Iline));
}
}
userfeatures.close();
itemfeatures.close();
}
静态集合相交(字符串Uline、字符串Iline){
Set result=newhashset(Arrays.asList(Uline.split(“,”));
Set-IlineSet=newhashset(Arrays.asList(Iline.split(“,”));
结果:保留(IlineSet);
返回结果;
}
静态集合并集(字符串Uline、字符串Iline){
Set result=newhashset(Arrays.asList(Uline.split(“,”));
Set-IlineSet=newhashset(Arrays.asList(Iline.split(“,”));
结果:addAll(IlineSet);
返回结果;
}
}
我认为这个问题与
非常感谢,
令牌。尝试删除两个字符串中的双引号 因为当你分手的时候 “tt0306047”,“喜剧,动作”,“nm0267506,nm0000221,nm0356021”,“导演”_ nm0001878,“美国” 你会得到一份工作 行动” 令牌,它将永远不会匹配 “行动”
令牌。如果打印行,它是什么样子的?我认为您的问题在于读取文件,例如: “005c2e08”、“行动”、“nm0000148”、“指令nm0764316”、“美国” 按“,”拆分将导致: “005c2e08” “行动” 等等。而对于第二行,它将是: “tt0306047” “喜剧 行动” 这就是为什么美国在拦截,但行动不是 使用csv读取器读取csv文件,然后用逗号分割csv行的属性。这样,您就可以去掉QUOUTE,代码也可以正常工作 例如,此库对于读取CSV文件非常方便:
如果打印行,它是什么样子的?我认为您的问题在于读取文件,例如: “005c2e08”、“行动”、“nm0000148”、“指令nm0764316”、“美国” 按“,”拆分将导致: “005c2e08” “行动” 等等。而对于第二行,它将是: “tt0306047” “喜剧 行动” 这就是为什么美国在拦截,但行动不是 使用csv读取器读取csv文件,然后用逗号分割csv行的属性。这样,您就可以去掉QUOUTE,代码也可以正常工作 例如,此库对于读取CSV文件非常方便:
因为当你用
拆分时,
你会得到“Action”
和Action”
。因为当你用拆分时,
你会得到“Action”
和Action”
。谢谢你的回答。很抱歉,我对Java是一个新手。我应该用行=行吗?替换(“\”,”)代码>?谢谢你的回答。对不起,我对Java还是个新手。我应该使用line=line.replace(“\”,”)
?不用担心。对于您的示例,它应该是开箱即用的。否则,您可以告诉opencsv您的分隔符和您的报价是什么。这样,它将把逗号作为CSV的内部报价,而外部报价作为分隔符。他们的网站上有一个很好的示例。非常感谢您的帮助。我简单地就得到了答案卸下““从两个字符串:)不用担心。对于你的例子来说,它应该是开箱即用的。否则,您可以告诉opencsv您的分隔符和报价是什么。这样,它将在引号内使用逗号,在引号外使用逗号作为CSV的分隔符。他们的网站上有一个很好的例子。非常感谢你的帮助。我通过简单地从两个字符串中删除“”得到了答案:)