Java学习_Day 12(学习内容:尚硅谷集合JAVA零基础P542-P547)

P542 集合-每日一考

  1. 集合Collection中存储的如果是自定义类的对象,需要自定义类重写哪个方法?为什么?
  2. ArrayList、LinkedList和Vector相同点和不同点
  3. List接口常用方法有哪些
  4. 如何使用Iterator和增强for循环遍历List
  5. Set存储数据的特点是什么?常见的实现类有哪些?说一下各自特点

P543 集合-复习:Collection遍历

P544 集合-复习:List接口

P545 集合-复习:Set接口

P546 集合-TreeSet的课后练习

package com.collection;

public class Employee implements Comparable{
    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 +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Employee){
            Employee e = (Employee) o;
            return this.name.compareTo(e.name);
        }
        throw new RuntimeException("传入数据类型不一致");
    }
}
package com.collection;

public class 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 "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }
}
package com.collection;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class EmployeeTest {

    @Test
    public void test1(){
        TreeSet set = new TreeSet();

        Employee e1 = new Employee("liudehua", 55,new MyDate(1965,5,4));
        Employee e2 = new Employee("zhangxueyou", 43,new MyDate(1985,5,4));
        Employee e3 = new Employee("guofucheng", 44,new MyDate(1975,5,4));
        Employee e4 = new Employee("liming", 51,new MyDate(1982,5,4));
        Employee e5 = new Employee("liaochaowei", 18,new MyDate(1998,5,4));

        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);

        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }
    @Test
    public void test2(){

        TreeSet set = 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 b1 = e1.getBirthday();
                    MyDate b2 = e2.getBirthday();

                    int minusYear = b1.getYear() - b2.getYear();
                    if (minusYear != 0){
                        return minusYear;
                    }
                    int minusMonth = b1.getMonth() - b2.getMonth();
                    if (minusMonth != 0){
                        return minusMonth;
                    }
                    return b1.getDay() - b2.getDay();

                }
                throw new RuntimeException("传入数据类型不一致");
            }
        });

        Employee e1 = new Employee("liudehua", 55,new MyDate(1965,5,4));
        Employee e2 = new Employee("zhangxueyou", 43,new MyDate(1985,5,4));
        Employee e3 = new Employee("guofucheng", 44,new MyDate(1975,5,4));
        Employee e4 = new Employee("liming", 51,new MyDate(1982,5,4));
        Employee e5 = new Employee("liaochaowei", 18,new MyDate(1998,5,4));

        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);

        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

P547 集合-Set课后两道面试题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值