练习题:集合

1: Collection与Collections的区别

 

Collection 是集合类的上级接口,集成他的接口主要有Set和List
 Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索, 排序, 线程安全等操作

 

2: Set里的元素是不能重复的, 那么用什么方法来区分重复与否呢? 使用== 还是用equals()?他们有何区别

Set里的元素是不可重复的,用equals()方法判断两个Set是否相等
equals()和== 方法决定引用的值是否指向同一对象equals()在类中被覆盖,为的是两个分离的对象的内容和类型相同的话返回真值

 

3: List ,Set, Map是否集成Collection接口

List, Set集成Collection Map不继承

 

4: 两个对象值相同(x.equals(y) == true ),但却可以有不同的hashcode这句话对吗

不对,两个equals()相同 那么hashcode一定相同

 

 

5: 说出ArrayList 与Vector, LinkedList的存储性和特性

 

 

 

 

 

10: 创建一个Set接口的实现类 添加10个元素通过iterator遍历此几个元素

    public static void main(String[] args) {
        Collection c1 = new HashSet();
        int num = 1;
        while (num <= 10){
            c1.add(num);
            num++;
        }
        System.out.println(c1.size());
        Iterator t1 = c1.iterator();
        while (t1.hasNext()){
            System.out.println(t1.next());
        }
    }
添加Set集合内10个元素 通过iterator遍历

 

 

11: 创建一个Set接口的实现类 添加10个元素通过foreach遍历此集合内元素

    public static void main(String[] args) {
        Collection c1 = new HashSet();
        int num = 1;
        while (num <= 10){
            c1.add(num);
            num++;
        }
        System.out.println(c1.size());
        Iterator t1 = c1.iterator();


        for(Object o : c1){
            System.out.println(o);
        }
    }
添加Set集合内10个元素 通过foreach遍历元素

 

 

12:创建一个Set接口的实现类 添加元素, 要求能排序

 

   public static void main(String[] args) {
        Collection c1 = new HashSet();
        c1.add(new PersTestOne("老王",13));
        c1.add(new PersTestOne("老李",14));
        c1.add(new PersTestOne("老张", 10));
        System.out.println(c1);  // [PersTestOne{name='老张', age=10}, PersTestOne{name='老李', age=14}, PersTestOne{name='老王', age=13}]
    }
}




class PersTestOne implements  Comparable{

    private  String name;
    private int age;
    @Override
    public int compareTo(Object o) {
        if(o instanceof  PersTestOne){
            PersTestOne p1 = (PersTestOne) o;
            return  this.name.compareTo(p1.name);
        }else{
            return 0;
        }
    }

    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;
    }

    @Override
    public String toString() {
        return "PersTestOne{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public PersTestOne(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public PersTestOne() {
    }
}
创建Set 添加元素要求能排序

 

 

 

 

import sun.reflect.generics.tree.Tree;

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

public class EmployTest {
    public static void main(String[] args) {

        /*
        TreeSet set1 = new TreeSet();

        EmplTe e1 = new EmplTe("老王",17,new Myd(1994,11,23));
        EmplTe e2 = new EmplTe("老刘",18,new Myd(1995,9,13));
        set1.add(e1);
        set1.add(e2);
//        Iterator iterator = set1.iterator();
        for (Object obj:set1) {
            System.out.println(obj);
        }


         */

        // 按照生日进行排序

        TreeSet set = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
               if(o1 instanceof  EmplTe && o2 instanceof  EmplTe){
                   EmplTe e1 = (EmplTe) o1;
                   EmplTe e2 = (EmplTe) o2;
                   Myd b1Birthday = e1.getBirthday();
                   Myd b2Birthday = e2.getBirthday();
                   int minusYear = b1Birthday.getYear() - b2Birthday.getYear();
                   int minusMonth = b1Birthday.getMonth() - b2Birthday.getMonth();
                   int minusDay = b1Birthday.getDay() - b2Birthday.getDay();
                   if(minusYear != 0){
                       return minusYear;
                   }
                   if(minusMonth != 0){
                       return minusMonth;
                   }
                   return minusDay;

               }
               throw  new RuntimeException("输入的不对");
            }
        });


        EmplTe e1 = new EmplTe("老王",17,new Myd(1994,11,23));
        EmplTe e2 = new EmplTe("老刘",18,new Myd(1995,9,13));
        EmplTe e3 = new EmplTe("老张",18,new Myd(1995,9,10));
        set.add(e1);
        set.add(e2);
        set.add(e3);
        System.out.println(set);


    }
}

class Myd{
    private  int year;
    private  int month;
    private  int day;

    public Myd(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;
    }

    public Myd() {
    }

    @Override
    public String toString() {
        return "Myd{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }
}

class EmplTe implements  Comparable{

    @Override
    public int compareTo(Object obj){
        if(obj instanceof  EmplTe){
            EmplTe e1 = (EmplTe) obj;
            return this.name.compareTo(e1.name);
        }
        return 0;
    }
    private  String name;
    private  int age;
    private Myd                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                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 Myd getBirthday() {
        return birthday;
    }

    public  void setBirthday( Myd birthday) {
        this.birthday = birthday;
    }

    public EmplTe(String name, int age,  Myd birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public EmplTe() {
    }

    @Override
    public String toString() {
        return "EmplTe{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
};

 

 

posted @ 2020-01-19 16:29  可爱的红领巾  阅读(258)  评论(0编辑  收藏  举报