javaSE-常用类-集合类Collection

来源:互联网 发布:linux切换root用户 编辑:程序博客网 时间:2024/06/04 18:23

集合类

为什么出现集合类?
面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式。
数组和集合类同是容器,有何不同?
数组虽然也可以存储对象,但长度是固定的;集合长度是可变的。数组中可以存储基本数据类型,集合只能存储对象。
集合类的特点
集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象。

集合框架的构成及分类


集合框架中的常用接口

Collection接口有两个子接口:
List(列表) ,Set(集)

List:可存放重复元素,元素存取是有序的。
Set:不可以存放重复元素,元素存取是无序的。
public static void collectionAllDemo() {/* * 创建两个集合对象。并添加元素。 */Collection c1 = new ArrayList();Collection c2 = new ArrayList();c1.add("abc1");c1.add("abc2");c1.add("abc3");c1.add("abc4");c2.add("abc1");c2.add("abc2");c2.add("abc3");c2.add("abc4");// 演示添加集合。// c1.addAll(c2);//将c2中的元素都添加到c1中。// 判断是否包含一个容器中的元素呢?// boolean b = c1.containsAll(c2);// System.out.println("b="+b);// 删除一个容器中的所有元素。// boolean b = c1.removeAll(c2);//将c1中与c2中相同的元素从c1中删除。// System.out.println("b="+b);//boolean b = c1.retainAll(c2);// 将c1与c2中相同的元素保留到c1中,不同的被删除。System.out.println("b=" + b);System.out.println("c1:" + c1);System.out.println("c2:" + c2);}public static void collectionDemo(Collection coll) {// 1,添加元素。coll.add("abc1");coll.add("abc2");coll.add("abc3");// 2,删除元素。删除一个。// coll.remove("abc2");// 3,清除元素。删全部。// coll.clear();System.out.println(coll.contains("abc1"));System.out.println(coll.isEmpty());System.out.println(coll);}

迭代器

迭代是取出集合中元素的一种方式。
因为Collection中有iterator方法,所以每一个子类集合对象都具备迭代器。
用法:


public static void main(String[] args) {// 创建容器对象。Collection coll = new ArrayList();// 添加元素。coll.add("abc1");coll.add("abc2");coll.add("abc3");coll.add("abc4");// 要获取容器中的元素,必须要先获取该容器中的迭代器。通过iteratr()方法完成。Iterator it=coll.iterator();while (it.hasNext()) {System.err.println(it.next());}/* * for(Iterator it = coll.iterator(); it.hasNext(); ){ * System.out.println(it.next()); } */// System.out.println(it.next());// System.out.println(it.next());// System.out.println(it.next());// System.out.println(it.next());// System.out.println(it.next());//NoSuchElementException}

java.util

Interface Collection<E>

  • Method Summary

    Methods Modifier and TypeMethod and Descriptionbooleanadd(E e)
    Ensures that this collection contains the specified element (optional operation).
    booleanaddAll(Collection<? extends E> c)
    Adds all of the elements in the specified collection to this collection (optional operation).
    voidclear()
    Removes all of the elements from this collection (optional operation).
    booleancontains(Object o)
    Returns true if this collection contains the specified element.
    booleancontainsAll(Collection<?> c)
    Returns true if this collection contains all of the elements in the specified collection.
    booleanequals(Object o)
    Compares the specified object with this collection for equality.
    inthashCode()
    Returns the hash code value for this collection.
    booleanisEmpty()
    Returns true if this collection contains no elements.
    Iterator<E>iterator()
    Returns an iterator over the elements in this collection.
    booleanremove(Object o)
    Removes a single instance of the specified element from this collection, if it is present (optional operation).
    booleanremoveAll(Collection<?> c)
    Removes all of this collection's elements that are also contained in the specified collection (optional operation).
    booleanretainAll(Collection<?> c)
    Retains only the elements in this collection that are contained in the specified collection (optional operation).
    intsize()
    Returns the number of elements in this collection.
    Object[]toArray()
    Returns an array containing all of the elements in this collection.
    <T> T[]toArray(T[] a)
    Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.
java.lang

Interface Iterable<T>

Method Summary

Methods Modifier and TypeMethod and DescriptionbooleanhasNext()
Returns true if the iteration has more elements.
Enext()
Returns the next element in the iteration.
voidremove()
Removes from the underlying collection the last element returned by this iterator (optional operation).

迭代器模式

内部通过new了一个内部类暴露给外界  使用  符合这种情况的遍历情况都属于迭代器模式

0 0