如何读取多个CSV文件以Java中的数据排序?

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

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

第二个CSV文件具有“国家/地区”,“位置”,“洲”,“人口_年”,“人口”格式。

我想做的是读取这两个数据,运行一个函数来计算大洲数据,并提出大洲(string),total_cases(int)并按照从高到低的顺序对其进行排序。然后打印整个东西

输出示例:

大陆(大陆,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);
    }
}
评论
  • non_at
    non_at 回复

    The continent at position 3 and total at position 5, read the first file and get the total cases and I guess every row in the first file related to the same row in the second file.

    然后创建包含大洲和与该大洲相关的数字的地图,并根据总案例进行排序

     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;
        }