javaSE-常用类-集合类Collection
来源:互联网 发布:linux切换root用户 编辑:程序博客网 时间:2024/06/04 18:23
集合类
为什么出现集合类?
面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式。
数组和集合类同是容器,有何不同?
数组虽然也可以存储对象,但长度是固定的;集合长度是可变的。数组中可以存储基本数据类型,集合只能存储对象。
集合类的特点
集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象。
集合框架的构成及分类
集合框架中的常用接口
Collection接口有两个子接口:
List(列表) ,Set(集)
List:可存放重复元素,元素存取是有序的。
Set:不可以存放重复元素,元素存取是无序的。
Modifier and Type Method and Description
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方法,所以每一个子类集合对象都具备迭代器。
用法:
因为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
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
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
- javaSE-常用类-集合类Collection
- javaSE-常用类-集合类List系列
- javaSE-常用类-集合类Set系列
- Java集合类:Collection接口常用方法
- 【javase复习】## day7 集合Collection ##
- JavaSE总结 -集合框架collection
- Javase—集合框架Collection
- JavaSE入门学习34:Java集合框架之Collection接口、子接口及其实现类
- javaSE 集合类学习
- 集合类:单列集合--Collection
- 集合类(Collection)
- Collection集合类方法
- Java--collection集合类
- collection--集合类
- 集合类Collection
- Collection Map 集合类
- 集合类介绍Collection
- Collection集合类
- C#的四种排序方法
- UIView概念及创建UIView
- NSString / NSData 和图片之间的转换
- Android中获取应用程序(包)的信息-----PackageManager的使用
- linux基础:tty pty pts tts介绍
- javaSE-常用类-集合类Collection
- 冒泡排序时间复杂度和算法
- python的lambda函数介绍
- 【深入Java虚拟机】之一:Java内存区域与内存溢出
- IcePHP框架中的快速后台中的通用CRUD功能框架(四) 表配置文件
- 山东大学软件学院计算机组成原理实验4
- android 上的imageview 图片全屏显示
- 如何在service中弹出dialog
- mini2440学习入门