Java集合——容器

           Java集合类又被称为容器类,用来保存对象(实际上保存的是对象的引用变量)。而数组既可以保存基本数据类型的值,也可以保存对象,但是长度固定,对局数量变化的数据就无能为力了。

       Java集合类主要由两个接口类派生:Collection和Map,这两个接口有包含了一些子接口和实现类。如下图所示




由上图可以看到:

1.Collection接口包括Set和List两个集合,分别表示无序和有序集合。Queue是队列,有点类似于List

2.Map实现类用于保存具有映射关系的数据,每项数据都是key-value对。Map中的key是不可重复的,key用于标识集合中的每项数据,如果要获得数据,总是根据Map的key来获取。

3.Set和Map就像两个罐子,把一个元素添加到Set集合,由于无法记住添加元素的顺序,所以Set集合元素不可以重复。Map集合中每项数据由两个值组成。List就像一个数据,能记住添加元素的顺序,只是它的长度可变。


对于Set,List,Queue和Map四种集合,最常用的实现类有:HashSet、TreeSet、ArrayList、LinkedList、ArrayDeque、HashMap、TreeMap

下面是Collection接口的方法,对于所有的实现类都通用

boolean add(E e)
Ensures that this collection contains the specified element (optional operation).
boolean addAll(Collection<? extends E> 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.
default Stream<E> parallelStream()
Returns a possibly parallel   Stream  with this collection as its source.
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).
default boolean removeIf(Predicate<? super E> filter)
Removes all of the elements of this collection that satisfy the given predicate.
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.
default Spliterator<E> spliterator()
Creates a   Spliterator  over the elements in this collection.
default Stream<E> stream()
Returns a sequential   Stream  with this collection as its source.
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

Set就是Collection,就是行为略有不同,即不允许包含重复元素。Set判断两个对象是否相等,不是使用==运算符,而是根据equals()方法。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值