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;
}
}
标签:Map,join,String,java,import,new,Java,line,left
From: https://www.cnblogs.com/DCFV/p/18442141