黑马程序员_java_02_集合框架

来源:互联网 发布:淘宝友臣肉松饼是假的 编辑:程序博客网 时间:2024/05/16 01:12
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


1.集合框架类的继承关系

Collection接口中定义的方法在下面子类中公用的方法:

Iterator<E> iterator() 
          返回在此 collection 的元素上进行迭代的迭代器。

返回iterator使用while循环遍历集合

 void clear() 
          移除此 collection 中的所有元素(可选操作)。

boolean contains(Object o) 
          如果此 collection 包含指定的元素,则返回 true  

boolean equals(Object o) 
          比较此 collection 与指定对象是否相等。 

boolean isEmpty() 
          如果此 collection 不包含元素,则返回 true 判断Collection是否为空。

int size() 
          返回此 collection 中的元素数 

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

Collection是集合框架的之间定义着,根据数据结构的不同又分为List和Set接口。其中List为有序数据存储,内部数据可以重复。

Set为内部存储数据为无序排列(没有角标值),内部存储数据不可以重复。

 

Iteroter

在集合每个子类(子接口)中取数据的方式不同。java把取数据的方法抽取封装成了一个特定的接口。在Collection接口中定义Ireroter方法返回Iteroter对象,每个子集合自己去描述切出数据的实现形式。

 ArrayList集合

为有序集合,可以使用索引值取出特定数据,故能够使用for循环遍历数据,取出数据。但是该集合也是Collection的子集合。故也可以使用Iteroter 对象遍历数据。

ArrayList;底层实现原理是数组,存放的元素都有固定的下标值,查询修改的时候比较快,删除,增加元素的时候比较慢

List集合特有的迭代器ListIterator是Iterator的子接口

在迭代时,不可以通过集合对象的方法操作集合中的元素。因为会发生异常。

 所以,在迭代时,只能用迭代器的方法操作元素,可是Iterator的方法是有限的,只能对元素进行判断,取出,删除的操作,

如果想要其他的操作,如添加,修改,就需要使用其子接口,ListItertor

该接口只能通过List集合ListItrator

使用iterator变量Array集合

import java.util.ArrayList;import java.util.Iterator;class ArrayListDemo {public ArrayList<String> arrayList=new ArrayList<String>();public static void main(String[] args){ArrayListDemo arry=new ArrayListDemo();arry.iteratorList();}public ArrayListDemo(){//arrayList;arrayList.add("张一");arrayList.add("张二");arrayList.add("张三");arrayList.add("张四");arrayList.add("张五");arrayList.add("张六");}public void iteratorList(){Iterator iterator=arrayList.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}}}


使用listIterator遍历arrayList

package com.db.xianc;import java.util.ArrayList;import java.util.Iterator;import java.util.ListIterator;class ArrayListDemo {public ArrayList<String> arrayList=new ArrayList<String>();public static void main(String[] args){ArrayListDemo arry=new ArrayListDemo();arry.ListIteratorList();}public ArrayListDemo(){//arrayList;arrayList.add("张一");arrayList.add("张二");arrayList.add("张三");arrayList.add("张四");arrayList.add("张五");arrayList.add("张六");}public void iteratorList(){Iterator iterator=arrayList.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}}public void ListIteratorList(){ListIterator<String> listIterator=arrayList.listIterator();while(listIterator.hasNext()){Object aa= listIterator.next();System.out.println(aa);if(aa.equals("张二")){listIterator.add("张2.5");}}}}

LinkedList;底层实现原理是链表,查询的时候比较慢,删除,增加元素的时候比较快。Vector;与arraylist功能一样,不过arraylist出现的早,他出现时的时候还没有集合框架,该集合是线程同步的,使用起来比较慢,但是比较安全。现在优先使用arraylist。

Set 集合元素不可以重复,存放的数据是无序的,线程是非同步的。

Set集合有连个子类hashSet和TreeSet

HashSet集合底层实现原理是哈希表,判断元素是否唯一的方式是先判断hashcode值是否相同,再使用equals方法判断是否为同一对象。如果hashcode值不同,则不执行equals方法。对于判断元素是否存在,以及删除等操作,依赖的方法是元素的hashcode方法和equals方法。

TreeSet集合底层实现原理是二叉树,判断元素是否唯一的方式是compareTo()方法返回值是否为0。TreeSet集合可以对存放的元素进行排序。排序的依据的技术原理是让存放的对象具有比较性。使存放的对象实现compareble接口。复写compareto方法。compareto方法返回值如果为负数则存放数据时在二叉树左边位置存放。返回值为0时则认为元素重复,元素不被存储。返回值为正数时则存放数据时在二叉树的右边位置存储。

TreeSet集合实现排序的第二种方式

自定义类实现comparator复写compare方法自定义排序方式。

在创建TreeSet集合的时候把comparator接口子类传入构造方法参数实现排序。

例子:

使用TreeSet集合存储字符串并按照字符串的长度排序。

importjava.util.*;classTreeSetText{      public static void main(String[] args){           TreeSet ts=new TreeSet(newstrLnComparator());           ts.add("a");           ts.add("sd");           ts.add("bbbbbs");           ts.add("eee");           ts.add("ggas");           ts.add("gges");           Iterator iterator=ts.iterator();           while(iterator.hasNext()){                 System.out.println(iterator.next());           }      }}classstrLnComparator implements Comparator{           public int compare(Object o1,Object o2){           String s1=(String)o1;           String s2=(String)o2;           /*           if(s1.length()>s2.length()){                 return 1;           }           if(s1.length()==s2.length()){                 return 0;           }           return -1;           */           int num=newInteger(s1.length()).compareTo(new Integer(s2.length()));           if(num==0)                 return s1.compareTo(s2);           return num;      }   }


注意:

在实现compare方法时返回0的情况下,必须判断次要条件(字符串内容是否一样)。否则长度相同的字符串只能存储一条信息。

0 0
原创粉丝点击