import java.io.*;
import java.sql.Time;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
public static void main(String[] args) throws InterruptedException, FileNotFoundException {
String filepath1 = "src/file1.txt";
String filepath2 = "src/file2.txt";
Map<Integer,String[]> hashmap1 = readfile1(filepath1);
Map<Integer,String> hashmap2 = readfile2(filepath2);
Map<Integer,String[]> hashmap3 = new HashMap<>();
for(Map.Entry<Integer,String[]> entry1 : hashmap1.entrySet()){
for(Map.Entry<Integer,String> entry2:hashmap2.entrySet()){
if(entry1.getKey().equals(entry2.getKey())){
String[] res = new String[]{"","",""};
res[0] = entry1.getValue()[0];
res[1] = entry1.getValue()[1];
res[2] = entry2.getValue();
hashmap3.put(entry1.getKey(),res);
}
}
}
for(Map.Entry<Integer,String[]> entry:hashmap3.entrySet()){
System.out.print(entry.getKey()+" ");
for(String s : entry.getValue()){
System.out.print(s+" ");
}
System.out.println();
}
}
public static Map<Integer,String[]> readfile1(String filepath) throws FileNotFoundException {
Map<Integer,String[]> hashmap = new HashMap<>();
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(filepath))){
String line = bufferedReader.readLine();
while((line = bufferedReader.readLine())!=null){
System.out.println("line "+line);
String[] temp = line.split(",");
Integer id = Integer.valueOf(temp[0]);
String name = temp[1];
String age = temp[2];
hashmap.put(id,new String[]{name,age});
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return hashmap;
}
public static Map<Integer,String> readfile2(String filepath) throws FileNotFoundException {
Map<Integer,String> hashmap = new HashMap<>();
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(filepath))){
String line = bufferedReader.readLine();
while((line = bufferedReader.readLine())!=null){
System.out.println("line "+line);
String[] temp = line.split(",");
Integer id = Integer.valueOf(temp[0]);
String salary = temp[1];
hashmap.put(id,salary);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return hashmap;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义