Java 解析两个文件并生成员工数据

Java 解析两个文件并生成员工数据,java,sql,file,bufferedreader,filereader,Java,Sql,File,Bufferedreader,Filereader,假设我们有两个以逗号分隔的日志文件。file1.txt表示员工id和员工姓名,file2.txt表示员工id和与其关联的项目。 file1具有唯一的条目文件2将有许多关系。如果新员工没有分配任何项目,则在file2.txt中没有任何条目 File1.txt:(EmpId, EmpName) 1,abc 2,ac 3,bc 4,acc 5,abb 6,bbc 7,aac 8,aba 9,aaa File2.txt: (EmpId, ProjectId) 1,102 2,102 1,103 3,1

假设我们有两个以逗号分隔的日志文件。
file1.txt
表示
员工id
员工姓名
file2.txt
表示
员工id
和与其关联的
项目。
file1
具有唯一的条目<代码>文件2
将有许多关系。如果新员工没有分配任何项目,则在
file2.txt
中没有任何条目

File1.txt:(EmpId, EmpName)
1,abc
2,ac
3,bc
4,acc
5,abb
6,bbc
7,aac
8,aba
9,aaa

File2.txt: (EmpId, ProjectId)
1,102
2,102
1,103
3,101
5,102
1,103
2,105
2,200
9,102

Find the each employee has been assigned to number of projects. For New employees if they dont have any projects print 0;
Output:
1=3
2=3
3=1
4=0
5=1
6=0
7=0
8=0
9=1
    // Walk the projects list.
    Arrays.stream(file2)
            // Get empId - Split on comma, take the first field and convert to integer (again).
            .map(s -> Integer.valueOf(s.split(",")[0]))
            // Count the projects.
            .forEach(empId -> employeeProjectCount.put(empId, employeeProjectCount.get(empId)+1));
我使用BufferedReader从
file1
中读取一行,并将其与
file2
中的每一行进行比较。下面是我的代码

public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        BufferedReader file1 = new BufferedReader(new FileReader("file1.txt"));
        BufferedReader file2 = new BufferedReader(new FileReader("file2.txt"));
        BufferedReader file3 = new BufferedReader(new FileReader("file2.txt"));
        HashMap<String,Integer> empProjCount = new HashMap<String, Integer>();
        int lines =0;
        while (file2.readLine() != null) 
            lines++;
        String line1 = file1.readLine();
        String[] line_1 = line1.split(",");
        String line2 = file3.readLine();
        String[] line_2 = line2.split(",");
        while(line1 != null && line2 != null)
        {
            int count = 0;
            for(int i=1;i<=lines+1 && line2 != null;i++)
            {
            if(line_1[0].equals(line_2[0]))
            {
                count++;
            }
            line2 = file3.readLine();
            if(line2 != null){
                line_2 = line2.split(",");
            }
            }
            file3 = new BufferedReader(new FileReader("file2.txt"));
            empProjCount.put(line_1[0], count);
            line1 = file1.readLine();
            if(line1 != null) line_1 = line1.split(",");
            line2 = file3.readLine();
            if(line2 != null) line_2 = line2.split(",");
        }
        System.out.println(empProjCount); 
