【Java集合】集合框架Collection和Map

    在面试的时候,一般从java基础开始问,而Java集合框架被问的概率一定是位于前列。刚开始,被问到java集合框架,List,Set,Map脱口而出。而其实真正学习过java集合的,答案肯定不一样。

    一. 集合由来

    在编程中,常常需要集中存放多个数据。从传统意义上讲,数组是我们的一个很好的选择,前提是我们事先已经明确知道我们将要保存的对象的数量。一旦在数组初始化时指定了这个数组长度,这个数组长度就是不可变的,如果我们需要保存一个可以动态增长的数据(在编译时无法确定具体的数量),java的集合类就是一个很好的设计方案了。

    二. 集合基本概念

    集合类主要负责保存、盛装其他数据,因此集合类也被称为容器类。所有的Java集合类都位于java.util包下,后来为了处理多线程环境下的并发安全问题,java5还在java.util.concurrent包下提供了一些多线程支持的集合类。

    值得注意的几点:

①、集合只能存放对象。比如你存一个 int 型数据 1放入集合中,其实它是自动转换成 Integer 类后存入的,Java中每一种基本类型都有对应的引用类型。
②、集合存放的是多个对象的引用,对象本身还是放在堆内存中。
③、集合可以存放不同类型,不限数量的数据类型。

    三. Java集合框架

    Java的集合类主要由两个接口派生而出:Collection和Map,Collection和Map是Java集合框架的根接口。

这里写图片描述

    图中,ArrayList,HashSet,LinkedList,TreeSet是我们经常会有用到的已实现的集合类。

    Map实现类用于保存具有映射关系的数据。Map保存的每项数据都是key-value对,也就是由key和value两个值组成。Map里的key是不可重复的,key用户标识集合里的每项数据。
这里写图片描述

    图中,HashMap,TreeMap是我们经常会用到的集合类。

    Colleciton和Map接口中的方法,是我们在操作list,set,map等具体实现类中会经常应用到的,所以,参照着jdk文档,做个总结。

    四. Collection接口

    Collection接口是Set,Queue,List的父接口。Collection接口中定义了多种方法可供其子类进行实现,以实现数据操作。

  1. int size()

    Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
    返回集合中元素的个数,若个数大于最大值,则返回最大值。

  2. boolean isEmpty()

    Returns true if this collection contains no elements.
    返回true,若当前集合不包含任何元素。

  3. boolean contains(Object o)

    Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).
    返回true,若当前集合中包含某个具体的元素。 当且仅当当前集合至少包含一个元素,判断的具体元素为null,同样返回true.

    Throws:
        ClassCastException - if the type of the specified element is incompatible with this collection (optional)
        类型转换异常,若具体的元素类型与当前集合类型不兼容
        NullPointerException - if the specified element is null and this collection does not permit null elements (optional)
        空指针异常,若具体的元素为null,且当前集合不允许包含null元素

  4. Iterator iterator()

    Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).
    返回此集合中元素的迭代器。对于返回元素的顺序没有保证(除非这个集合是提供保证的某个类的实例)。

  5. boolean add(E e)

    Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

    Collections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.

    If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.

    确保此集合包含指定元素(可选操作)。如果此集合作为调用的结果而更改,则返回true。(如果此集合不允许重复并已包含指定的元素,则返回false)。

    支持此操作的集合可能会限制哪些元素可以添加到此集合中。特别是,一些集合将拒绝添加空元素,而其他集合将对可能添加的元素的类型施加限制。集合类应该在文档中明确地规定对哪些元素可以添加的任何限制。

    如果集合拒绝任何特定元素的添加,除了它已经包含元素之外,它必须抛出异常(而不是返回false)。这保留了集合在调用返回后始终包含指定元素的不变量。

    Throws:
        UnsupportedOperationException - if the add operation is not supported by this collection
        不支持操作异常,若当前集合不支持增加元素操作
        ClassCastException - if the class of the specified element prevents it from being added to this collection
        类型转换异常,若具体的元素类型不允许添加至当前集合
        NullPointerException - if the specified element is null and this collection does not permit null elements (optional)
        空指针异常,若具体的元素为null,且当前集合不允许包含null元素
        IllegalArgumentException - if some property of the element prevents it from being added to this collection
        若当前元素的某些属性不允许添加至当前集合
        IllegalStateException - if the element cannot be added at this time due to insertion restrictions
        由于插入限制,该元素不能添加至当前集合

  6. boolean remove(Object o)

    Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).
    从集合中移除指定元素的单个实例.

    Throws:
        ClassCastException - if the type of the specified element is incompatible with this collection (optional)
        类型转换异常,若具体的元素类型与当前集合类型不兼容
        NullPointerException - if the specified element is null and this collection does not permit null elements (optional)
        空指针异常,若具体的元素为null,且当前集合不允许包含null元素
        UnsupportedOperationException - if the add operation is not supported by this collection
        不支持操作异常,若当前集合不支持增加元素操作

  7. boolean containsAll(Collection c)

    Returns true if this collection contains all of the elements in the specified collection.
    返回true,若当前集合包含指定集合中的所有元素.

  8. boolean addAll(Collection c)

    Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)
    将指定集合中的所有元素添加到此集合中

  9. boolean removeAll(Collection c)

    Removes all of this collection’s elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.
    移除包含在指定集合中的所有集合元素。此调用返回后,此集合将不包含与指定集合相同的元素。

  10. boolean retainAll(Collection c)

    Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.
    只保留包含在指定集合中的集合中的元素。换句话说,从该集合中移除其未包含在指定集合中的所有元素。

    五. Map接口

Map是键值对的形式,和Collections中也有着相同的方法,这里不再重复总结了,下面主要是一些对键值对操作的方法。

  1. boolean containsKey(Object key)

    Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)
    若当前map中包含某个具体的key,则返回true.

  2. boolean containsValue(Object value)

    Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface.
    若当前map中包含某个或多个key对应的值,则返回true.

  3. V get(Object key)

    Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
    获取map中某个Key对应的值,若当前map中不包含此key,则返回null

  4. V put(K key,V value)

    Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)
    添加某键值对至当前map中,若当前map中已包含添加的key,则key的值将会被新值替代。

  5. V remove(Object key)

    Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)
    从当前map中移除某key.

  6. void putAll(Map m)

    Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
    将某个map中所有元素添加至当前map中,此操作等同于使用put()方法循环添加。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值