Java容器_4个接口

来源:互联网 发布:惠普g3110扫描仪软件 编辑:程序博客网 时间:2024/04/27 04:49

数组容量有限,为了解决对象的自由存放问题,Java在util类中提供了容器,主要有4个主要的接口:

Collection接口:定义了存取一组对象的方法,其子接口Set和List分别定义了存储方式。
   - Set接口中的数据对象没有顺序且不可重复。
   - Lst接口中的数据对象有顺序且可以重复。

Map接口定义了存储 键(Key)-值(Value)映射对的方法。

Collection函数库与数组的两点不同:
(1)数组的容量是有限的,而Collection库没有这样的限制,它容量可以自动的调节;
(2)Collection函数库只用来存放对象,而数组没有这样的限制。

Collection接口是Collection层次结构中的根接口,它定义了一些最基本的访问方法,让我们能用统一的方式通过它或它的子接口来访问数据。

打开源码看看:

public interface Collection<E> extends Iterable<E> {    // Query Operations    /**     * Returns the number of elements in this collection.  If this collection     * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns     * <tt>Integer.MAX_VALUE</tt>.     *     * @return the number of elements in this collection     */    int size();
可见Collection十几个常用的抽象函数,例如:size,contains(object),add,remove,clear等。

Map接口,打开源码看看:

public interface Map<K,V> {    // Query Operations    /**     * Returns the number of key-value mappings in this map.  If the     * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns     * <tt>Integer.MAX_VALUE</tt>.     *     * @return the number of key-value mappings in this map     */    int size();
可见Map和Collection差不多定义了十多个抽象函数,例如:containsKey(Object),put(K,V),get(Object),clear等。
接口的作用是规范,规范定义好了,下文看看怎么实现吧。

0 0
原创粉丝点击