【问题标题】:How to sort certain items in an ArrayList in Java?如何在 Java 中对 ArrayList 中的某些项目进行排序?
【发布时间】:2014-03-18 21:02:30
【问题描述】:

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

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

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

以下是所有相关代码:

员工等级:

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

【问题讨论】:

  • 您能否为一小组输入显示您预期的排序输出?我能理解你在说什么,但想确认一下。
  • 当然,看看主方法类:我希望它对 Employee 对象有参数(String,integer,“Accounting”)的地方进行排序,并对有参数的 Employee 对象(String,整数,“信息系统”)。我不想对最后一个参数为“营销”或“人力资源”的其他对象进行排序。
  • 否 - 我要的是排序输出的示例。请不要以文字的标准。

标签: java list sorting


【解决方案1】:

试试这个例子,我离开了 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 接口

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

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

【讨论】:

    【解决方案2】:
    Add this to Your Model 
    
    @Override
        public int compareTo(Object arg0) {
            // TODO Auto-generated method stub
            Employee employee = (Employee) arg0;
            if(employee.getAge() == 10 && this.getAge() == 10 ){
    
                return this.getDepartment().compareTo(employee.getDepartment());
            } 
            return 0;
        }
    

    但这只有在您想要排序的所有条目都在一起时才有效。 请更改您希望对其进行排序的字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-05
      • 2013-08-20
      • 2019-09-26
      • 1970-01-01
      • 2011-06-05
      • 2013-08-28
      • 1970-01-01
      相关资源
      最近更新 更多