Java Hashmap / Arraylist计算不同的值

问题描述 投票:1回答:1

我是编程的新手,我有一个任务要做,但我卡住了。

我必须实现一个程序,它将读取一个CSV文件(100万+行)并计算在特定日期订购“x”个不同产品的客户数量。

CSV看起来像这样:

Product Name | Product ID | Client ID |  Date
Name              544           86       10/12/2017
Name              545           86       10/12/2017
Name              644           87       10/12/2017
Name              644           87       10/12/2017
Name              9857          801      10/12/2017
Name              3022          801      10/12/2017
Name              3021          801      10/12/2017

我的代码的结果是:

801:2 - 不正确

86:2 - 正确

87:2 - 不正确

期望的输出是:

客户1(801):3种不同的产品

客户2(86):2种不同的产品

客户3(87):1个不同的产品

另外,

  • 如果我想知道有多少客户订购了2种不同的产品,我希望结果如下:

总计:1位客户订购了2种不同的产品

  • 如果我想知道一天内订购的不同产品的最大数量,我希望结果如下:

订购的不同产品的最大数量为:3

我尝试使用Google Guava的Hash Map和Multimap(我最好的猜测),但我无法绕过它。

我的代码看起来像这样:

package Test;

import java.io.BufferedReader;
import java.io.FileReader;    
import java.io.IOException;

import java.util.ArrayList;   
import java.util.HashMap;    
import java.util.Map;  

import com.google.common.collect.ArrayListMultimap;  
import com.google.common.collect.HashMultimap;    

public class Test {

    public static void main(String[] args) {
        //HashMultimap<String, String> myMultimap = HashMultimap.create();
        Map<String, MutableInteger> map = new HashMap<String, MutableInteger>();
        ArrayList<String> linesList = new ArrayList<>();
        // Input of file which needs to be parsed
        String csvFile = "file.csv";
        BufferedReader csvReader;

        // Data split by 'TAB' in CSV file
        String csvSplitBy = "\t";
        try {
            // Read the CSV file into an ArrayList array for easy processing.
            String line;
            csvReader = new BufferedReader(new FileReader(csvFile));
            while ((line = csvReader.readLine()) !=null) {
                linesList.add(line);
            }
            csvReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } 

        // Process each CSV file line which is now contained within
        // the linesList list Array
        for (int i = 0; i < linesList.size(); i++) {
            String[] data = linesList.get(i).split(csvSplitBy);
            String col2 = data[1];
            String col3 = data[2];
            String col4 = data[3];

            // Determine if Column 4 has the desired date
            // and count the values
            if (col4.contains("10/12/2017"))  {
                String key = col3;
                if (map.containsKey(key)) {
                      MutableInteger count = map.get(key);
                      count.set(count.get() + 1);
                } else {
                      map.put(key, new MutableInteger(1));
                }
            }
        }

        for (final String k : map.keySet()) {
            if (map.get(k).get() == 2) {
              System.out.println(k + ": " + map.get(k).get());
            }
        }
    }
}

任何关于如何实施这一建议的建议或建议将不胜感激。

提前谢谢你们。

java csv arraylist count hashmap
1个回答
1
投票

你可以存储Setof productIdsclientId,并采取它的大小。由于Set不允许重复值,这将有效地为您提供不同数量的productIds

此外,我建议您为变量提供有意义的名称而不是col2,k,map ...这将使您的代码更具可读性。

Map<String, Set<String>> distinctProductsPerClient = new HashMap<String, Set<String>>();
// Process each CSV file line which is now contained within
// the linesList list Array
// Start from 1 to skip the first line
for (int i = 1; i < linesList.size(); i++) {
    String line = linesList.get(i);
    String[] data = line.split(csvSplitBy);
    String productId = data[1];
    String clientId = data[2];
    String date = data[3];

    // Determine if Column 4 has the desired date
    // and count the values
    if (date.contains("10/12/2017"))  {
        if (!distinctProductsPerClient.containsKey(clientId)) {
            distinctProductsPerClient.put(clientId, new HashSet<>());
        }
        distinctProductsPerClient.get(clientId).add(productId);

    }
}

for (final String clientId : distinctProductsPerClient.keySet()) {
    System.out.println(clientId + ": " + distinctProductsPerClient.get(clientId).size());
}

More advanced solution using Stream API (requires Java 9)

如果你引入类OrderData(代表CSV中的单行),就像这样:

private static class OrderData {

    private final String productName;
    private final String productId;
    private final String clientId;
    private final String date;

    public OrderData(String csvLine) {

        String[] data = csvLine.split("\t");

        this.productName = data[0];
        this.productId = data[1];
        this.clientId = data[2];
        this.date = data[3];

    }

    public String getProductName() {
        return productName;
    }

    public String getProductId() {
        return productId;
    }

    public String getClientId() {
        return clientId;
    }

    public String getDate() {
        return date;
    }
}

你可以用这个替换for循环:

Map<String, Set<String>> distinctProductsPerClient2 = linesList.stream()
        .skip(1)
        .map(OrderData::new)
        .collect(groupingBy(OrderData::getClientId, mapping(OrderData::getProductId, toSet())));

但是我认为如果你是新手编程的话,这可能有点复杂(尽管如果你试图理解上面代码的作用,这可能是一个很好的练习)。

© www.soinside.com 2019 - 2024. All rights reserved.