Logo

郎哥编程

认识Java集合类Collection接口

2018-04-10 929

Collection接口作为集合的一个根接口,它提供了对集合对象进行基本操作的通用接口方法,接口在Java 类库中有很多具体的实现。其意义是为各种具体的集合提供了最大化的统一操作方式。

JDK不提供此接口的任何直接实现,而是提供更具体的子接口,有Set接口、List接口、Queue接口。Set接口存放的元素是无序的且不包含重复元素(对象);List接口存放的元素是有序的且允许有重复的元素;Queue接口存放的元素顺序符合先入先出的规则,和我们日常生活中的排队模型很类似。Collection接口层次如下图所示:

                         

m000003.jpg

                   

图 14-2  Collection接口层次

Collection接口定义了一组对象和它的子类需要实现的方法,下面分类列出并说明。

1、容器类中添加、删除的操作方法

● boolean  add(Object  obj)

将Object对象添加到collection。

● boolean  remove(Object  obj)

如果collection中有与obj相匹配的对象,则删除该对象。

2、容器类中元素查询的操作方法

● int  size()

返回当前集合中元素的数量。

● boolean  isEmpty()

从当前collection中查询是否包含元素。

● boolean  contains(Object  obj)

查找此collection是否包含指定的元素。

● boolean  contains(Collection  collec)

判断此Collection是否包含指定Collection中所有元素。

● Iterator  iterator ()

返回此Collection上的迭代器,用来访问该Collection中各个元素。

● boolean  contains All(Collection  collec)

判断Collection是否含有collec中的所有元素。

3、容器类中的组操作方法

● boolean  addAll(Collection  collec)

将指定的collec中的所有元素添加到当前Collection。

● void  clear()

删除当前Collection中的所有元素。

● void  removeAll(Collection  collec)

从当前Collection中删除collec中的所有元素。

● void  retainAll(Collection  collec)

从当前Collection中删除collec中不包含的元素。

4、转换操作,用于集合与数组间的转换

● Object[]  toArray()

将当前Collection转成对象数组

● Object[]  toArray(Object[]  a)

返回一个内含当前Collection所有元素的array。

在Collection中并未提供get()方法获取元素。如果要遍历Collection中的元素,一般要采用Iterator迭代器,可以通过Iterator迭代器遍历Collection各个对象元素。

Iterator接口中定义的方法如下:

● boolean  hasNext()

判断是否有下一个元素。

● Object  next()

返回当前指针指向的元素,并指向下一个元素

● void  remove()

删除当前指针所指向的元素,一般和next方法一起用,这时候的作用就是删除next方法返回的元素。

下面给出利用Iterator遍历Collection的用法

package com.milihua.fruits;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Person {
    //声明名称属性
    private String name;
    //声明年龄属性,并被初始化为22
    private int age;
   
    //定义一个显式无参构造方法
    public Person()
    {
     this.name = "张三";
     this.age = 22;
    }
 
    //定义一个显式有参构造方法
    public Person(String instrName,int innAge)
    {
     this.name = instrName;
     this.age = innAge;
    }
   
    //定义一个方法,public是修饰符,void表示没有返回值
    public void tell() {
     System.out.println("姓名:" + name + ",年龄:" + age) ;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Collection<Person> persons = new ArrayList<Person>();
        Person p1 = new Person("张三",32);
        persons.add(p1);
        Person p2 = new Person("王五",29);
        persons.add(p2);
        Iterator<Person> iterator = persons.iterator();
        while (iterator.hasNext()) {
            Person  p =  iterator.next();
            p.tell();
        } 
    }
}

例子代码构建了一个Person,在main方法中声明了Collection集合类,并实例化为ArrayList集合对象,先后添加了p1和P2两个Person对象。最后用Iterator迭代器遍历ArrayList包含的所有Person元素。

■ 知识点拨

容器中的元素类型都为Object类型,从容器中取得元素时,必须把它转换成原来的类型。


代码在线纠错(通义千问 qwen-max)

支持粘贴多个代码文件,提交后由阿里云通义千问自动分析代码漏洞、语法错误、逻辑问题并给出修改建议。
您已解锁 AI 代码纠错功能,可正常使用!

评论区

登录 后发表评论
暂无评论