浅谈关于collection接口及相关容器类(一)
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); }
} }
浅谈关于collection接口及相关容器类(一)的更多相关文章
- 浅谈Java中接口与抽象类的异同
浅谈Java中接口与抽象类的异同 抽象类和接口这两个概念困扰了我许久,在我看来,接口与抽象类真的十分相似.期间也曾找过许许多多的资料,参考了各路大神的见解,也只能是简简单单地在语法上懂得两者的区别.硬 ...
- 浅谈JavaScript原型对象与相关设计模式
引言 本文先从介绍JavaScript中基本的几种设计模式开始,最后引出原型对象,然后对原型对象有一个较全面的介绍. 1.创建对象的几种设计模式 A.工厂模式 我们知道在JavaScript中创建对象 ...
- 为什么这些java接口没有抽象方法?浅谈Java标记接口
在jdk的源码中,存在这样的一些接口,他们不包含任何的(抽象)方法,但是却广泛的存在. 这种接口我们称之为Mark Interface,也就是标记接口. 这些接口呢,我们不用来实现任何的方法,他们的作 ...
- [iOS]浅谈NSRunloop工作原理和相关应用
一. 认识NSRunloop 1.1 NSRunloop与程序运行 那么具体什么是NSRunLoop呢?其实NSRunLoop的本质是一个消息机制的处理模式.让我们首先来看一下程序的入口——main ...
- 浅谈Java Future接口
Java项目编程中,为了充分利用计算机CPU资源,一般开启多个线程来执行异步任务.但不管是继承Thread类还是实现Runnable接口,都无法获取任务执行的结果.JDK 5中引入了Callable和 ...
- 浅谈java中接口与抽象类之间的异同
刚学习java的时候,总觉得接口和抽象类很像,但又说不上具体有什么区别.今天静下来,翻翻书,查查资料,做个小结.首先举两个例子,看看interface和abstract class 在“外形”上有啥异 ...
- 浅谈JSON与与JS相关的JSON函数
本文内容主要引用在微信公众号上看到的一片文章,因为自己对Json了解不是很深入,所以就整理出这篇博文与大家分享! 一. JSON是一种格式,基于文本,优于轻量,用于交换数据 1.一种数据格式 数据的传 ...
- 示例浅谈PHP与手机APP开发,即API接口开发
示例浅谈PHP与手机APP开发,即API接口开发 API(Application Programming Interface,应用程序接口)架构,已经成为目前互联网产品开发中常见的软件架构模式,并且诞 ...
- 浅谈Android系统进程间通信(IPC)机制Binder中的Server和Client获得Service Manager接口之路
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6627260 在前面一篇文章浅谈Service ...
随机推荐
- [HTML 5] aria-live
"aria-live" is a method to tell the information to the screen reader once value changed. a ...
- java-类生命周期(二)
上文介绍了java-类生命周期(一),理论性比較强.认为太复杂的同学,瞟一下本文的样例加深理解. 先给道题目,看看答对没. /** * 类载入试验基类 * * @author peter_wang * ...
- Linux--对文件夹下的配置文件批量改动IP
sed -i 's/10.1.1.1/10.1.1.2/g' `grep -ir 10.1.1.1 * |grep -E '.xml:|.cfg:|.ini:|.wsdl:|.properties:' ...
- wpf获取目录路径
AppDomain.CurrentDomain.BaseDirectory +文件名即可,简单吧? //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称. string str5=Appl ...
- 0x06 倍增
这东西太玄学了我真是不太会... 对于这道例题,很容易看出最大值必然是最大减最小,次大减次小…… 常规的贪心思想,分的个数一样,总长度越大越好.其实我的第一想法是二分右端点..但是只有40,至今没有搞 ...
- page template in kentico
Ad-hoc templates are used for one page only, for which they were created - this is why they are not ...
- netty可靠性
Netty的可靠性 首先,我们要从Netty的主要用途来分析它的可靠性,Netty目前的主流用法有三种: 1) 构建RPC调用的基础通信组件,提供跨节点的远程服务调用能力: 2) NIO通信框架,用于 ...
- 杂项:MSP(管理服务提供商)
ylbtech-杂项:MSP(管理服务提供商) 随着外包市场的日益成熟,为了满足企业的需求,一个全新的业务方向被开发出来—MSP.MSP采用业界领先的系统管理技术,由经验丰富的系统管理专家通过WAN为 ...
- JPA实现一对多(OneToMany)关联
转自:https://blog.csdn.net/qq_32444825/article/details/77084580 1.考试类 @Entity public classExam impleme ...
- Migrations有两个文件迁移数据的方法
不分开迁移报错如下; 解决方案 1 迁移Migration/nfoManage EntityFramework\Enable-Migrations -ContextTypeName InfoModel ...