繁体   English   中英

如何使用 map 读取 CSV 文件,其中列表带有 Java 中的分隔元素

[英]How to read the CSV file with a map with lists with separated elements in Java

我不知道为什么我之前的问题已关闭,所以我决定以另一种方式提出问题。 嗨,我是 Java 编程的新手,我在从 CSV 文件中读取数据时遇到了一些问题。 这是我的 CSV 文件:

1,Ali,1201345673,Normal
2,Siti,1307891435,Normal

这是我读取数据的方式:

Map<String, List<Customer>> customers =
    Files.lines(Paths.get("customer.csv"))
            .map(line -> line.split("\\s*,\\s*"))
            .map(field -> new Customer(
                    Integer.parseInt(field[0]), field[1],
                    Integer.parseInt(field[2]), field[3]))
            .collect(Collectors
                   .groupingBy(Customer::getName));
    System.out.println (customers);
    return customers;

客户 class:

public class Customer {
private int customerNo;
private String name;
private int phoneNo;
private String status;
public Customer () {}
public Customer (int customerNo, String name, int phoneNo, String status){
    this.customerNo = customerNo;
    this.name = name;
    this.phoneNo = phoneNo;
    this.status = status;
}
public String getName(){
    return name;
}

public String toString(){
    return customerNo + " " + name + " " + phoneNo + " " + status;
}

然后我意识到这就是我的 map 的样子:

(Ali = ["1 Ali 1201345673 Normal"] , Siti = ["2 Siti 1307891435 Normal"])

但不是我想要的方式:

(Ali = [1 , "Ali" , 1201345673 , "Normal" ] , Siti = [2 , "Siti" , 1307891435 , "Normal" ])

对于阿里来说,1是一个元素,Ali是一个元素,1201345673是一个元素,Normal是另一个元素。

每个人的数据都已读入我的列表中的一个元素,所以我不能像数组列表一样不 output 他们。 So the problem is how can I separate the every element in the list when I read the CSV data and I really need the Map function of the program so please keep it, or maybe how can I read the data as a array list in my map . 感谢您的关注。

那是因为 toString() 方法。 它通过基本上定义自定义格式的字符串来定义 class 将如何打印。

如果您将其更改为如下:

public String toString(){
    return customerNo + ", " + name + ", " + phoneNo + ", " + status;
}

它将打印为:

Ali = ["1, Ali, 1201345673, Normal"]

您可以自定义此 function 以按照您想要的方式打印。

尝试这个:

public String toString(){
    return customerNo + ", \"" + name + "\", " + phoneNo + ", \"" + status + "\"";
}

我相信以下内容可以实现您的愿望。

/*
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
 */
Supplier<List<List<Object>>> s = () -> new ArrayList<List<Object>>();

BiConsumer<List<List<Object>>, Customer> b = (l, c) -> {
            l.add(c.toList());
};

BinaryOperator<List<List<Object>>> o = (l1, l2) -> {l1.addAll(l2); return l1;};

Collector<Customer, List<List<Object>>, List<List<Object>>> c = Collector.of(s, b, o);

Map<String, List<List<Object>>> customers =
        Files.lines(Paths.get("customer.csv"))
             .map(line -> line.split("\\s*,\\s*"))
             .map(field -> new Customer(Integer.parseInt(field[0]),
                                        field[1],
                                        Integer.parseInt(field[2]),
                                        field[3]))
             .collect(Collectors.groupingBy(Customer::getName,
                                            Collectors.mapping(Function.identity(), c)));
System.out.println(customers);

output 是

{Siti=[[2, Siti, 1307891435, Normal]], Ali=[[1, Ali, 1201345673, Normal]]}

我创建了一个自定义Collector ,即我编写了一个Collector接口的实现。

Map中的每个值,即customers ,是一个列表列表。 根据您的示例 CSV 文件,文件中有两行,每行都有一个单独的名称。 因此Map包含两个条目,每个条目的值是一个包含单个元素的列表,该元素本身就是一个列表。 这就是 output 显示双[]字符的原因。

请注意,我将方法toList添加到 class Customer

public class Customer {
    private int customerNo;
    private String name;
    private int phoneNo;
    private String status;

    public Customer() {
    }

    public Customer(int customerNo, String name, int phoneNo, String status) {
        this.customerNo = customerNo;
        this.name = name;
        this.phoneNo = phoneNo;
        this.status = status;
    }

    public String getName() {
        return name;
    }

    public List<Object> toList() {
        return List.of(customerNo, name, phoneNo, status);
    }

    public String toString() {
        return customerNo + " " + name + " " + phoneNo + " " + status;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM