完善并运行如下程序,使其能够按如下规则排序:先按age升序排,当age相同时,再按name升序排。
import java.util.TreeSet;
class Teacher implements Comparable {
String name;
int age;
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return name + ":" + age;
}
//在此处补充排序依据代码
}
public class SortTeacher {
public static void main(String[] args) {
TreeSet ts = new TreeSet();
ts.add(new Teacher("Jack",19));
ts.add(new Teacher("Rose",18));
ts.add(new Teacher("Tom", 19));
ts.add(new Teacher("Rose",18));
System.out.println(ts);
}
}
【输入形式】
【输出形式】
[Rose:18, Jack:19, Tom:19]
import java.util.Comparator;
import java.util.TreeSet;
/**
* Created with IntelliJ IDEA.
*
* @Author: ice_water
* @Date: 2022/03/22/5:15 PM
*/
class MyComparator implements Comparator<String> {
public int compare(String obj1, String obj2) {
return obj1.length() - obj2.length();
}
}
public class ComparatorDemo {
public static void main(String[] args) {
TreeSet ts = new TreeSet(new MyComparator());
ts.add("Jack");
ts.add("Helena");
ts.add("Eve");
System.out.println(ts);
}
}
完善并运行程序,使其能够按照字符串的长度升序排序。
import java.util.Comparator;
import java.util.TreeSet;
class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
//在此补充代码
}
}
public class ComparatorDemo {
public static void main(String[] args) {
TreeSet ts = new TreeSet(new MyComparator());
ts.add(“Jack”);
ts.add(“Helena”);
ts.add(“Eve”);
System.out.println(ts);
}
}
【输入形式】
【输出形式】
[Eve, Jack, Helena]
import java.util.TreeSet;
/**
* Created with IntelliJ IDEA.
*
* @Author: ice_water
* @Date: 2022/03/22/5:07 PM
*/
class Teacher implements Comparable<Teacher> {
String name;
int age;
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 Teacher(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return name + ":" + age;
}
public int compareTo(Teacher teacher) {
if (this.getAge() - (teacher.getAge()) != 0) {
return this.getAge() - (teacher.getAge());
} else {
return this.getName().compareTo(teacher.getName());
}
}
}
public class SortTeacher {
public static void main(String[] args) {
TreeSet ts = new TreeSet();
ts.add(new Teacher("Jack", 19));
ts.add(new Teacher("Rose", 18));
ts.add(new Teacher("Tom", 19));
ts.add(new Teacher("Rose", 18));
System.out.println(ts);
}
}
书后练习题
编程题一
在HashSet集合中添加三个person对象,把姓名相同的当作一个人,禁止重复添加。要求如下:
person类定义name和age属性,重写hashcode方法和equals方法。针对person类中的name属性进行比较如果name相同,hashcode方法的返回值相同,equals方法返回true。
import java.util.HashSet;
/**
* Created with IntelliJ IDEA.
*
* @Author: ice_water
* @Date: 2022/03/22/10:10 PM
*/
// 若未重写hashcode和equals方法(已被注释) 那么则会输出三个person 若取消下面的注释,则会输出两个对象
public class demo {
public static void main(String[] args) {
Person person1 = new Person("张三", 18);
Person person2 = new Person("张三", 19);
Person person3 = new Person("王五", 20);
HashSet<Person> hashSet = new HashSet();
hashSet.add(person1);
hashSet.add(person2);
hashSet.add(person3);
System.out.println(hashSet);
}
}
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
Person person = (Person) o;
return name.equals(person.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
编程题二
选择合适的Map集合保存5为学员的学号和姓名,然后按照学号的自然顺序倒序将这些键值对一一打印出来。要求如下:
1)创建TreeMap集合。
2)使用put方法存储对象到Map中,存的时候 可以观察打乱顺序排序后的结果
3)使用map.keySet()获取键的set集合。
4)使用set集合的iterator方法获取Integer对象用于迭代键。
5)使用Map集合的get()方法获取所有键值对所对应的值。
import java.util.*;
/**
* Created with IntelliJ IDEA.
*
* @Author: ice_water
* @Date: 2022/03/22/10:10 PM
*/
public class demo {
public static void main(String[] args) {
TreeMap map = new TreeMap(new MyComparator());
map.put("1", "Lucy");
map.put("2", "Lucy");
map.put("3", "John");
map.put("4", "Smith");
map.put("5", "Amanda");
Set set = new HashSet();
set.add(map);
Iterator iterator = set.iterator();
for (Object key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
}
}
class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
String ele1 = (String) obj1;
String ele2 = (String) obj2;
return ele2.compareTo(ele1);
}
}
插播广告:java助攻 python助攻,实训代做,网课代刷都可 欢迎咨询
企鹅号 1427774041