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. java中文件路径读取

    windows下 1)相对路径 public static final String TestDataExcelFilePath="src/omstestdata.xlsx"; 2 ...

  2. node-webkit 屏幕截图功能

    做 IM 屏幕截图是少不了的,之前 windows 版本是调用的 qq 输入法的截图功能,这个版本又再次尝试自己实现发现是可以的,getusermedia 的权限很高,代码如下 <!DOCTYP ...

  3. Electron 问题

    打包 Mac 桌面程序时报上述错误 E:\research\node\XXX>electron-packager ./ XXX --platform=darwin --arch=x64 --ic ...

  4. 软件测试中的fault,error,failure

    问题:给定两段代码,设计fault,error,failure的测试用例. fault:即引起错误的原因,类似病因. error:类似疾病引起的内部结果. failure:类似疾病引起的症状. 代码1 ...

  5. PopupWindow实现点击外部不消失

    View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.alert_ip, null); final Po ...

  6. popupwindows

    <1>.能够点击popupwindow 内部空间,外部触摸消失,外部点击事件. package myapplication.com.myapplication; import androi ...

  7. Java基础——Servlet

    什么是Servlet Servlet是Java Web的三大组件之一,它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Servlet来处理,在Servlet中通常需要: l  ...

  8. 大数乘法 poj2389

    Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14972   Accepted: 7695 Descri ...

  9. 使用C++部署Keras或TensorFlow模型

    本文介绍如何在C++环境中部署Keras或TensorFlow模型. 一.对于Keras, 第一步,使用Keras搭建.训练.保存模型. model.save('./your_keras_model. ...

  10. 粘包解决高端_Client

    from socket import * #导入套接字模块的所有命令import struct #导入struck模块,用于封装数据流长度# from functools import partial ...