编写一个类实现java.util.Comparator接口的接口。
时间: 2024-05-09 16:17:33 浏览: 73
可以的,这是一个关于Java编程的问题。Comparator接口是用来比较两个对象的大小,实现该接口并重写compare()方法可以自定义比较规则。以下是一个简单的示例代码:
```
public class MyComparator implements Comparator<Integer> {
@Override
public int compare(Integer a, Integer b) {
return b - a; // 从大到小排序
}
}
```
这个示例代码实现了一个比较两个整数大小的比较器,按照从大到小的顺序进行排序。
相关问题
import java.util.Scanner; import java.util.ArrayList; import java.util.Comparator; public class Main { public static void main(String []args) { Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); ArrayList<String> a = new ArrayList<String>(); while(n!=0){ String s = in.nextLine(); if(!a.contains(s)){ a.add(s); } n--; } a.sort(new Comparator<String>() { @Override public int compare(String s1, String s2) { return Integer.parseInt(s1.substring(0, 4))-Integer.parseInt(s2.substring(0, 4)); } }); System.out.println(a.size()); for(String i:a){ System.out.println(i); } } }
这是一段使用Java语言编写的程序。程序的功能是读取用户输入的一组字符串,去除其中重复的字符串,并按照字符串前四个字符转换成的整数从小到大排序,最后输出排序后的字符串列表。
具体实现方法是先使用Scanner类读取用户输入的整数n,表示接下来要读取n个字符串。然后使用ArrayList类创建一个字符串列表a,用于存储读取到的字符串。接着通过一个循环,读取n个字符串,并判断是否已经在列表a中存在,若不存在则将其添加到列表a中。最后,使用sort方法和自定义的Comparator类对列表a中的字符串进行排序,并输出排序后的字符串列表。
需要注意的是,该程序假设用户输入的字符串都能够成功转换成整数,并且每个字符串的前四个字符都是数字。如果这些条件不满足,程序可能会出现异常。
用java实现以下代码: 定义Staff类(职工),添加如下属性(name, address, age, sex, salary, dateHired),类型自定,其中salary为工资,dateHired为雇佣日期(java.util.Date类型)。生成相应的setter/getter方法。编写NameComparator,AgeComparator和ComplexComparator比较器。其中ComplexComparator为复合比较器,选用工资和雇佣日期进行升序排序。比较器中需要使用Lambda表达式来完成
以下是用Java实现的代码:
```
import java.util.*;
public class Staff {
private String name;
private String address;
private int age;
private String sex;
private double salary;
private Date dateHired;
public Staff(String name, String address, int age, String sex, double salary, Date dateHired) {
this.name = name;
this.address = address;
this.age = age;
this.sex = sex;
this.salary = salary;
this.dateHired = dateHired;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Date getDateHired() {
return dateHired;
}
public void setDateHired(Date dateHired) {
this.dateHired = dateHired;
}
}
class NameComparator implements Comparator<Staff> {
public int compare(Staff a, Staff b) {
return a.getName().compareTo(b.getName());
}
}
class AgeComparator implements Comparator<Staff> {
public int compare(Staff a, Staff b) {
return a.getAge() - b.getAge();
}
}
class ComplexComparator implements Comparator<Staff> {
public int compare(Staff a, Staff b) {
if (a.getSalary() != b.getSalary()) {
return Double.compare(a.getSalary(), b.getSalary());
} else {
return a.getDateHired().compareTo(b.getDateHired());
}
}
}
class Main {
public static void main(String[] args) {
List<Staff> staffList = new ArrayList<>();
staffList.add(new Staff("Alice", "123 Main St", 25, "F", 50000, new Date(2021, 1, 1)));
staffList.add(new Staff("Bob", "456 Second St", 30, "M", 60000, new Date(2020, 1, 1)));
staffList.add(new Staff("Charlie", "789 Third St", 35, "M", 70000, new Date(2019, 1, 1)));
// Sort by name using NameComparator
Collections.sort(staffList, new NameComparator());
System.out.println("Sorted by name:");
for (Staff staff : staffList) {
System.out.println(staff.getName());
}
// Sort by age using AgeComparator
Collections.sort(staffList, new AgeComparator());
System.out.println("Sorted by age:");
for (Staff staff : staffList) {
System.out.println(staff.getAge());
}
// Sort by salary and date hired using ComplexComparator
Collections.sort(staffList, new ComplexComparator());
System.out.println("Sorted by salary and date hired:");
for (Staff staff : staffList) {
System.out.println(staff.getSalary() + ", " + staff.getDateHired());
}
}
}
```
阅读全文