JAVA温习课堂7

40、TreeSet(默认按升序排序) 
        调用集合元素的compareTo方法
        TreeSet 添加的应该是同一个类的对象
        equals方法返回的结果应该与compareTo方法返回的结果有一致性
        如果使用TreeSet 无参数的构造器创建一个TreeSet对象,则要求放入其中的元素的类必须实现CompareTo方法,所以在其中不能放入null对象
        必须放入同样类的对象,否则发生类型转换异常 java.lang.ClassCastException
    定制排序
        创建TreeSet对象时,创建Comparator接口的实现类
        要求Comparator 接口的 Compare 方法的返回值和两个元素的equals方法具有一致的返回值
程序代码:
    public class TestTreeSet {
            public static void main(String[] args) {
                Set set = new TreeSet();


                set.add(new TreeSetStudent("EngineerZhong",21));
                set.add(new TreeSetStudent("EngineerYe",20));

                System.out.println(set.size());

                for(Object obj : set){
                    System.out.println(obj);
                }

                Comparator comparator = new Comparator() {

                    public int compare(Object obj1, Object obj2) {
                        if(obj1 instanceof TreeSetStudent && obj2 instanceof TreeSetStudent){

                            TreeSetStudent stu1 = (TreeSetStudent) obj1;
                            TreeSetStudent stu2 = (TreeSetStudent) obj2;

                            return stu1.getScore() - stu2.getScore();
                            //返回值 
                            // a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. 
                            //正数、零、负数  情况是 第一个小于第二个,两个相等 ,第一个大于第二个

                        }else{
                            throw new ClassCastException("类型转换失败");
                        }
                    }
                };
                Set set2 = new TreeSet<E>(comparator);

                set2.add(new TreeSetStudent("EngineerZhong", 21));
                set2.add(new TreeSetStudent("EngineerYe", 20));
                set2.add(new TreeSetStudent("EngineerZhou",19));

                System.out.println(set2.size());

                for(Object obj : set2){
                    System.out.println(obj);
                }
            }
    }
TreeSetStudent类的部分程序代码:
实现了 Comparable 接口
重写hashCode方法 equals 方法 以及 compareTo 方法
    public int compareTo(Object obj ){
        if(obj instanceof TreeSetStudent){
            TreeSetStudent stu = (TreeSetStudent) obj;
            return -(this.score - stu.score);
        }else{
            throw new ClassCastException("无法进行类型转换");
        }
    }
输出结果:
    2
    TreeSetStudent [name=EngineerZhong, score=21]
    TreeSetStudent [name=EngineerYe, score=20]
    3
    TreeSetStudent [name=EngineerZhou, score=19]
    TreeSetStudent [name=EngineerYe, score=20]
    TreeSetStudent [name=EngineerZhong, score=21]

41、List(有序、可重复的集合)
        集合中每个元素都有其对应的顺序索引
        通过索引去访问位置的集合元素
        默认按元素的添加顺序设置元素的索引
        可以把元素放到指定的位置
        ArrayList 是线程不安全的,Vector 是线程安全的(不推荐)

    具体方法详解
        void add(int index, Object ele)
        添加对象到指定的索引
        boolean addAll(int index, Collection eles)
        添加一个集合到指定的索引
        Object get(int index)
        得到指定的索引号下的对象
        int indexOf(Object obj)
        查找指定对象的索引值 需要实现了equals 方法
        int lastIndexOf(Object obj)
        查找指定对象最后出现的索引值
        Object remove(int index)
        移除指定索引下的对象
        Object set(int index, Object ele)
        重新设置指定索引下的对象(replace 替换)
        List subList(int fromIndex, int toIndex)
        从指定位置分割出一个新的 List

几种遍历集合的方法

        //      Iterator 遍历
        //      Iterator iterator = list.iterator();    
        //      while(iterator.hasNext()){
        //          System.out.println(iterator.next());
        //      }

        //      ListIterator 遍历
        //      ListIterator lit = list.listIterator();
        //      while(lit.hasNext()){
        //          System.out.println(lit.next());
        //      }

        //      for循环遍历
        //      for(int i = 0;i<list.size();i++){
        //          System.out.println(list.get(i));
        //      }

        //      高级for循环遍历
        //      for(Object obj : list){
        //          System.out.println(obj);
        //      }

42、Map集合 (具有映射关系的集合)
        Map集合里保存着两组值,一组保存Key,另外一组保存Value
        Key不允许重复,即同一个Map对象的任何两个Key通过equals 方法比较中返回false
        可以通过Key找到value,但是不能通过value找到Key
        Map中的key和value都可以是任何引用类型的数据
        Key 和 Value 之间存在单向一对一的关系,即通过指定的Key 总能找到唯一的确定的Value
        HashMap 定义了 HashSet : HashMap 的键为HashSet 里的元素
    Properties 类 是 HashMap的子类(处理属性文件)
            FileName :jdbc.properties
            properties 文件在 Java中对应的一个是Properties 类的对象
    典型 HashMap
    程序代码块:                      
        Map map = new HashMap();
        //向Map放入元素 put(key,value)
        map.put("EngineerZ",new Person("EngineerZhong",20));
        map.put("EngineerY",new Person("EngineerYe",19));
        map.put("EngineerX",new Person("EngineerXie",19));
        map.put("EngineerZh",new Person("EngineerZhou",19));

        System.out.println(map.size());

// 遍历Map中的元素
        // 得到键的集合 keySet 方法
        Set KeySet = map.keySet();
        for(Object obj:KeySet){
            //利用键得到值 get(key)
            Object value = map.get(obj);
            System.out.println(obj + "value: " + value);
        }

        // 直接得到Map 中的 value 的集合
        Collection values = map.values();
        for(Object obj : values){
            System.out.println(obj);
        }

    //       得到键值对的集合(泛型)
    //      Map<String,Object> map = new HashMap<String,Object>();
    //      for(Map.Entry<String, Object> entry : map.entrySet()){
    //          String key = entry.getKey();
    //          Object val = entry.getValue();
    //          System.out.println("key: " + key + " value: "+ val);
    //      }

//遍历Map中的元素方法结束
    //移除元素
     map.remove("EngineerZh");
    System.out.println(map.size());

    //Contains()方法、isEmpty()方法 
    System.out.println(map.containsKey("EngineerZ"));
    System.out.println(map.isEmpty());

程序输出:
    4
    EngineerZh value: Person [name=EngineerZhou, age=19]
    EngineerZ value: Person [name=EngineerZhong, age=20]
    EngineerY value: Person [name=EngineerYe, age=19]
    EngineerX value: Person [name=EngineerXie, age=19]
    Person [name=EngineerZhou, age=19]
    Person [name=EngineerZhong, age=20]
    Person [name=EngineerYe, age=19]
    Person [name=EngineerXie, age=19]
    3
    true
    false
更新时间:2016年9月17日
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值