publicstaticvoidmain(字符串[]args)引发IOException{
//TODO自动生成的方法存根
BufferedReader file1=新的BufferedReader(新文件读取器(“file1.txt”);
BufferedReader file2=新的BufferedReader(新文件读取器(“file2.txt”);
BufferedReader file3=新的BufferedReader(新文件读取器(“file2.txt”);
HashMap empProjCount=新HashMap();
int行=0;
while(file2.readLine()!=null)
行++;
字符串line1=file1.readLine();
String[]line_1=line1.split(“,”);
字符串line2=file3.readLine();
String[]line_2=line2.split(“,”);
while(line1!=null&&line2!=null)
{
整数计数=0;
对于(inti=1;i对于1:是

对于2:是:

我将在两次迭代中完成:

  • 迭代ID(文件1)并初始化映射(empId、projectCounter)

  • 迭代项目(file2)并为每行更新(projectCounter++)映射中的相应条目

  • 这样,您将有几乎线性的执行时间(对于file1和file2大小)。

    对于1:是

    对于2:是:

    我将在两次迭代中完成:

  • 迭代ID(文件1)并初始化映射(empId、projectCounter)

  • 迭代项目(file2)并为每行更新(projectCounter++)映射中的相应条目


  • 这样,您将有几乎线性的执行时间(对于文件1和文件2大小)。

    文件1
    中收集所有员工ID的
    映射,并对其进行初始化,以包含项目计数的
    0

        // Build my map of all employees.
        Map<Integer, Integer> employeeProjectCount = Arrays.stream(file1)
                // Get empId - Split on comma, take the first field and convert to integer.
                .map(s -> Integer.valueOf(s.split(",")[0]))
                // Build a Map for the results.
                .collect(Collectors.toMap(
                        // Key is emp ID.
                        empId -> empId,
                        // Value starts at zero.
                        empId -> ZERO
                ));
    
    打印它:

        // Print it.
        System.out.println(employeeProjectCount);
    
    给予

    {1=3,2=3,3=1,4=0,5=1,6=0,7=0,8=0,9=1}

    顺便说一句:我使用的文件是
    String[]
    s

    String[] file1 = {
            "1,abc",
            "2,ac",
            "3,bc",
            "4,acc",
            "5,abb",
            "6,bbc",
            "7,aac",
            "8,aba",
            "9,aaa",};
    String[] file2 = {
            "1,102",
            "2,102",
            "1,103",
            "3,101",
            "5,102",
            "1,103",
            "2,105",
            "2,200",
            "9,102",
    };
    

    文件1
    收集所有员工ID的
    映射图
    ,并对其进行初始化,以包含项目计数的
    0

        // Build my map of all employees.
        Map<Integer, Integer> employeeProjectCount = Arrays.stream(file1)
                // Get empId - Split on comma, take the first field and convert to integer.
                .map(s -> Integer.valueOf(s.split(",")[0]))
                // Build a Map for the results.
                .collect(Collectors.toMap(
                        // Key is emp ID.
                        empId -> empId,
                        // Value starts at zero.
                        empId -> ZERO
                ));
    
    打印它:

        // Print it.
        System.out.println(employeeProjectCount);
    
    给予

    {1=3,2=3,3=1,4=0,5=1,6=0,7=0,8=0,9=1}

    顺便说一句:我使用的文件是
    String[]
    s

    String[] file1 = {
            "1,abc",
            "2,ac",
            "3,bc",
            "4,acc",
            "5,abb",
            "6,bbc",
            "7,aac",
            "8,aba",
            "9,aaa",};
    String[] file2 = {
            "1,102",
            "2,102",
            "1,103",
            "3,101",
            "5,102",
            "1,103",
            "2,105",
            "2,200",
            "9,102",
    };
    

    使用
    文件.line
    和正则表达式:

    Pattern employeePattern = Pattern.compile("(?<id>\\d+),(?<name>\\s+)");
    Set<String> employees = Files.lines(Paths.get("file1.txt"));
        .map(employeePattern::matcher).filter(Matcher::matches)
        .map(m -> m.group("id")).collect(Collectors.toSet());
    
    Pattern projectPattern = Pattern.compile("(?<emp>\\d+),(?<proj>\\d+)");
    Map<String,Long> projects = Files.lines(Paths.get("file2.txt"))
        .map(projectPattern::matcher).filter(Matcher::matches)
        .collect(Collectors.groupingBy(m -> m.group("emp"), Collectors.counting());
    

    使用
    文件.line
    和正则表达式:

    Pattern employeePattern = Pattern.compile("(?<id>\\d+),(?<name>\\s+)");
    Set<String> employees = Files.lines(Paths.get("file1.txt"));
        .map(employeePattern::matcher).filter(Matcher::matches)
        .map(m -> m.group("id")).collect(Collectors.toSet());
    
    Pattern projectPattern = Pattern.compile("(?<emp>\\d+),(?<proj>\\d+)");
    Map<String,Long> projects = Files.lines(Paths.get("file2.txt"))
        .map(projectPattern::matcher).filter(Matcher::matches)
        .collect(Collectors.groupingBy(m -> m.group("emp"), Collectors.counting());
    

    因此,请求代码检查不是最好的地方。如果不使用任何
    sql
    ,为什么要标记它?在sql中,它是一个简单的
    选择emp.EmpId,count(*)从emp left join proj on e.EmpId=proj.EmpId group by emp.EmpId
    Performance:
    n
    是文件1和文件2中的
    m
    记录数,可以在
    O(n*m)
    中使用
    O(1)
    内存执行,也可以在
    O(n+m)
    中使用
    O(n)执行
    memory。我投票决定将这个问题作为离题题题结束,因为它属于on@Stultuske@JimGarrison我没有发布我的代码以供审查。我只是发布它来展示我的方法,我在这方面做了一些工作。我真正的问题是如何以不同的方式处理它。如果它仍然不属于这里,请让我知道。因此,这不是要求代码回顾如果不使用任何
    sql
    ,为什么要标记它?在sql中,它是一个简单的
    选择emp.EmpId,count(*)从emp left join proj上的emp.EmpId=proj.EmpId按emp进行分组。EmpId
    性能:
    n
    是文件1中的记录数,
    m
    是文件2中的记录数,可以在
    O(n*m)中完成
    O(1)
    内存,或在
    O(n+m)
    O(n)
    memory。我投票决定将这个问题作为离题题题结束,因为它属于on@Stultuske@JimGarrison我没有发布我的代码以供审查。我只是发布它来展示我的方法,我在这方面做了一些工作。我真正的问题是如何以不同的方式处理它。如果它仍然不属于这里,请告诉我。