浅谈关于collection接口及相关容器类(一)


                 Collection在英文单词的意思是:采集,收集。而在JAVA API 文档中它是一个收集数据对象的容器。

          Collection作为容器类的根接口。如下图(部分子接口未写出):

             

                                 

              文档中说明了Collection接口,有如下的方法:

booleanadd(E e)
Ensures that this collection contains the specified element (optional operation).
booleanaddAll(Collection<? extendsE> c)
Adds all of the elements in the specified collection to this collection (optional operation).
voidclear()
Removes all of the elements from this collection (optional operation).
booleancontains(Object o)
Returns true if this collection contains the specified element.
booleancontainsAll(Collection<?> c)
Returns true if this collection contains all of the elements in the specified collection.
booleanequals(Object o)
Compares the specified object with this collection for equality.
inthashCode()
Returns the hash code value for this collection.
booleanisEmpty()
Returns true if this collection contains no elements.
Iterator<E>iterator()
Returns an iterator over the elements in this collection.
booleanremove(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation).
booleanremoveAll(Collection<?> c)
Removes all of this collection's elements that are also contained in the specified collection (optional operation).
booleanretainAll(Collection<?> c)
Retains only the elements in this collection that are contained in the specified collection (optional operation).
intsize()
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);
       
       }
	}
   
}


 
        

                  

                 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值