Set练习题
第一题:
定义一个Employee类。
该类包含:private成员变量name,age,birthday,其中birthday 为MyDate 类的对象;
并为每一个属性定义getter, setter 方法;
并重写toString 方法输出name, age, birthday
MyDate类包含:
private成员变量year,month,day;并为每一个属性定义getter, setter 方法;
创建该类的5 个对象,并把这些对象放入TreeSet 集合中(下一章:TreeSet 需使用泛型来定义)
分别按以下两种方式对集合中的元素进行排序,并遍历输出:
1). 使Employee 实现Comparable 接口,并按name 排序
2). 创建TreeSet 时传入Comparator对象,按生日日期的先后排序。
public class TreeSetExercise {
public static void main(String[] args) {
Employee e1 = new Employee("Tom", 20, new MyDate(2000,2,4));
Employee e2 = new Employee("Jack", 21, new MyDate(1999,12,4));
Employee e3 = new Employee("Jerry", 18, new MyDate(2002,6,3));
Employee e4 = new Employee("Marry", 25, new MyDate(1995,4,4));
Employee e5 = new Employee("Steve", 18, new MyDate(2002,2,4));
TreeSet tre = new TreeSet();
tre.add(e1);
tre.add(e2);
tre.add(e3);
tre.add(e4);
tre.add(e5);
tre.forEach(System.out::println);
System.out.println("----------------------");
TreeSet tre1 = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Employee && o2 instanceof Employee) {
Employee e1 = (Employee) o1;
Employee e2 = (Employee) o2;
MyDate d1 = e1.getBirthday();
MyDate d2 = e2.getBirthday();
int minusYear = d1.getYear()-d2.getYear();
if (minusYear != 0){
return minusYear;
}
int minusMonth = d1.getMonth()-d2.getMonth();
if (minusMonth != 0){
return minusMonth;
}
int minusDay = d1.getDay()-d2.getDay();
if (minusDay != 0){
return minusDay;
}else{
return e1.getName().compareTo(e2.getName());
}
}else{
throw new RuntimeException("参数有误");
}
}
});
tre1.add(e1);
tre1.add(e2);
tre1.add(e3);
tre1.add(e4);
tre1.add(e5);
tre1.forEach(System.out::println);
}
}
public class Employee implements Comparable {
private String name;
private int age;
private MyDate birthday;
@Override
public int compareTo(Object o) {
if (o instanceof Employee){
Employee e = (Employee)o;
return this.name.compareTo(e.name);
}else{
throw new RuntimeException("参数有误");
}
}
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
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 MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
if (age != employee.age) return false;
if (name != null ? !name.equals(employee.name) : employee.name != null) return false;
return birthday != null ? birthday.equals(employee.birthday) : employee.birthday == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
result = 31 * result + (birthday != null ? birthday.hashCode() : 0);
return result;
}
}
class MyDate{
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
}
第二题:在List内去除重复数字值,要求尽量简单
public class ListExercise {
public static List duplicateList(List list) {
HashSet hashSet = new HashSet();
hashSet.addAll(list);
return new ArrayList(hashSet);
}
public static void main(String[] args) {
List list= new ArrayList();
list.add(new Integer(1));
list.add(new Integer(2));
list.add(new Integer(2));
list.add(new Integer(4));
list.add(new Integer(4));
List list2 = duplicateList(list);
for(Object integer: list2) {
System.out.println(integer);
}
}
}
第三题HashSet(先HashCode后equals):
public class HashSetExercise {
@Test
public void test(){
HashSet set = new HashSet();
Person01 p1 = new Person01(1001,"AA");
Person01 p2 = new Person01(1002,"BB");
set.add(p1);
set.add(p2);
p1.name = "CC";//集合中原{1001,"AA"}的hash值位置,变为了的 {1001,"CC"}
set.remove(p1);// 在{1001,"CC"}Hash值位置没有找到{1001,"CC"},因为它在原来的{1001,"AA"}位置
System.out.println(set);
//[Person01{id=1002, name='BB'}, Person01{id=1001, name='CC'}]
set.add(new Person01(1001,"CC"));
//正常添加因为,原来集合中的{1001,"CC"}在{1001,"AA"}的hash值位置
System.out.println(set);
//[Person01{id=1002, name='BB'}, Person01{id=1001, name='CC'},Person01{id=1001, name='CC'}]
set.add(new Person01(1001,"AA"));//想通过Hash值比较相同,
// 再通过equals比较,不相同,所以添加成功
System.out.println(set);
//[Person01{id=1002, name='BB'}, Person01{id=1001, name='CC'},
// Person01{id=1001, name='CC'},Person01{id=1001, name='AA'}]
}
}
class Person01{
public int id;
public String name;
public Person01(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person01 person01 = (Person01) o;
if (id != person01.id) return false;
return name != null ? name.equals(person01.name) : person01.name == null;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Person01{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}