天天看点

黑马程序员 JAVA基础<四> 集合

                    ------- android培训、java培训、期待与您交流! ----------

1 Collection(add,remove,contains,size,iterator)

 |--List(有序,可重复 add(index),get(index),set(index),remove(index))

         |--ArrayList

                底层数据结构是数组,查询快,增删慢。

                线程不安全,效率高。

         |--Vector

               底层数据结构是数组,查询快,增删慢。

               线程安全,效率低。

         |--LinkedList

               底层数据结构是链表,查询慢,增删快。

                线程不安全,效率高。

|--Set(无序,唯一)

            |--HashSet

                  底层数据结构是哈希表。

                  如何保证元素唯一性呢?

                 它依赖两个方法,hashCode()和equals()方法。

                  首先根据hashCode()进行比较:

                  相同:继续,equals()方法

                 如果返回true,不添加

                    如果返回false,添加

               不相同:直接添加到集合中。

           |--LinkedHashSet

                 底层数据结构是哈希表和链表。

                哈希表保证元素唯一。

                  链表保证元素有序。

           |--TreeSet

                底层数据结构是二叉树。

                如何保证元素唯一性呢?

                根据自然排序或比较器返回的最终结果是不是0来确定元素是否重复。

                 如果是0,不添加。否则,添加。

                 怎么实现排序的呢?

            方式1:让元素本身具备比较性

              实现Comparable接口

            方式2:让集合具备比较性

             实现Comparator接口

             比较接口:

             Comparable:自然排序比较,被元素本身实现。

             Comparator:比较器接口,用于集合实现。

2:集合的使用

集合的使用步骤:

        A:创建集合对象

        B:创建元素对象

        C:把元素对象添加到集合对象中

        D:遍历集合

       遍历的方式:

           Collection:

                      迭代器:

                      增强for:

         |--List

           |--get()和size()结合

          |--Set

List:如果需要索引,那么用get()方式。否则,就用Collection的方式。

为了方便我们一般使用for。

3:什么时候使用哪种集合:

元素是否唯一?

          是:Set

元素需要排序吗?

           需要:TreeSet

          不需要:HashSet

如果不知道,用HashSet

           不是:List

线程是否安全:

         安全:Vector

         不安全:ArrayList,LinkedList

查询多:ArrayList

增删多:LinkedList

       如果不知道,用ArrayList。

        如果不知道,用ArrayList。

4:集合中用到的数据结构总结

        ArrayXxx:底层数据结构是数组,查询快,增删慢

        LinkXxx:底层数据结构是链表,查询慢,增删快

        HashXxx:底层数据结构是哈希表,跟hashCoe()和equals()有关。

         TreeXxx:底层数据结构是二叉树,跟Comparable和Comparator有关。

代码一:用迭代器遍历数组

   Iterator it = array.iterator();

  while (it.hasNext()) {

   Student s = (Student) it.next();

   System.out.println(s.getName() + "***" + s.getAge());

  }

  代码二:HashSet保持唯一性要重写HashCode()和equals()方法

例子:

public int hashCode() {

  return this.name.hashCode()+this.age*17;

 }

 @Override

 public boolean equals(Object obj) {

  if (this == obj) {

   return true;

  }

  if (!(obj instanceof Student)) {

   return false;

  }

  Student s = (Student) obj;

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

 }

代码三:TreeSet 保持唯一性要实现comparable 或者comparetor 例子:

public static void main(String[] args) {

  TreeSet<Student> tree = new TreeSet<Student>(new Comparator<Student>(){

   @Override

   public int compare(Student o1, Student o2) {

    // TODO Auto-generated method stub

    int num = o1.getName().length()-o2.getName().length();

    int num2 =(num ==0)?o1.getAge()-o2.getAge():num;

    int num3 = (num2==0)?o1.getName().compareTo(o2.getName()):num2;

    return num3;

   }

  });

           ------- android培训、java培训、期待与您交流! ----------