从arraylist搜索数据

2fjabf4q  于 2021-06-26  发布在  Java
关注(0)|答案(2)|浏览(266)

我是c++的新手。现在我正在做一个项目,需要从一个csv文件中读取一个客户列表,然后搜索是否有像“ali”这样的用户名,并打印出有关ali的所有数据。如何搜索“阿里”并打印出所有关于阿里的数据,如客户编号、姓名、电话号码和状态?如果有多个带有“ali”的数据,我又怎么能把它们全部打印出来呢?这是我的密码:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Iterator;

public class LoadCustomer {
    public static void main(String[] args) throws IOException{
        System.out.println ("Load customer from file");
        ArrayList<Customer> customers = readCustomerFromFile();

        System.out.println (customers);
        System.out.println ();

    private static ArrayList<Customer> readCustomerFromFile() throws IOException{
        ArrayList<Customer> customers = new ArrayList<>();
        List<String> lines = Files.readAllLines(Paths.get("customer.csv"));
        for (int i = 1 ; i < lines.size() ; i++){
            String[] items = lines.get(i).split(",");
            int customerNo = Integer.parseInt(items[0]);
            int phoneNo = Integer.parseInt(items[2]);
            customers.add (new Customer(customerNo,items[1],phoneNo,items[3]));
        }
        return customers;
    }
}

这是我的客户类:(添加getname getter)

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;
}
public String toCSVString(){
    return customerNo + "," + name + "," + phoneNo + "," + status;
}
}

这是我的数据:

CustomerNo            Name       PhoneNo     Status
    1                  Ali        12345        Normal
    2                  Siti       23456        Normal
    3                  Rone       78910        Normal
    4                  Jean       56789        Normal
    5                  Roby       28573        Normal
    6                  Ali        78532        Normal

非常感谢您的关注。编辑:这是我的程序代码:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

public class FindCustomer {
    public static void main(String[] args) throws IOException{
        System.out.println ("Load customer from file");
        java.util.Map<String, List<Customer>> customers =
        Files.lines(Paths.get("customer.csv"))
                .map(line -> line.split(","))
                .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);
    }

}

tvmytwxo

tvmytwxo1#

正如有人建议的那样,一张Map会很有用。您可以在读取文件时动态创建一个。
分割线条
创建客户。
在Map上按名字分组。
现在,Map将保留每个名称,所有具有该名称的客户。

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

获取客户名单 Ali 执行以下操作。

List<Customer> ali = customers.get("Ali");

现在,您可以根据需要格式化或以其他方式使用列表。您仍然需要通过try/catch处理异常。

c3frrgcw

c3frrgcw2#

有点宽泛的问题。
如果您希望经常这样做,并且在一船数据上这样做,那么当其他人面临大量需要在其上运行查询的关系数据时,您可以像其他人一样这样做。使用数据库,如postgres或h2。要与来自java的用户交互,请使用jdbi或jooq。
如果这只是一个小的简单文本文件和/或您正试图学习一些java,那么,这里仍然有两个选项:您可以在数据中循环,或者,您可以构建一个Map。
循环选项很简单:

for (Customer c : customers) if (c.getName().equals("Ali")) {
   // do what you want here. 'c' holds the customer object of Ali.
}

当然,这确实需要每次对所有条目进行一次完整的检查。另一种选择是构建Map:

var map = new HashMap<String, Customer>();
for (Customer c : customers) map.put(c.getName(), c);
// map now maps a customer name to the customer object.
Customer ali = map.get("Ali");

Map的优点是它们几乎可以即时查找。即使Map上有一百万个条目, map.get(x) 是(几乎)瞬间的。一个不错的解决方案,如果你有大量的数据+需要做大量的查找。但是,你必须为你想要查询的所有东西建立一个完整的Map。所以,如果您想查找name,然后稍后执行类似“获取所有状态正常的6位电话号码的客户”之类的操作,那么就获取一个数据库。

相关问题