簡體   English   中英

如何在Java中對ArrayList中的某些項目進行排序?

[英]How to sort certain items in an ArrayList in Java?

我知道有人問過這個問題,但是我覺得我的情況很特殊。 我仍然無法弄清楚這一點。

因此,如果員工對象位於部門Information Systems或Account中 ,則需要按其年齡對它們進行排序。

但是,我無法獲得僅對部門信息系統或會計部門中的員工進行排序的代碼,我如何指定僅按年齡對其進行排序並僅打印其余部分?

這是所有相關代碼:

員工類別:

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接口

package de.professional_webworkx.blog.companymanager.filter;

public interface IFilter<T> {

    public boolean accept(T value);
}

簡單的Departmentfilter

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

}

AgeComparator

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

也許這對您有幫助。 帕特里克

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

但這僅在您要排序的所有條目都在一起時才有效。 並且,請更改要對其排序的字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM