java排序

对复杂的排序需求的处理

EmployeeSortTest 测试类

package v1ch06.interfaces;

import java.util.*;

/**
 * This program demonstrates the use of the Comparable interface.
 * @version 1.30 2004-02-27
 * @author Cay Horstmann
 */
public class EmployeeSortTest
{
   public static void main(String[] args)
   {

      // 数组排序
      Employee[] staff = new Employee[9];
      List<Employee> list = new ArrayList<>();
      staff[0] = new Employee("05", "05", "06", list);
      staff[4] = new Employee("04", "04", "05", list);
      staff[3] = new Employee("03", "03", "04", list);
      staff[1] = new Employee("02", "02", "03", list);
      staff[2] = new Employee("01", "01", "02", list);
      staff[5] = new Employee("06", "06", "07", list);
      staff[8] = new Employee("07", "07", "", list);
      staff[7] = new Employee("08", "08", "017", list);
      staff[6] = new Employee("09", "09", "017", list);
      for (Employee employee : staff) {
         list.add(employee);
      }

      Arrays.sort(staff);

      for (Employee e : staff)
         System.out.println("name=" + e.getName() );


      //列表排序

//      for (Employee employee : staff) {
//         list.add(employee);
//      }
//
//      list.sort(new EmployeeCom(list));
//
//
//      for (Employee e : list)
//         System.out.println("name=" + e.getName() );
   }


}

EmployeeCom

package v1ch06.interfaces;


import java.util.Comparator;
import java.util.List;
import java.util.Optional;

class EmployeeCom implements Comparator<Employee> {

    List<Employee> list;

    public EmployeeCom(List<Employee> list) {
        this.list = list;
    }



    public int compare(Employee var1, Employee var2) {
        return isParent(var1, var2) ? -1 : 1;
    }


    /**
     * 判断对方是不是我的上级
     * @param other
     * @return
     */
    private boolean isParent(Employee me, Employee other) {
        Employee parent = me;
        while (true && parent != null) {
            if (other.getMobile().equalsIgnoreCase(parent.getPreMobile())) {
                return true;
            } else  {
                parent = getParent(parent);
            }
        }
        return false;
    }

    private Employee getParent(Employee employee) {
        Optional<Employee> optional = this.list.stream().filter(item -> item.getMobile().equals(employee.getPreMobile())).findFirst();
        if (optional.isPresent()) {
            return  optional.get();
        } else {
            return null;
        }
    }
}

Employee

package v1ch06.interfaces;

import java.util.List;
import java.util.Optional;

public class Employee implements Comparable<Employee>
{
   private String name;
   private String mobile;
   private String preMobile;
   private List<Employee> list;

   public String getName() {
      return name;
   }

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

   public String getMobile() {
      return mobile;
   }

   public void setMobile(String mobile) {
      this.mobile = mobile;
   }

   public String getPreMobile() {
      return preMobile;
   }

   public void setPreMobile(String preMobile) {
      this.preMobile = preMobile;
   }

   public List<Employee> getList() {
      return list;
   }

   public void setList(List<Employee> list) {
      this.list = list;
   }

   public Employee(String name, String mobile, String preMobile, List<Employee> list) {
      this.name = name;
      this.preMobile = preMobile;
      this.mobile = mobile;
      this.list = list;
   }
   /**
    * Compares employees by salary
    * @param other another Employee object
    * @return a negative value if this employee has a lower salary than
    * otherObject, 0 if the salaries are the same, a positive value otherwise
    */
   @Override
   public int compareTo(Employee other)
   {
      return isParent(other) ? -1 : 1;
   }

   /**
    * 判断对方是不是我的上级
    * @param other
    * @return
    */
   private boolean isParent(Employee other) {
      Employee parent = this;
      while (true && parent != null) {
         if (other.getMobile().equalsIgnoreCase(parent.getPreMobile())) {
            return true;
         } else  {
            parent = getParent(parent);
         }
      }
      return false;
   }

   private Employee getParent(Employee employee) {
      Optional<Employee> optional = this.list.stream().filter(item -> item.getMobile().equals(employee.getPreMobile())).findFirst();
      if (optional.isPresent()) {
         return  optional.get();
      } else {
         return null;
      }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值