gpt4 book ai didi

java - 如何在 Java 中对 ArrayList 中的某些项目进行排序?

转载 作者:太空宇宙 更新时间:2023-11-04 15:01:47 25 4
gpt4 key购买 nike

我知道有人问过这个问题,但我觉得我的问题是一个特例。我还是没能弄清楚这一点。

因此,如果员工对象属于部门信息系统或帐户,我需要按他们的年龄对他们进行排序。

但是,我无法获取代码来对信息系统或会计部门中的员工进行排序,我如何指定仅按年龄对他们进行排序并打印其余部分?

这是所有相关代码:

员工类别:

public class Employee {

private String name;
private int age;
private String department;

public String getDept(){
return department;
}//end dept

public void setDept(String dept){
this.department = dept;
}//end

public String getName(){
return name;
}//end name

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

public int getAge(){
return age;
}//end age

public void setAge(int a){
this.age = a;
}//end



public String toString(){

return name + " " + age + " " + department;
}//end to string


public Employee (String n,int age,String dept){

this.name = n;
this.age = age;
this.department = dept;

}//end employee
}//end class

院系类别:

import java.util.*;






public class Department implements Comparator<Employee> {

@Override
public int compare(Employee o1, Employee o2){

return o1.getAge() - o2.getAge();

}//end method

}//end departmen

主要方法类:

import java.util.*;



public class Company {


public static void main(String [] args){


PrimeAgeChecker p = new PrimeAgeChecker();
ArrayList<Employee> test = new ArrayList<Employee>();


test.add(new Employee("Counting Guru",55,"Accounting"));
test.add(new Employee("Counting Pro",45,"Accounting"));
test.add(new Employee("Counting Savvy",40,"Accounting"));
test.add(new Employee("Counting Novice",25,"Accounting"));
test.add(new Employee("Sales Guru",50,"Marketing"));
test.add(new Employee("Sales Pro",48,"Marketing"));
test.add(new Employee("Sales Savvy",38,"Marketing"));
test.add(new Employee("Hiring Guru",58,"Human Resrouces"));
test.add(new Employee("Hiring Pro",47,"Human Resrouces"));
test.add(new Employee("Hacking Pro",47,"Information Systems"));
test.add(new Employee("Hacking Guru",51,"Information Systems"));
test.add(new Employee("Hacking Savvy",38,"Information Systems"));
test.add(new Employee("Hacking Novice",23,"Information Systems"));


for(Employee i: test){
if(i.getDept().equals("Information Systems") || i.getDept().equals("Accounting")){
Collections.sort(test, new Department());
System.out.println(i + " " + p.isPrime(i));
}//end
else{
System.out.println(i + " " + p.isPrime(i));
}





}//end main
}//end company

最佳答案

试试这个例子,我把 PrimeAgeCheck 排除在外:

package de.professional_webworkx.blog.companymanager;

import de.professional_webworkx.blog.companymanager.comparators.AgeComparator;
import de.professional_webworkx.blog.companymanager.filter.DepartmentFilter;
import de.professional_webworkx.blog.companymanager.filter.utils.FilterUtils;
import de.professional_webworkx.blog.companymanager.model.Employee;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CompanyManager {

public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();

employees.add(new Employee("Counting Guru", 55, "Accounting"));
employees.add(new Employee("Counting Pro", 45, "Accounting"));
employees.add(new Employee("Counting Savvy", 40, "Accounting"));
employees.add(new Employee("Counting Novice", 25, "Accounting"));
employees.add(new Employee("Sales Guru", 50, "Marketing"));
employees.add(new Employee("Sales Pro", 48, "Marketing"));
employees.add(new Employee("Sales Savvy", 38, "Marketing"));
employees.add(new Employee("Hiring Guru", 58, "Human Resrouces"));
employees.add(new Employee("Hiring Pro", 47, "Human Resrouces"));
employees.add(new Employee("Hacking Pro", 47, "Information Systems"));
employees.add(new Employee("Hacking Guru", 51, "Information Systems"));
employees.add(new Employee("Hacking Savvy", 38, "Information Systems"));
employees.add(new Employee("Hacking Novice", 23, "Information Systems"));

for(Employee employee : employees) {
System.out.println(employee);
}

List<String> filterDepartsments = new ArrayList<>();
filterDepartsments.add("Accounting");
filterDepartsments.add("Information Systems");

List<Employee> applyFilter = FilterUtils.applyFilter(employees, new DepartmentFilter("Accounting", "Information Systems"));

System.out.println("filtered list");
for(Employee employee : applyFilter) {
System.out.println(employee);
}

Collections.sort(applyFilter, new AgeComparator());
System.out.println("sorted list");
for(Employee employee : applyFilter) {
System.out.println(employee);
}
}

}

