gpt4 book ai didi

java - 如何结合扫描仪和多重 map ?

转载 作者:行者123 更新时间:2023-11-30 03:53:36 26 4
gpt4 key购买 nike

我有两个文件,正在尝试使用扫描仪逐行读取每个文件。另外,我想通过使用多重映射将这两个文件与相同的键(名称)组合起来,以便将这两个文件合并为一个。这是我到目前为止的脚本。有人可以给我建议吗?谢谢。

001.csv 包含:

David 188 Male doctor A  
Jacob 190 Male CEO A+
Sam 175 Male Engineer A-

002.txt 包含:

David 80kg US3000  
Jacob 70kg US100000
Sam 65kg US80000

源代码:

public class same_test{
public static void main (String[] args) throws FileNotFoundException {
MultiMap multiMap = new MultiValueMap();
Scanner scanner1 = new Scanner(new File("001.csv"));
Scanner scanner2 = new Scanner(new File("002.txt"));
while (scanner1.hasNextLine()) {
String line = scanner1.nextLine();
String[] array = line.split("\t",2);
String TheName = array[0];
String score = array[1];
multiMap.put(TheName,score);
}
while (scanner2.hasNextLine()) {
String line2 = scanner2.nextLine();
String[] array2 = line2.split("\t",2);
String TheName2 = array2[0];
String rs = array2[1];
multiMap.put(TheName2,rs);
}
Set<String> keys = multiMap.keyset();
for (String key : keys){
System.out.println(key + "\t" + multiMap.get(key) );
}
}
}

最佳答案

请分享您遇到的问题或您得到的输出。我运行你的代码,它对我有用。我使用了MultiHashMap。

public static void main (String[] args) throws FileNotFoundException {
MultiMap multiMap = new MultiHashMap();
Scanner scanner1 = new Scanner(new File("/obp/f1.csv"));
Scanner scanner2 = new Scanner(new File("/obp/f2.csv"));
while (scanner1.hasNextLine()) {
String line = scanner1.nextLine();
String[] array = line.split("\\s",2);
String TheName = array[0];
String score = array[1];
multiMap.put(TheName,score);
}
while (scanner2.hasNextLine()) {
String line2 = scanner2.nextLine();
String[] array2 = line2.split("\\s",2);
String TheName2 = array2[0];
String rs = array2[1];
multiMap.put(TheName2,rs);
}
Set<String> keys = multiMap.keySet();
for (String key : keys){
System.out.println(key + "\t" + multiMap.get(key) );
}
}

输出

David   [188 Male doctor A  , 80kg US3000  ]
Jacob [190 Male CEO A+ , 70kg US100000 ]
Sam [ 175 Male Engineer A- , 65kg US80000 ]

关于java - 如何结合扫描仪和多重 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23806825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com