(A) on the set

Foreword

  In recent collection of related things to see, watching the always incomprehensible, so far is still foggy. It seems necessary to understand the underlying implementation of the set, can only solution confusion of mind. Then establish a set of themes, record and think what they have learned.

 

What is a collection of

  There are many ways to save Java objects (or object reference), such as arrays. If you want to save a set of objects can be used; if you wish to save a set of basic types of data, should an array. Array is good, can not a panacea. Because the array is initialized when you want to specify a fixed length, which is very bad. why? Because sometimes, you do not know how many objects, if the array length set too small, it will error. Set too much is wasted (though most people have no object). Therefore, the array size is fixed at the time of this restriction seems too limited a storage object.

  Therefore java class library provides a fairly complete set of containers to solve this problem. Wherein the basic type is List, Set, Queue, Map, these types are generally referred to as collections. Usually use the term "container" broader call them.

  java containers can automatically adjust its size (automatic expansion mechanism). Unlike arrays it is that you can put any number of objects into the container without having to worry about how much you want to set the container.

 

basic concepts

  In the Java class libraries, the collections of two basic interfaces: Collection interfaces , the Map interface. Collection of which there are three sub-interfaces Interface List Interface , Set Interface , Queue interfaces.

 

  Why so divided?

    Collection interface primary store is a sequence of independent elements, these elements are subject to one or multiple rules.

    Map interface is mainly stored in a set of key-value pairs objects, allowing you to search by using a key (key) value (value).

 

  Put a simplified diagram of a set of frame:

     

 

Collection Interface

  Let's take a look at the Collection interface Source:

1 public interface Collection<E> extends Iterable<E> {
2     //...
3 }

  Visible Collection interface extends Iterable interface. Iterable interface is the role of providing a return iterator object and a method of traversing a unified collection of elements, the iterator is used to loop through the collection of objects. About Iterable interface and the Iterator interface here is not introduced.

 

About method:

  Look again at the interface defined by the collection method:

 Here are some of the ways in which the API documentation and presentation, behind only introduce partially changed

  • Iterator < E> iterator()
    Returns a collection iterator for accessing each element.
    • int size()
    Returns the number of elements currently stored in the collection.
    • boolean isEmpty()
    If there are no elements in the collection, it returns true.
    • boolean contains(Object obj)
    If the collection contains the object obj with an equal, it returns true.
    • boolean containsAl 1(Collection<?> other)
    If this collection contains all the other elements in the collection, return trueo
    • boolean add(Object element)
    Adding an element to the collection. If the result of this call to change the collection, returning true.
    • boolean addAl 1(Col 1 ection<? extends E> other)
    将 other 集合中的所有元素添加到这个集合。如果由于这个调用改变了集合,返回 true。
    • boolean remove(Object obj)
    从这个集合中删除等于 obj 的对象。如果有匹配的对象被删除, 返回 true。
    • boolean removeAl 1(Col 1 ection<?> other)
    从这个集合中删除 other 集合中存在的所有元素。如果由于这个调用改变了集合,返回 true。
    • default boolean removelf(Predicate<? super E> filter)8
    从这个集合删除 filter 返回 true 的所有元素。如果由于这个调用改变了集合, 则返回 true。
    • void clear()
    从这个集合中删除所有的元素。
    • boolean retainAl 1(Collection<?> other)
    从这个集合中删除所有与 other 集合中的元素不同的元素。如果由于这个调用改变了
    集合, 返回 true。
    •Object[]toArray()
    返回这个集合的对象数组。

总结:

  可见,Collection接口作为java集合的基本接口之一,声明了适用于JAVA集合(只包括Set和List)的通用方法。

 

Set接口

  Set接口是Collection接口的子接口

1 public interface Set<E> extends Collection<E> {
2     //...
3 }

 

关于定义:

  我们来看Set的注解:

        

  为什么说Set是不包含重复元素的集合?我们来看Set接口中add方法的注解:

      

   

  

 

 

 

 

 

 

 

  所以Set方法是重新定义了包括add方法在内的部分方法,使得在添加元素时,已包含的元素不能在添加进集合中,从而实现了Set集合 不包含重复元素。

 

  我们再来看Iterater()方法的注解:

 

  我们可以得知,当使用迭代器迭代Set集合中的元素时,是对返回顺序不做保证的除非具体的实现类提供保证。那么什么是对返回顺序不做保证?

  首先顺序有两个概念,一是按添加的顺序排列,二是按自然顺序a-z排列。不做保证是指集合元素可能是乱序的,可能是有序的(按添加顺序或者自然顺序),还是具体有序无序还是要看具体实现类。

  你比如HashSet就是输出时两个顺序都不能保证,LinkedHashSet保证输出添加顺序,TreeSet保证输出自然顺序。具体为什么待整理到相关内容再具体介绍。

 

 关于方法:

  

  我们可以看到,Set接口没有新增任何的方法,只是重新定义了Collection的部分方法(多态性)。

 

