黑马程序员——集合

来源:互联网 发布:js给数组添加一个对象 编辑:程序博客网 时间:2024/05/23 18:00
                                                                                -------android培训、java培训、期待与您交流! ----------

一、集合

         1.集合的由来

                   由于面向对象的语言操作数据的最基本单位就是对象。

                   而有些时候为了对多个对象进行操作,我们就必须对这多个对象进行存储和取出。

                   用以前学习过的数组可以实现,但是,数组的长度是固定的,所以java就提供了

                   集合供我们使用。

         2.集合的特点

                   A:长度可以发生改变

                   B:可以存储不同类型的对象

                   C:只能存储对象类型

         3.集合和数组的区别

                   集合:

                            长度可以改变

                            可以存储不同类型的对象

                            只能存储对象类型

                   数组:

                            长度固定

                            只能存储同一种类型的元素

                            可以存储基本类型,也可以存储对象类型

         4.集合体系的由来

                   A:由于数据结构不同,所以,java就会提供多种集合供我们使用,

                     而不管这些集合是怎么存储和取出的,它们都有共性的内容,通过

                     不断的向上抽取,最终形成了集合的继承体系结构图。

                            数据结构:存储数据的方式

                   B:集合的体系图:

                            Collection

                                     |--List

                                               |--ArrayList

                                               |--Vector

                                                              |--LinkedList

                                     |--Set

                                               |--HashSet

                                               |--TreeSet

         5Collection接口中的功能概述

                   A:添加功能

                            booleanadd(Object obj)        确保此collection 包含指定的元素

         boolean addAll(Collection c)   将指定 collection 中的所有元素都添加到此collection

                   B:删除功能

                            booleanremove(Obejct obj)     从此 collection中移除指定元素的单个实例

                            booleanremoveAll(Collection c)移除此 collection 中那些也包含在指定 collection中的所有元素

                            voidclear()                   移除此collection 中的所有元素

                   C:判断功能

booleancontains(Object obj)   如果此collection 包含指定的元素,则返回 true

booleancontainsAll(Collection c)如果此 collection包含指定 collection 中的所有元素,则返回 true

                        booleanisEmpty()              如果此 collection不包含元素,则返回true

                   D:迭代器功能

                            Iteratoriterator()

                                     |--booleanhasNext()       如果仍有元素可以迭代,则返回 true

                                     |--Objectnext()        返回迭代的下一个元素

                   E:长度

                            intsize()                    返回此collection 中的元素数

 

                   F:交集

                            booleanretainAll(Collection c)        仅保留此collection 中那些也包含在指定                          collection的元素

 

                   G:转成数组

                            Object[]toArray()             返回包含此collection 中所有元素的数组。

         6.迭代器的使用:(理解)

                   A:就是集合的一种遍历方式。

                   B:使用:必须以集合作为前提。

                   C:原理:请参照迭代器的原理.bmp

         7.代码体现:(掌握)

                   集合使用的步骤:

                            a:创建集合对象

                            b:创建元素对象

                            c:添加元素

                            c:遍历集合

                                     **通过集合对象调用iterator()方法获取迭代器对象

                                     **通过迭代器对象的hasNext()进行判断是否有元素

                           

                                     **通过迭代器对象的next()进行获取元素

 

 

                   A:集合存储字符串并遍历

                           

                            Collectionc = new ArrayList();

 

                            //Strings = new String("hello");

 

                            c.add("hello");

                            c.add("world");

                            c.add("java");

 

                            Iteratorit = c.iterator();

                            while(it.hasNext())

                            {

                                     Strings = (String)it.next();

                                     System.out.println(s);

                            }

                           

 

                   B:集合存储自定义对象并遍历

                           

                            a:先写一个类。

                             我假设这个类已经存在。

                            b:代码测试:

 

                            Collectionc = new ArrayList();

 

                            Students1 = new Student("hello",20);

                            Students2 = new Student("world",30);

                            Students3 = new Student("java",40);

 

                            c.add(s1);

                            c.add(s2);

                            c.add(s3);

 

                            Iteratorit = c.iterator();

                            while(it.hasNext())

                            {

                                     Students = (Student)it.next();

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

                            }

二、List及其子类

1List的体系结构及特点

                   List元素有序(存储和取出的顺序一致),元素可以重复

                            |--ArrayList

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

                                    线程不安全,效率高

                            |--Vector

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

                                    线程安全,效率低

                            |--LinkedList

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

                                    线程不安全,效率高

         2.什么时候该使用那种集合

                   是否线程安全:

                            是:Vector(一般也不用,会有更好的方式)

                            否:ArrayList,LinkedList

                                    查询多:ArrayList

                                    增删多:LinkedList

 

                   当你不知道用谁,请用ArrayList

         3List接口的特殊功能

                   A:添加功能

                            add(intindex,Object obj)

                   B:删除功能

                            remove(intindex)

                   C:修改功能

                            set(intindex,Object obj)

                   D:获取功能

                            get(intindex)

                   E:列表迭代器

                            ListIteratorlistIterator()

         4List存储字符串并遍历(加入泛型)

                   List<String>list = new ArrayList<String>();

 

                   list.add("hello");

                   list.add("world");

                   list.add("java");

 

                   Iterator<String>it = list.iterator();

                   while(it.hasNext())

                   {

                            Strings = it.next();

                            System.out.println(s);

                   }

 

                   for(intx=0; x<list.size(); x++)

                   {

                            Strings = list.get(x);

                            System.out.println(s);

                   }

         5List存储自定义对象并遍历List:(存取字符串)

