javaSE-常用类-集合类Collection

集合类

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

集合框架的构成及分类


集合框架中的常用接口

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 Type Method and Description
    boolean add(E e)
    Ensures that this collection contains the specified element (optional operation).
    boolean addAll(Collection<? extends E> c)
    Adds all of the elements in the specified collection to this collection (optional operation).
    void clear()
    Removes all of the elements from this collection (optional operation).
    boolean contains(Object o)
    Returns  true if this collection contains the specified element.
    boolean containsAll(Collection<?> c)
    Returns  true if this collection contains all of the elements in the specified collection.
    boolean equals(Object o)
    Compares the specified object with this collection for equality.
    int hashCode()
    Returns the hash code value for this collection.
    boolean isEmpty()
    Returns  true if this collection contains no elements.
    Iterator<E> iterator()
    Returns an iterator over the elements in this collection.
    boolean remove(Object o)
    Removes a single instance of the specified element from this collection, if it is present (optional operation).
    boolean removeAll(Collection<?> c)
    Removes all of this collection's elements that are also contained in the specified collection (optional operation).
    boolean retainAll(Collection<?> c)
    Retains only the elements in this collection that are contained in the specified collection (optional operation).
    int size()
    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 Type Method and Description
boolean hasNext()
Returns  true if the iteration has more elements.
E next()
Returns the next element in the iteration.
void remove()
Removes from the underlying collection the last element returned by this iterator (optional operation).

迭代器模式

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值