总结:

  所以,对于Set集合我们可以得知它的两个特性不包含重复元素。对返回顺序不做保证的。

 

List接口

  List接口是Collection接口的子接口,除了继承 Collection 的一些方法,还新增了许多方法

 

1 public interface List<E> extends Collection<E> {
2     //...
3 }

 

关于定义:

  那么List集合有什么特点呢?

  

   我们可知,List集合被定义为一个①有序集合同时能够对某个位置的元素进行操作。

  

   List集合第二个特点是②允许有重复元素

 

关于方法:

  对应List集合的方法:

   我们可以看到,List接口新增了以下操作:

    位置相关:add(int ,E),get(int ,E),remove(int),set(int ,E),为增查删改提供了具体对某个位置操作的方法

    搜索:indexOf(Object)、lastIndexOf(Object),从List集合中查找某个对象的位置

    迭代器:listIterator()、listIterator(int),listIterator迭代器比Iterator更强大,支持向前迭代,迭代时修改等等。

    范围操作:subList(),返回List中一个片段

 

总结:

  可见,对于List集合我们知道它是①有序集合并且允许有重复元素

 

Queue接口

  java中的队列接口Queue是一个被设计用来按一定的优先级处理元素的集合,除了拥有基本的集合操作和数据结构队列的特性外它还添加了额外的元素插入,获取,检查操作。所有的这些额外添加的操作都有一个共性,如果操作失败,一种情况是:抛出异常;一种情况是:返回特殊的值(根据情况可能是null或false)。返回特殊值这种方式是针对有容量队列的插入操作。在大多数队列的实现中插入操作是不能失败的。

1 public interface Queue<E> extends Collection<E> {
2     //....
3 }

 

 

关于方法:

  

  我们可以看到Queue接口里并未新增或者重写多少方法。

 

  • boolean add(E e);

     add 方法将指定的元素添加入队列,如果队列是有界的且没有空闲空间则抛出异常。

  • boolean offer(E e);

    offer 方法将指定的元素添加入队列,如果队列是有界的且没有空闲空间则返回false。在使用有界队列时推荐使用该方法来替代add方法。

  • E remove();

    remove删除并返回队首的元素。如果队列为空则会抛异常。

  • E poll();

    poll删除并返回队首的元素。如果队列为空返回null。

  • E element();

    element返回队首元素但是不删除。如果队列为空会抛出异常。

  • E peek();

    element返回队首元素但是不删除。如果队列为空则返回null。

 

总结:

  在JDK里面没有一个队列的实现是仅仅实现Queue接口定义的功能。可能是因为具有基本功能的队列实现比较简单而且实际的用途有点少。队列的实现基本可以分为:1.并发队列(ConcurrentLinkedQueue); 2.阻塞队列(ArrayBlockingQueue, LinkedBlockingQueue); 3.双端队列(Deque, ArrayDeque, LinkedList, ConcurrentLinkedDeque); 4:优先级队列(PriorityQueue, PriorityBlockingQueue) 每种类型的队列都是针对不同的应用场景的,所以还是需要仔细区分来选择合适的队列实现。

 

Map接口

  map接口是集合根接口之一,用于保存具有映射关系的数据(key-vlaue)

1 public interface Map<K,V> {
2     //...
3 }                        

  

关于定义:

  map集合的定义

  

  我们可得知:

  • Map中的对象是用①键值对来存储数据
  • Map的key不允许重复,即同一个Map对象的任何两个key通过equals方法比较总是返回false
  • 每个③键只对应一个值,不存在一个键对应多个值的情况。

  

  • Map中的④元素是无序的,但是也有实现了排序的Map实现类,如TreeMap

   

关于内部接口:

   Map中包括一个内部接口:Entry。此接口为泛型,定义为Entry<K,V>,它表示Map中的一个实体(一个key-value对)。

   Entry有几个方法,这里只列举其中三个方法:

  • Object getkey():返回该Entry里包含的key值。
  • Object getValue():返回该Entry里包含的value值。
  • Object setValue():设置该Entry里包含的value值,并返回新设置的value值。

     

关于方法:

  Map接口提供的方法大致可以分为几类: 

  • 增删改查 :put/putAll 、 remove/clear 、  replace/replaceAll 、 get/values 
  • 判断:containKey/containValue(判断键/值是否存在)、isEmpty(是否为空)
  • 迭代:entrySet/keySet (获取由key/entry对象组成的set集合)
  • 比较:equals/hashcode (判断是否相等)

 

总结:

  Map集合是用①键值对来存储数据的。其中②键(key不允许重复,③只对着应一个值,而value可以重复。Map中的④元素是无序的,但是也有实现了排序的Map实现类,如:TreeMap。

 

 

至此,集合框架基本接口整理完毕。

 

参考资料:https://leokongwq.github.io/2016/10/15/java-Queue.html

 

Guess you like

Origin www.cnblogs.com/chiayhon/p/11741676.html
set
set