创建一个类(Student):

         publicclass Student{

                   privateString name;

                   privateint age;

                   publicStudent();

                   publicStudent(String name,int age){

                                     this.name= name;

                                     this.age= age;

                            }

                   publicString getName() {

                            returnname;

                   }

 

                   publicvoid setName(String name) {

                            this.name= name;

                   }

 

                   publicint getAge() {

                            returnage;

                   }

 

                   publicvoid setAge(int age) {

                            this.age= age;

                   }

         }

 

         创建一个测试类:(ListDemo2.java

 

         importjava.util.List;

         importjava.util.Iterator;

         publicclass ArrayListDemo2{

                   publicstatic void main(String[] args){

                                     //创建集合对象

                                     List<Student>array = new List<Student>();

                                     //创建元素对象

                                     Students1 = new Student("张三",22);

                                     Students2 = new Student("李四",25);

                                     Students3 = new Student("王五",28);

                                     //添加元素

                                     array.add(s1);

                                     array.add(s2);

                                     array.add(s3);

                                     //遍历集合

                                     Iterator<Student>it = array.iterator();

                                     while(it.hasNext()){

                                               Students = it.next();

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

                                     }

                                     System.out.println("-------------------------------");

                                     //遍历集合

                                     for(intx = 0;x<array.size();x++){

                                               Studentss = array.get(x);

                                               System.out.println(ss.getName()+"\t"+ss.getAge());

                                               }

                            }

         }

三、Set集合

         1.Set集合的特点:元素无序,唯一。

         2.Set的子类:(理解)

                   Set

                            |--HashSet

                                    底层数据结构是哈希表。

                                    是如何保证元素的唯一性的呢?

                                              依赖两个方法:hashCode()equals()

                                              首先判断hashCode()值是否相同。

                                                       是:

                                                                继续判断equals()的返回值

                                                                           true:元素重复。

                                                                           false:添加到集合。

                                                       否:

                                                                直接添加到集合。

                                     |--LinkedHashSet

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

                                              由哈希表保证元素的唯一。

                                              由链表保证元素的有序。

                            |--TreeSet

                                    底层数据结构是二叉树。

                                    是如何保证元素的唯一性和排序的呢?

                                              是根据自然排序或者比较器的返回值是否是0决定。

                                              排序有两种方式:

                                                       自然排序:

                                                                对象具备比较性,自定义类实现Comparable接口

                                                       比较器排序:

                                                                集合具备比较性,接收Comparator的构造参数

         3.HashSet代码体现

                   需求:存储自定义对象,要求成员变量值都相同即为同一个对象。

 

                   publicclass Student

                   {

                            privateString name;

                            privateint age;

 

                            publicStudent(){}

 

                            publicStudent(String name,int age)

                            {

                                     this.name= name;

                                     this.age= age;

                            }

                           

                            //...getXxx()/setXxx()

 

                            publicint hashCode()

                            {

                                     returnthis.name.hashCode()+this.age*13;

                            }

 

                            publicboolean equals(Object obj)

                            {

                                     if(this==obj)

                                     {

                                               returntrue;

                                     }

 

                                     if(!(objinstanceof Student))

                                     {

                                               returnfalse;

                                     }

 

                                     Students = (Student)obj;

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

                            }

                   }

 

                   实际操作代码:

                            importjava.util.HashSet;

                            importjava.util.Iterator;

                            publicclass HashSetTest

                            {

                                     publicstatic void main(String[] args)

                                     {       

                                               HashSet<Student>s=new HashSet<Student>();

                                               Students1=new Student("",22);

                                               Students2=new Student("",21);

                                               Students3=new Student("",23);

                                               s.add(s1);

                                               s.add(s2);

                                               s.add(s3);

                                               Iterator<Student>it=s.iterator();

                                               while(it.hasNext())

                                               {

                                                        Studentss=it.next();

                                                        System.out.println(ss.getName()+ss.getAge());

                                               }       

                                     }

                            }

                           

四、Map

 

         1.Map的特点(了解)

                   是由键值对形成的一个集合。

                   键是唯一的。值可以重复。

数据结构只是对键有效。

         2.MapCollection的区别(了解面试题)

                   A:Map

                            Map是键值对形式的集合,数据是成对出现的。

                            Map的键是唯一的。

                            Map看成是夫妻对。

                   B:Collection

                            Collection是单列形式的集合,数据是单一出现的。

                            Collection的儿子Set的元素是唯一的。

                            Collection看成是单身汉。

         3.Map的功能概述

 

                   A:添加功能

                         V put(K key, V value):把指定的键值对元素添加到集合中

 

 

                 B:删除功能

                         void clear():移除所有键值对元素

                         V remove(Object key):根据指定的键删除键值对元素

 

 

                 C:判断功能

                           booleancontainsKey(Object key):判断集合中是否包含指定的键

                           booleancontainsValue(Object value):判断集合中是否包含指定的值

                           boolean isEmpty():判断集合是否为空

                   D:获取功能

                            Set<Map.Entry<K,V>>entrySet()

                           V get(Object key):根据键获取值

                            Set<K> keySet():获取所有键的集合

                            Collection<V>values():获取所有值的集合

                   E:长度功能

                            intsize():键值对的对数

        

 

原创粉丝点击