java代码比较器下载_Java实现代码-比较器

如下代码:

import java.util.*;

class company implements Comparable {

private String name;

private String technology;

private double marketValue;

private double salary;

private int city;

public company(String name, String technology, double marketValue, double salary, int city) {

super();

this.name = name;

this.technology = technology;

this.marketValue = marketValue;

this.salary = salary;

this.city = city;

}

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name

* the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the technology

*/

public String getTechnology() {

return technology;

}

/**

* @param technology

* the technology to set

*/

public void setTechnology(String technology) {

this.technology = technology;

}

/**

* @return the marketValue

*/

public double getMarketValue() {

return marketValue;

}

/**

* @param marketValue

* the marketValue to set

*/

public void setMarketValue(double marketValue) {

this.marketValue = marketValue;

}

/**

* @return the salary

*/

public double getSalary() {

return salary;

}

/**

* @param salary

* the salary to set

*/

public void setSalary(double salary) {

this.salary = salary;

}

/**

* @return the city

*/

public int getCity() {

return city;

}

/**

* @param city

* the city to set

*/

public void setCity(int city) {

this.city = city;

}

@Override

public int compareTo(Object o) {

// TODO Auto-generated method stub

company comp = (company) o;

int result = comp.getSalary() > salary ? 1 : (comp.getSalary() == salary ? 0 : -1);

return result;

}

public String toString() {

return "\r\tnum:" + name + " name:" + name + "\r";

}

}

public class 比较器 {

public static void main(String[] args) {

Listlist = new ArrayList(10);

list.add(new company("xx", "11", 1, 12, 1));

list.add(new company("yy", "11", 1, 13, 1));

list.add(new company("zz", "11", 1, 12, 1));

Collections.sort(list); // 内部比较器:要排序的对象实现Comparable接口,可以对自身进行比较

System.out.println(Arrays.toString(list.toArray()));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Comparator 接口有一个 reversed() 方法,它返回与当前比较器相反的比较器。这意味着,如果当前比较器认为 a 小于 b,则返回的比较器将认为 a 大于 b。 以下是一个使用 Java Comparator reversed() 方法的示例代码: ```java import java.util.*; public class Example { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3); // 使用 Comparator.naturalOrder() 方法创建一个自然排序的比较器 Comparator<Integer> naturalOrder = Comparator.naturalOrder(); // 使用 reversed() 方法创建一个与自然排序相反的比较器 Comparator<Integer> reverseOrder = naturalOrder.reversed(); // 使用两个不同的比较器对列表进行排序 Collections.sort(numbers, naturalOrder); System.out.println(numbers); Collections.sort(numbers, reverseOrder); System.out.println(numbers); } } ``` 输出结果为: ``` [1, 1, 2, 3, 3, 4, 5, 5, 6, 9] [9, 6, 5, 5, 4, 3, 3, 2, 1, 1] ``` 在这个示例中,我们首先创建了一个包含整数的列表。然后,我们使用 `Comparator.naturalOrder()` 方法创建一个自然排序的比较器。接下来,我们使用 `reversed()` 方法创建一个与自然排序相反的比较器。最后,我们使用这两个不同的比较器对列表进行排序,并输出结果。 需要注意的是,reversed() 方法只能用于已经实现了 Comparator 接口的类。如果你想使用 reversed() 方法,但你的类没有实现 Comparator 接口,你需要实现它并重写 compare() 方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值