练习一
1. 定义一个Employee类。 该类包含:private成员变量name,age,birthday,其中 birthday 为 MyDate 类的对象; 并为每一个属性定义 getter, setter 方法; 并重写 toString 方法输出 name, age, birthday 2. MyDate类包含: private成员变量year,month,day;并为每一个属性定义 getter, setter 方法; 3. 创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中(TreeSet 需使用泛型来定义) 4. 分别按以下两种方式对集合中的元素进行排序,并遍历输出: 1). 使Employee 实现 Comparable 接口,并按 name 排序 2). 创建 TreeSet 时传入 Comparator对象,按生日日期的先后排序。
代码
package com.atguigu01.use.exer1; /** * ClassName: Employee * Description: * 定义一个Employee类。 * 该类包含:private成员变量name,age,birthday,其中 birthday 为 MyDate 类的对象; * 并为每一个属性定义 getter, setter 方法; * 并重写 toString 方法输出 name, age, birthday * @Author 尚硅谷-宋红康 * @Create 17:03 * @Version 1.0 */ public class Employee implements Comparable<Employee>{ private String name; private int age; private MyDate birthday; public Employee() { } 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 + "]}"; } //按照name从低到高排序 @Override public int compareTo(Employee o) { return this.name.compareTo(o.name); } }
package com.atguigu01.use.exer1; import org.junit.Test; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; /** * ClassName: EmployeeTest * Description: * * @Author 尚硅谷-宋红康 * @Create 17:06 * @Version 1.0 */ public class EmployeeTest { //需求1:使Employee 实现 Comparable 接口,并按 name 排序 @Test public void test1(){ TreeSet<Employee> set = new TreeSet<>(); Employee e1 = new Employee("HanMeimei",18,new MyDate(1998,12,21)); Employee e2 = new Employee("LiLei",20,new MyDate(1996,11,21)); Employee e3 = new Employee("LiHua",21,new MyDate(2000,9,12)); Employee e4 = new Employee("ZhangLei",19,new MyDate(1997,5,31)); Employee e5 = new Employee("ZhangYi",23,new MyDate(2001,11,2)); set.add(e1); set.add(e2); set.add(e3); set.add(e4); set.add(e5); //遍历 Iterator<Employee> iterator = set.iterator(); while(iterator.hasNext()){ Employee employee = iterator.next(); System.out.println(employee); } } //需求2:创建 TreeSet 时传入 Comparator对象,按生日日期的先后排序。 @Test public void test2(){ Comparator<Employee> comparator = new Comparator<Employee>(){ @Override public int compare(Employee o1, Employee o2) { //错误的写法: // return o1.getBirthday().toString().compareTo(o2.getBirthday().toString()); //正确的写法1: // int yearDistince = o1.getBirthday().getYear() - o2.getBirthday().getYear(); // if(yearDistince != 0){ // return yearDistince; // } // // int monthDistince = o1.getBirthday().getMonth() - o2.getBirthday().getMonth(); // if(monthDistince != 0){ // return monthDistince; // } // // return o1.getBirthday().getDay() - o2.getBirthday().getDay(); //正确的写法2: return o1.getBirthday().compareTo(o2.getBirthday()); } }; TreeSet<Employee> set = new TreeSet<>(comparator); Employee e1 = new Employee("HanMeimei",18,new MyDate(1998,12,21)); Employee e2 = new Employee("LiLei",20,new MyDate(1996,11,21)); Employee e3 = new Employee("LiHua",21,new MyDate(2000,9,12)); Employee e4 = new Employee("ZhangLei",19,new MyDate(1996,5,31)); Employee e5 = new Employee("ZhangYi",23,new MyDate(2000,9,2)); set.add(e1); set.add(e2); set.add(e3); set.add(e4); set.add(e5); //遍历 Iterator<Employee> iterator = set.iterator(); while(iterator.hasNext()){ Employee employee = iterator.next(); System.out.println(employee); } } }
package com.atguigu01.use.exer1; /** * ClassName: MyDate * Description: * MyDate类包含: * private成员变量year,month,day;并为每一个属性定义 getter, setter 方法; * * @Author 尚硅谷-宋红康 * @Create 17:04 * @Version 1.0 */ public class MyDate implements Comparable<MyDate>{ private int year; private int month; private int day; public MyDate() { } 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 year + "年" + month + "月" + day + "日"; } //按照生日从小到大排列 @Override public int compareTo(MyDate o) { int yearDistince = this.getYear() - o.getYear(); if(yearDistince != 0){ return yearDistince; } int monthDistince = this.getMonth() - o.getMonth(); if(monthDistince != 0){ return monthDistince; } return this.getDay() - o.getDay(); } }
练习二
(1)创建一个ArrayList集合对象,并指定泛型为<Integer> (2)添加5个[0,100)以内的随机整数到集合中 (3)使用foreach遍历输出5个整数 (4)使用集合的removeIf方法删除偶数,为Predicate接口指定泛型<Ineteger> (5)再使用Iterator迭代器输出剩下的元素,为Iterator接口指定泛型<Integer>
代码
package com.atguigu01.use.exer2; import java.util.ArrayList; import java.util.Iterator; import java.util.function.Predicate; /** * ClassName: Exer02 * Description: * * @Author 尚硅谷-宋红康 * @Create 17:23 * @Version 1.0 */ public class Exer02 { public static void main(String[] args) { //(1)创建一个ArrayList集合对象,并指定泛型为<Integer> ArrayList<Integer> list = new ArrayList<>(); //(2)添加5个[0,100)以内的随机整数到集合中 for (int i = 0; i < 5; i++) { int random = (int)(Math.random() * (99 - 0 + 1)); list.add(random); } //(3)使用foreach遍历输出5个整数 for(Integer value : list){ System.out.println(value); } //(4)使用集合的removeIf方法删除偶数,为Predicate接口指定泛型<Ineteger> list.removeIf(new Predicate<Integer>(){ @Override public boolean test(Integer value) { return value % 2 == 0; } }); System.out.println(); //(5)再使用Iterator迭代器输出剩下的元素,为Iterator接口指定泛型<Integer> Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()){ Integer value = iterator.next(); System.out.println(value); } } }