员工

package de.professional_webworkx.blog.companymanager.model;


public class Employee {

private String name;
private int age;
private String department;

public Employee(final String name, final int age, final String department) {
this.name = name;
this.age = age;
this.department = department;
}

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}


@Override
public String toString() {
return name + ", " + age + ", " + department;
}

}

IFilter接口(interface)

package de.professional_webworkx.blog.companymanager.filter;

public interface IFilter<T> {

public boolean accept(T value);
}

简单的部门过滤器

package de.professional_webworkx.blog.companymanager.filter;

import java.util.ArrayList;
import java.util.List;

public class DepartmentFilter<String> implements IFilter<String>{

private final String acceptedDepartment;
private final String acceptedDepartment2;

public DepartmentFilter(final String department, final String department2) {
this.acceptedDepartment = department;
this.acceptedDepartment2= department2;
}
@Override
public boolean accept(String value) {
return acceptedDepartment.equals(value) || acceptedDepartment2.equals(value);
}

}

年龄比较器

package de.professional_webworkx.blog.companymanager.comparators;

import de.professional_webworkx.blog.companymanager.model.Employee;
import java.util.Comparator;

public class AgeComparator implements Comparator<Employee>{

@Override
public int compare(Employee o1, Employee o2) {

return Integer.valueOf(o1.getAge()).compareTo(Integer.valueOf(o2.getAge()));
}

}

FilterUtils 类

package de.professional_webworkx.blog.companymanager.filter.utils;

import de.professional_webworkx.blog.companymanager.filter.IFilter;
import de.professional_webworkx.blog.companymanager.model.Employee;
import java.util.ArrayList;
import java.util.List;

public class FilterUtils {

private static final List<Employee> filteredList = new ArrayList<>();

public static List<Employee> applyFilter(final List<Employee> list, final IFilter filter) {

for(Employee e : list) {
if(filter.accept(e.getDepartment())) {
filteredList.add(e);
}
}

return filteredList;
}
}

示例输出

Counting Guru, 55, Accounting
Counting Pro, 45, Accounting
Counting Savvy, 40, Accounting
Counting Novice, 25, Accounting
Sales Guru, 50, Marketing
Sales Pro, 48, Marketing
Sales Savvy, 38, Marketing
Hiring Guru, 58, Human Resrouces
Hiring Pro, 47, Human Resrouces
Hacking Pro, 47, Information Systems
Hacking Guru, 51, Information Systems
Hacking Savvy, 38, Information Systems
Hacking Novice, 23, Information Systems
filtered list
Counting Guru, 55, Accounting
Counting Pro, 45, Accounting
Counting Savvy, 40, Accounting
Counting Novice, 25, Accounting
Hacking Pro, 47, Information Systems
Hacking Guru, 51, Information Systems
Hacking Savvy, 38, Information Systems
Hacking Novice, 23, Information Systems
sorted list
Hacking Novice, 23, Information Systems
Counting Novice, 25, Accounting
Hacking Savvy, 38, Information Systems
Counting Savvy, 40, Accounting
Counting Pro, 45, Accounting
Hacking Pro, 47, Information Systems
Hacking Guru, 51, Information Systems
Counting Guru, 55, Accounting

也许这对你有帮助。帕特里克

关于java - 如何在 Java 中对 ArrayList 中的某些项目进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22490988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com