首页 > 解决方案 > 如何读取多个 CSV 文件以在 Java 中对数据进行排序?

问题描述

因此,我的 Java 项目有两个 CSV 文件,它们需要从高到低读取和打印某种数据。

第一个 CSV 文件具有日期、位置、new_cases、new_deaths、total_cases、total_deaths 格式。

第二个 CSV 文件具有国家、位置、大陆、人口年、人口格式。

我要做的是读取这两个数据,运行一个函数来计算大陆数据,并提出大陆(字符串),total_cases(int)并根据高到低对其进行排序。然后打印整个东西

示例输出:

大陆 (continent, csv2): ------ 病例总数:(total_cases,csv1)

大洋洲 123456

澳大利亚 12345

欧洲 123

到目前为止我写的代码包括在下面,请帮助我推导和排序数据。

import java.io.FileReader;
import java.io.*;
import java.util.Scanner;
import java.util.* ;
public class Main {

    static void loadData() {

        String pathFile1 = "./locations.csv";
        String pathFile2 = "./full_data.csv";
        String row;
        try {

            BufferedReader csvReader = new BufferedReader(new FileReader(pathFile1));

            while ((row = csvReader.readLine()) != null) {
              System.out.println(row);
            }
            csvReader.close();


            csvReader = new BufferedReader(new FileReader(pathFile2));

            while ((row = csvReader.readLine()) != null) {
                System.out.println(row);

            }
            csvReader.close();


        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    private static void add() {
    }

    public static void main(String[] args) {


        Scanner scnr = new Scanner(System.in);
        int choice = -1;
        System.out.println("*************************************************************");
        System.out.println("**** COVID 19 Global Statistics *****************************");
        System.out.println("*************************************************************");
        do {
            System.out.println("[1] Load Data From Files");
            System.out.println("[2] Print Continents Total Cases (Lowest to Highest)");
            System.out.println("[3] Print Continents Total Cases (Highest to Lowest)");
            System.out.println("[4] Print Continents Total Deaths (Lowest to Highest)");
            System.out.println("[5] Print Continents Total Deaths (Highest to Lowest)");
            System.out.println("[6] Prioritize top countries for testing based on new cases per 1 million");
            System.out.println("[7] To Exit");
            System.out.println("Please enter your choice:");
            choice = scnr.nextInt();
            Map<String, Integer> hm1;
            switch (choice) {
                case 1:
                    System.out.println("Loading files ...");
                    loadData();
                    System.out.println("Files loaded successfully!");
                    break;
                case 2:
                    break;
                case 3:

                    break;
                case 4:
                    break;
                case 5:

                var requestedNum = scnr.nextInt();
                System.out.println("Continent:        Total Cases:");

                    break;
                case 6:
                System.out.println("How many countries to pull from the priorities list:");
                    break;
                case 7:
                    System.out.println("Thank you for using our system..Goodbye!");
                    break;
                default:
                    System.out.println("Please a choice 1 - 7");
                    break;
            }
        } while (choice != 7);
    }
}

标签: javadata-structures

解决方案


continent位置 3 和total位置 5,读取第一个文件并获取total cases,我猜第一个文件中的每一行都与第二个文件中的同一行相关。

然后创建一个包含与该大陆相关的大陆和编号的地图,并根据总病例数进行排序

 public static Map<String, Long> loadData() {
        String pathFile1 = "./locations.csv";
        String pathFile2 = "./full_data.csv";
        String row;
        try {

            BufferedReader csvReader = new BufferedReader(new FileReader(pathFile1));
            List<Long> totalCases = new ArrayList<>();
            while ((row = csvReader.readLine()) != null) {
                String[] split = row.split(",");
                totalCases.add(Long.parseLong(split[4])); // total at position 5
            }
            csvReader.close();

            csvReader = new BufferedReader(new FileReader(pathFile2));
            Map<String, Long> map = new LinkedHashMap<>();
            int i = 0;
            while ((row = csvReader.readLine()) != null) {
                String[] split = row.split(",");
                String continent = split[2]; // continent at position 3
                map.put(continent, map.getOrDefault(continent, 0L) + totalCases.get(i));
                i++;
            }
            csvReader.close();

            return map.entrySet().stream().sorted(Entry.<String, Long>comparingByValue().reversed())
                    .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

推荐阅读