Java Foundation Consolidation Series (IX): the use and relationship between the objects held (iterable, Collection, List, Queue, Set, Map, Stack)

Source: Internet
Author: User
Tags addall iterable

Total: Java provides a relatively complete set of container classes, the basic types are: List, set, Queue, Map, these object types are called collection classes.

one, interface inheritance relationship:


Iterable interface, in Java.lang package, Collection, List, Queue, set interface inherit Iterable interface



As can be seen, list, Queue, set these three interfaces are in the Java.util package, inherit from the collection interface



The map interface does not inherit the collection interface, nor does it inherit other interfaces

Ii. the relationship between Iterable and iterator

Iterable belongs to Java.lang package, iterator belongs to Java.util package.

Next, let's take a look at the Iterable API documentation for this interface:


The List, Map, Set, and collection all inherit the iterator interface, which defines the iterator<t> iterator () method, which is capable of using the iterator iterator after inheriting the interface.

Question: Why do you have to implement Iterable interface, why not directly implement iterator interface? (From:http://liuyun025.iteye.com/blog/1321045 )

look at the collection classes in the JDK, such as the list family or set family, are implemented Iterable interface, but do not directly implement the iterator interface.
it makes sense to think carefully about it.

because the core method of the Iterator interface next () or Hasnext () is dependent on the current iteration position of the iterator.
If collection implements the iterator interface directly, it is bound to cause the collection object to contain the data (pointers) of the current iteration position.
When a collection is passed between different methods, the result of the next () method becomes unpredictable because the current iteration position is not pre-provisioned.
unless you add a reset () method to the iterator interface, you can reset the current iteration position.
But in this case, collection can only have one current iteration position at a time.
Instead, Iterable returns an iterator that counts from the beginning of each call.
multiple iterators are non-interfering.


Third, the relationship between the collection interface and the collections class, and the Arrays class

Below, look at a program:

public static void Main (string[] args) {//(1) list<integer> List = Arrays.aslist (1,2,3,4);//(2) collection< integer> C = new arraylist<integer> (arrays.aslist (1,2,3,4,5));//(3) integer[] moreints = {6,7,8,9,10}; C.addall (Arrays.aslist (moreints));//(4) Collections.addall (c, 11,12,13,14,15); Collections.addall (c, moreints);}
(1) The Arrays class contains various methods for manipulating arrays, including a number of static methods, Arrays.aslist (T-T) return list<t> collection Exclusive

(2) ArrayList upward transformation into collection object

(3) Adding an array to collection object C through the AddAll method of the collection object

(4) By collections class static method AddAll (collection<t> c,t....elements), add the variable parameter in the parameter table to the Collection object

Iv. List (lists)

The list's characteristics are stored in a linear fashion, allowing the storage of duplicate objects

There are three main classes of list implementations:

(1) ArrayList: Provides an implementation of an extensible array, with the advantage that random access (set () and get ()) is faster, with the disadvantage of inserting and deleting intermediate items at a slower rate.

(2) LinkedList: Provides the implementation of the double-linked list (is based on the linked list), for data insertion and deletion requires less overhead

(3) Vector: Vector class can achieve a growing array of objects, compared with ArrayList, ArrayList is non-synchronous, in the case of multi-threading requires the use of vectors (will continue to be discussed in future multithreading problems)

Set (a collection that does not contain duplicate elements)

Duplicate objects are not allowed in the Set collection, objects in the collection are not sorted in a particular way

The set interface mainly implements two classes:

(1) HashSet: This class is based on a hash table support, is not synchronized, is inherently used to improve the search rate. The object that is deposited into the hashset must be defined hashcode ()

(2) TreeSet: Based on the implementation of binary tree, save the order of set, the bottom of the tree structure. Use it to extract ordered sequences from the set.

Vi. Map (map)

Map is a collection of key object and value object mappings, each of which contains a pair of key objects and value objects. Map does not inherit from the collection interface when retrieving elements from the map collection, the corresponding value object is returned whenever the key object is given.

There are two main implementation classes for map:

HASHMAP:map is based on the implementation of the hash table. The cost of inserting and querying "key-value pairs" is fixed. The capacity capacity and load factor load factor can be set through the constructor to adjust the performance of the container.

TREEMAP: based on the implementation of red-black tree data structure. When you look at key or key-value pairs, they are sorted (the order is determined by comparabel or comparator). TreeMap is characterized by the fact that the results you get are sorted. TreeMap is the only map with the Submap () method, which can return a subtree












Java Foundation Consolidation Series (IX): the use and relationship between the objects held (iterable, Collection, List, Queue, Set, Map, Stack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.