Ervin_o

导航

Java SE 学习笔记(集合框架的构成及分类,Math,System,泛型,Date,Runtime)

 

集合框架的构成分类:

 

List集合的具体子类

    子类之所以区分是因为内部结构存储数据的方式不同。

    vector 数据结构是数组。 数组是可变长度

去除重复元素方式:

    1.最后唯一性的元素有很多,可以先定义一个元素用于存储这些唯一性的元素

    2.对原有容器进行元素的获取,并到临时容器中判断是否存在。容器本身有这个功能,  判断元素是否存在

    3.存在则存储,不存在则不存储

    4.遍历完原容器后,临时容器中存储的就是唯一性的元素

 

代码演示: 去除重复元素:

    public static List SingleElements(List list){

      //  1.定义一个临时容器

      List temp =new Arraylist();

      // 2.遍历原容器

      for(Iterator it=list.Iterator(); it.hasNext();){

        Object obj=(Object)it.next();

        // 3.临时容器中判断遍历到的元素是否存在

        if(!temp.contains(obj)){

          // 4.如果不存在,就存储到临时容器中

           temp.add(obj);

        }

      }

      // 5. 将原容器清空

      list.clear();

      // 6.将临时容器中的元素都存储到原容器中

      list.addAll(temp);

    }

 

    用集合框架Arraylist取出自定义元素:

    public boolean equals(Object obj){

      if(this==obj)

        return true;

      if(!(obj instanceof person)){

        throw new ClassCastException("类型错误");

      }

      Person p=(Person)obj;

      return this.name.equals(p.name) && this.age=p.age;

    }

    

    LinkedList: 特殊的方法

    addFirst();     首行增加

    addLast();   末尾增加

    getFirst();   获取首行

    getLast();   获取末尾

    栈 : 先进后出  First In Last Out             FILO

    队列: 先进先出  First In First Out     FIFO

    

    Set  集合

     特点: 不允许重复,和collection 方法相同。set取元素只有Iterator

    Hash冲突的解决方案:

    "ab"的HashCode等于 "ba"的HashCode的时候

    使用equals 方法来判断值是否相等

    

    TreeSet 集合框架:  // 二叉树数据结构

      // 因为学生要排序就需要比较,而没有定义比较方法

      //比较add方法中使用的是comparable 接口的比较方法

      代码示例:

        ....

 

        @Override

        public int CompareTo(Object o){    // 改变TreeSet的自然排序

          if(this.age>stu.age)

            return;

          if(this.age<stu.age)

            return -1;

          return 0;          

        }

       //  返回值为1排前 0则不变 -1  后面

 

      类需要实现comparable, 重写compareTo方法。

        集合treeSet调用add方法时,会自动实现comparable接口

      

      TreeSet 比较器:

        使用方法:

          实现Comparator接口,覆盖compare 方法。将comparator接口的对象,作为参数传递给TreeSet集合的构造函数。

      需求:  元素具备了比较功能不是所需要具备的,就是说不想按照自然排序的方法。而是按照自定义的排序方式,对元素排序

      而且,存储到TreeSet中的元素万一没有比较功能,该如何排序呢?

                                                --解决方案   定义一个比较器

      代码演示:

        public class ComparatorByName implements Comparator{

          @Override

          public int compare(Object o1,Object o2){

            TreeSetStu t1=(TreeSetStu)o1;

            TreeSetStu t2=(TreeSetStu)o2;

            int temp=t1.getName().CompareTo(t2.getName());

            return temp==0?t1.getAge()-t2.getAge():temp;

          }  

        }

 

      代码块,main方法:

        // 声明一个TreeSet时,将比较器传入集合构造函数中

        TreeSet tree=new TreeSet(new ComparatorByName());

     

                  小结

      

      ArrayList:  数组结构,查询快。可重复,可以增删改查

      LinkedList: 链表结构,增删快

      HashSet:  哈希表,查询更快,不保证有序,不重复,覆盖HashCode,equals

      LinkedHashSet: 链表加哈希表 可以实现有序

      TreeSet: 二叉树,可以排序     

          1. 自然排序   Comparable

          2. 比较器      Comparator

      Enumberation  接口: 枚举

      Vector: 具备枚举取出方式的容器只有Vector

      for(Enumberation en=v.elements();en.hasMoreElements();){

        en.nextElement();

      }

      泛型类: 

        class Demo<T>{

         // 泛型方法

          public <Q> void print(Q w){

            System.out.println("print:"+w);

          }

        }

      //  静态方法是无法访问类上定义的泛型的。 如果静态方法需要定义泛型,泛型只能定义在方法

      public static <A> void staticShow(A a){

        System.out.println("static show:"+a);

      }

 

      泛型接口:

      语法:      

        interface Inter<T>{

          public void show(T t);

        }

      通配符:

        例子:

          Collection<T> coll=new Collection<?>();

          在类型不确定的情况下,使用通配符?保持两边一致

      泛型限定

          ? super E       泛型的下限,接口E类型或者E的子类型

          ? extends E    接收E类型或者E的父类型,上限

 

          例:// 遍历集合的方法、     // 限定只能遍历Person的子类或 Person

        private static void printCollection(Collection < ? extends Person > coll){

          for(Iterator <?> it=coll.iterator(); it.hasnext();){

          }

        }

      Map  集合

      HashTable :  哈希表,同步,不允许空值,键

      HashMap:  不同步,允许空值,键

      TreeMap:  二叉树,排序,对键排序

      TreeMap 比较器的使用:

        例: Comparator<Employee> comp=new Comparator<Employee>(){

            @Override

            public int compare(Employee o1,Employee o2){

              int temp=o1.getAge()-o2.getAge();

              return temp==0?o1.getName().compareTo(o2.getName()):temp;

            }

           }

        Map<Employee ,String> map=new TreeMap<Employee,String>(comp);    //  把比较器对象放入集合构造函数中

 

      LinkedHashMap  查询快,并且有序

        

      Ststem  类

        arraycopy();      //  复制数组

        currentTImeMillis();    // 返回以毫秒为单位的当前时间

        getProperties();        //  获取属性

        例:   Properties prop=System.getProperties();

            Set<String> keySet=prop.StringPropertyNames();

            for(String key: keySet){

              String values =prop.getProperty(key);

              System.out.println("名称"+key+"信息"+values);          

            }

 

     Utilities  工具类  

        集合框架的工具类:

          Collections: 定义的都是操作Collection 的静态方法

          1.对list 排序,自然排序

          Collections.sort(list);

          2.按照长度排序

          Collections.Sort(list,new ComparatorByLength());      // 比较器

          逆序:  reverseOrder()   强行逆转比较器

          Collection.sort(list,collections.reverserOrder(new ComparatorByLength()));

      

     获取集合 最值:

      public static <T extends Object & Comparable<? supper T>>T getMax(Collection<? extends T> coll){

        Iterator<? extends T> it=coll.Iterator();

        //  1.定义变量,记录容器中的一个

         T max=it.next();

        // 2.遍历容器中的所有元素

        while(it.next()){

          T temp=it.next();

          // 3.在遍历的过程中比较

          if(temp.compareTo(max)>0){

            max=temp;

          }

        }        

        return max;

      }

 

    同步方法:  synchronized

    

    可变参数:

      例子:  int sum=add(3,4,10,6,7,9);

       //  获得多个数值和

      public static int add(int... arr){

        int sum=0;

        for(int i=0;i<arr.Lenth;i++){

          sum+=arr[i];    

        }  

        return sum;

      }

 

    静态导入:

      例:Collections

      import static java.util.Collections.*;

      import static java.util.System.*;

 

    Runtime  单例模式的对象

      Runtime r=Runtime.getRuntime();

      r.exec("winmine.exe");

      r.exec("播放软件路径  视频文件路径" );

 

    Math 类

      Pow(double a,double e);  //  返回a的e次方

      ceil(double e);      //  返回上取整

      floor(double e);      // 向下取整

      Random(0.0~1.0);    // 随机数

    

    Date 类:

      //FULL : 2013年7月17日 星期三

      //LONG: 2013年7月17日

      //MEDIUM: 2013-7-17

      // SHORT: 13-7-17

      例:

        Date date=new Date();

        DateFormat dateFormat=new DateFormat.getDateInstance();

        // 加入风格

        dateFormat=DateFormat.getDateInstance(DateFormat.SHORT);

        String str=dateFormat.format(date);

      

          

      

          

          

      

        

     

      

    

posted on 2015-03-05 23:25  Ervin_o  阅读(164)  评论(0)    收藏  举报