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<?

> 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.

上图中,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); }
} }

        

浅谈关于collection接口及相关容器类(一)的更多相关文章

  1. 浅谈Java中接口与抽象类的异同

    浅谈Java中接口与抽象类的异同 抽象类和接口这两个概念困扰了我许久,在我看来,接口与抽象类真的十分相似.期间也曾找过许许多多的资料,参考了各路大神的见解,也只能是简简单单地在语法上懂得两者的区别.硬 ...

  2. 浅谈JavaScript原型对象与相关设计模式

    引言 本文先从介绍JavaScript中基本的几种设计模式开始,最后引出原型对象,然后对原型对象有一个较全面的介绍. 1.创建对象的几种设计模式 A.工厂模式 我们知道在JavaScript中创建对象 ...

  3. 为什么这些java接口没有抽象方法?浅谈Java标记接口

    在jdk的源码中,存在这样的一些接口,他们不包含任何的(抽象)方法,但是却广泛的存在. 这种接口我们称之为Mark Interface,也就是标记接口. 这些接口呢,我们不用来实现任何的方法,他们的作 ...

  4. [iOS]浅谈NSRunloop工作原理和相关应用

    一. 认识NSRunloop  1.1 NSRunloop与程序运行 那么具体什么是NSRunLoop呢?其实NSRunLoop的本质是一个消息机制的处理模式.让我们首先来看一下程序的入口——main ...

  5. 浅谈Java Future接口

    Java项目编程中,为了充分利用计算机CPU资源,一般开启多个线程来执行异步任务.但不管是继承Thread类还是实现Runnable接口,都无法获取任务执行的结果.JDK 5中引入了Callable和 ...

  6. 浅谈java中接口与抽象类之间的异同

    刚学习java的时候,总觉得接口和抽象类很像,但又说不上具体有什么区别.今天静下来,翻翻书,查查资料,做个小结.首先举两个例子,看看interface和abstract class 在“外形”上有啥异 ...

  7. 浅谈JSON与与JS相关的JSON函数

    本文内容主要引用在微信公众号上看到的一片文章,因为自己对Json了解不是很深入,所以就整理出这篇博文与大家分享! 一. JSON是一种格式,基于文本,优于轻量,用于交换数据 1.一种数据格式 数据的传 ...

  8. 示例浅谈PHP与手机APP开发,即API接口开发

    示例浅谈PHP与手机APP开发,即API接口开发 API(Application Programming Interface,应用程序接口)架构,已经成为目前互联网产品开发中常见的软件架构模式,并且诞 ...

  9. 浅谈Android系统进程间通信(IPC)机制Binder中的Server和Client获得Service Manager接口之路

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6627260 在前面一篇文章浅谈Service ...

随机推荐

  1. 51nod 1413:权势二进制

    1413 权势二进制 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 一个十进制整数被叫做权势二进制,当他的十进制 ...

  2. [POI 2007] 旅游景点

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1097 [算法] 首先,用Dijkstra算法求出2-k+1到每个点的最短路 然后,我 ...

  3. 【SDOI 2016】 排列计数

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4517 [算法] 有m个数在原来的位置上,说明有(n-m)个数不再原来的位置上 那么, ...

  4. Java基础之关键字

    一.Java关键字总览 基本数据类型(9):boolean.char.byte.short.int.long.float.double.null 变量引用(2):super.this 类.方法.变量修 ...

  5. 抓取git的log文件批处理命令示例

    @echoset sincedate="2016-04-28 00:00:01"          ::变量set beforedate="2016-04-29 00:0 ...

  6. 依赖注入Unity框架

    依赖注入和控制反转是对同一件事情的不同描述,从某个方面讲,就是它们描述的角度不同.依赖注入是从应用程序的角度在描述,可以把依赖注入描述完整点:应用程序依赖容器创建并注入它所需要的外部资源:而控制反转是 ...

  7. sql导出到excel

    1.先查询数据ID,别名  导出到excel 2.增加标准名称,标识错误数据,导入sqlServer select distinct [StandardName] from [GMSOA].[dbo] ...

  8. 使用最新vue_cli搭建的模版

    使用最新vue_cli搭建的模版,包含了常用的插件,router和axiox与测试插件.项目的结构如下: 使用之前请打开 REAMME.md 看看. 已经搭建好的框架的下载地址:https://sha ...

  9. Windos下的6种IO模型简要介绍

    windows进行数据的收发有6种IO模型.分别是阻塞(blocking)模型,选择(select)模型,异步选择(WSAAsyncSelect)模型,事件选择(WSAEventSelect )模型, ...

  10. Android琐碎知识点集合

    1.最近发现android studio更新之后用的v7包,每次创建Activity的时候自动继承的是AppCompatActivity,很不舒服,还是习惯Activity.没什么大的毛病,毕竟goo ...