Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

java - Searching data from arraylist

I am a newbie of c++. Now I am doing a project need to read a customer list from a csv file and then search if there is a username like "Ali" and printout all the data about Ali. How can I search "Ali" and printout all the data about Ali like CustomerNo , Name , PhoneNo and Status? And if there is multiple data with "Ali" , how can I printout all of them either? Here is my code:

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

Here is my Customer class:(added 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;
}
}

And here is my data:

 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

Thank you very much for your attention. Edited : Here is my code for this program:

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

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Bit of a broad question.

If you expect to do this a lot, and on a boatload of data, do what everybody else does when they are faced with a lot of relational data that they need to run queries on. Use a database, like postgres or h2. To interact with those from java, use JDBI or JOOQ.

If this is just a small simple text file and/or you're trying to learn some java, well, you still have two options here: You can loop through the data, or, you can build a mapping.

The loop option is simple:

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

But this does, of course, require a full run through all the entries every time. Another option is to build a mapping:

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

maps have the advantage that they are near instant lookup. Even if the map contains a million entries, map.get(x) is (near) instant. A decent solution if you have lots of data + the need to do lots of lookups. But, you have to build a complete map for everything you care to query on. So, if you want to do lookups on name, and then later something like 'get all customers with a 6 digit phone number whose status is Normal', then, get a database.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...