Loading

[Java手撕]读取文件并进行left join操作

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;
    }
}
posted @   Duancf  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示