浅谈关于collection接口及相关容器类(一)
2024-09-07 19:33:29
Collection在英文单词的意思是:採集,收集。
而在JAVA API 文档中它是一个收集数据对象的容器。
Collection作为容器类的根接口。例如以下图(部分子接口未写出):
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGVyb2Rz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
文档中说明了Collection接口,有例如以下的方法:
boolean |
add(E e)
Ensures that this collection contains the specified element (optional operation).
|
boolean |
addAll(Collection<? extendsE> 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<?
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.
|
上图中,Set与List、Map都是Collection的子类。
它们的差别在于,Set实现的类仅仅能存放无序、无反复的数据对象。而List仅仅存放的是有序,可反复的数据对象。Map接口定义了存储着"键(key)-值(value)映射对的方法。
以下代码示范List继承collection接口并实现它部分的相关方法:
<pre name="code" class="java"> package CrazyStone; import java.util.ArrayList;
import java.util.List;
public class DemoList {
public static void main(String[] args) {
int value;
List list=new ArrayList(); //定义List类对象list,并实现父类引用。
list.add("CSDN"); //加入数据对象方法;
list.add("GB记忆");
System.out.println( list.isEmpty()); //推断list容器是否为空
value=list.size(); //返回容器的大小;
for(int i=0;i<value;i++) //遍历容器。
{ String str =(String) list.get(i);
System.out.println(str); }
System.out.println("---------------------");
//跟顺序有关的操作
list.remove(0); //在容器中移出CSDN。但并未删除。
list.set(0, "CSDNGO"); //向容器中插入对象;
list.add("加油。");
value=list.size();
for(int i=0;i<value;i++) //遍历容器;
{ String str =(String) list.get(i);
System.out.println(str); }
} }
最新文章
- text-overflow
- Eclipse cpp 开发 include路径
- SpringMVC对日期类型的转换
- JS子父窗口互相取值赋值详解介绍
- 最大熵模型(Maximum Etropy)—— 熵,条件熵,联合熵,相对熵,互信息及其关系,最大熵模型。。
- Sqlerver_各类函数
- vyos (三) HA
- [转]使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 事件详解
- Redis的快照功能
- Conversion to Dalvik format failed: Unable to execute dex
- pageoffice 开发笔记
- PHP cookie禁用时session 方案
- 在mac上安装Docker
- .net敏捷开发框架 力软(learun) 让开发变的更简单
- nginx的stream反向代理mysql配置
- 102. Binary Tree Level Order Traversal 广度优先遍历
- C# 创建 写入 读取 excel
- 稀疏数据压缩查询方法:Rank &; Select 操作
- CSS中em,rem的区别
- Tomcat启动阻塞变慢
热门文章
- java中File的delete()方法删除文件失败的原因
- 使用Dropzone上传图片及回显演示样例
- ARP协议(3)ARP编程--winpcap&;amp;vs2012配置
- php PDO连接mysql
- 王立平--TF卡
- cocos2d 3.3 lua 代码加密 luac
- 2016.03.02,英语,《Vocabulary Builder》Unit 03
- 重构版机房收费系统之分层、接口、数据库连接、反射+工厂(vb.net)
- Oracle 11G R2 RAC中的scan ip 的用途和基本原理
- 运算符 and or ont