浅谈关于collection接口及相关容器类(一)
2014-11-05 20:24
176 查看
Collection在英文单词的意思是:采集,收集。而在JAVA API 文档中它是一个收集数据对象的容器。
Collection作为容器类的根接口。如下图(部分子接口未写出):
文档中说明了Collection接口,有如下的方法:
上图中,Set与List、Map都是Collection的子类。它们的区别在于,Set实现的类只能存放无序、无重复的数据对象,而List只存放的是有序,可重复的数据对象;Map接口定义了存储着"键(key)-值(value)映射对的方法。
下面代码示范List继承collection接口并实现它部分的相关方法:
Collection作为容器类的根接口。如下图(部分子接口未写出):
文档中说明了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. |
下面代码示范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); } } }
相关文章推荐
- 浅谈关于nil和 null区别及相关问题
- 浅谈关于nil和 null区别及相关问题(转)
- Java容器学习笔记(一) 容器中基本概念及Collection接口相关知识
- ios浅谈关于nil和 NIL区别及相关问题
- ios浅谈关于nil和 NIL区别及相关问题
- ios浅谈关于nil和 NIL区别及相关问题(转)
- 浅谈关于nil和 null区别及相关问题(转)
- Java容器学习笔记(一) 容器中基本概念及Collection接口相关知识
- 浅谈关于Docker容器互联的初步实践
- 浅谈关于nil和 null区别及相关问题(转)
- 浅谈关于nil和 null区别及相关问题
- ios浅谈关于nil和 NIL区别及相关问题
- (浅谈)AndroidStudio中关于多渠道打包相关处理
- 关于XMLHTTP无刷新数据获取和发送(转相关内容)
- 关于本Blog内容的声明、今后的发展方向及Teddy的其他相关站点
- 另眼看自动化测试工具-浅谈自动化测试的好处,缺陷及相关弥补策略
- 关于.Net操作XML相关类
- 关于webwork的IOC容器
- 关于使用VS.Net2003调试器出现的问题及相关解决方法
- 关于asp.net Page 里与form 相关的Js错误