如何在列表中按属性搜索?java

np8igboo  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(268)

这个问题在这里已经有答案了

如何按属性在arraylist中查找对象(7个答案)
上个月关门了。
我有一个程序,它正在读取作为参数发送的文件中的命令。例如,它读取一个名为 CreatePersonel U P001 Ali Poyraz 6 并用这些信息创建了一个工作人员( U = UnQualified Personal , P001 = ID , Ali = Name , Poyraz = Surname , 6 = workhour )创建此人员后,我将其保存在列表中。当程序读取如下命令时 ShowPersonnel P001 ,如何找到具有该id的元素?提前谢谢。如果你需要,这是我的密码:
驾驶员等级:

import Employee.Employee;
import Employee.QualifiedEmployee;
import Employee.UnQualifiedEmployee;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class Driver {
    public static void main(String[] args) throws FileNotFoundException {
        long start = System.currentTimeMillis();
        int workhour;

        List<Employee> employees = new ArrayList<Employee>();

        Scanner scan = new Scanner(new File(args[0]));
        while (scan.hasNextLine()) {
            String data = scan.nextLine();
            String[] datas = data.split("\\s+");
            int y = 0;
            try {
                while(datas[y] != null){
                    String command = datas[y];

                    switch(command){
                        case "CreatePersonel":
                            if(datas[y+1].equals("U")){
                                Employee newEmployee = new UnQualifiedEmployee(datas[y+2],datas[y+3],datas[y+4],workhour = Integer.parseInt(datas[y+5]));
                                employees.add(newEmployee);
                                System.out.println("Personal Created. ID: "+newEmployee.getId());
                                break;
                            }
                            else{
                                Employee newEmployee = new QualifiedEmployee(datas[y+2],datas[y+3],datas[y+4],datas[y+5]);
                                employees.add(newEmployee);
                                System.out.println("Personal Created. ID: "+newEmployee.getId());
                                break;
                            }

                        case "ShowPersonel":
                            String searchVal = datas[y+1];
                            System.out.println("ShowPersonel");         //Will be written...
                            break;

                        case "CreateVehicle":
                            System.out.println("CreateVehicle");        //Will be written...
                            break;

                        case "ShowVehicle":
                            System.out.println("ShowVehicle");          //Will be written...
                            break;

                        case "AssignPersonel":
                            System.out.println("AssignPersonel");       //Will be written...
                            break;

                        case "MoveToStartPoint":
                            System.out.println("MoveToStartPoint");     //Will be written...
                            break;

                        case "TransferCargo":
                            System.out.println("TransferCargo");        //Will be written...
                            break;

                        case "CanTransferCargo":
                            System.out.println("CanTransferCargo");     //Will be written...
                            break;

                        case "UseFireArm":
                            System.out.println("UseFireArm");           //Will be written...
                            break;

                        case "CanUseFireArm":
                            System.out.println("CanUseFireArm");        //Will be written...
                            break; 

                        case "Transport":
                            System.out.println("Transport");            //Will be written...
                            break;

                        case "CanTransport":
                            System.out.println("CanTransport");         //Will be written...
                            break;

                        case "EmergencyAction":
                            System.out.println("EmergencyAction");      //Will be written...
                            break;

                        case "CanEmergencyAction":
                            System.out.println("CanEmergencyAction");   //Will be written...
                            break;
                    }

                    break;
                }
            }
            catch(Exception ignored) {
            }
        }

        long end = System.currentTimeMillis();
        float sec = (end - start) / 1000F;
        System.out.println("Program executed in "+sec+" seconds.");
    }
}

员工类别:

package Employee;

public class Employee {
    private String id;
    private String name;
    private String surname;

    public Employee(String id, String name, String surname) {
        this.id = id;
        this.name = name;
        this.surname = surname;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public void info(){
        System.out.println("Name: "+this.getName()+" Surname: "+this.getSurname()+" ID: "+this.getId()+" Type: Employee");
    }
}

不合格员工类别:

package Employee;

public class UnQualifiedEmployee extends Employee{
    int workHour;

    public UnQualifiedEmployee(String id, String name, String surname, int workHour) {
        super(id, name, surname);
        this.workHour = workHour;
    }

    public int getWorkHour() {
        return workHour;
    }

    public void setWorkHour(int workHour) {
        this.workHour = workHour;
    }

    @Override
    public void info() {
        System.out.println("Name: "+this.getName()+" Surname: "+this.getSurname()+" ID: "+this.getId()+" Type: UnQualified Employee");
    }
}

合格员工类别:

package Employee;

public class QualifiedEmployee extends Employee{
    private String lisanceNumber;

    public QualifiedEmployee(String id, String name, String surname, String lisanceNumber) {
        super(id, name, surname);
        this.lisanceNumber = lisanceNumber;
    }

    public String getLisanceNumber() {
        return lisanceNumber;
    }

    public void setLisanceNumber(String lisanceNumber) {
        this.lisanceNumber = lisanceNumber;
    }

    @Override
    public void info() {
        System.out.println("Name: "+this.getName()+" Surname: "+this.getSurname()+" ID: "+this.getId()+" Type: Qualified Employee");
    }
}

我不知道你是否需要在我的项目其他类,但如果你需要告诉我,我会分享。

oymdgrw7

oymdgrw71#

我建议你做一张有身份证和位置的Map。它将比遍历列表中的所有项更快。
但如果列表中没有那么多员工,请选择stream.filter。这对他的工作有好处。

yqkkidmi

yqkkidmi2#

正如我从你的问题中了解到的,你有一个包含如下对象的列表(u=unqualified personal,p001=id,ali=name,poyraz=surname,6=workhour),你得到一个元素showpersonal p001,其中p001是id。你可以迭代你的列表并检查是否有具有该id的元素。
例如:
employee.stream().filter(employee->employee.getid().equals(“p001”)).collect(tolist())
然后,您将拥有一个包含该id的所有元素的列表,如果是唯一的,则只有一个元素的列表

相关问题