Java Basic Series--Collection class library (i)

Source: Internet
Author: User
Tags addall set set

Original works, can be reproduced, but please mark the source address: http://www.cnblogs.com/V1haoge/p/7229478.html

1. Overview

Java's collection Class library is very rich, encompassing most of the common data structure forms, allowing us to have a purposeful choice for the current business scenarios and functional scenarios of the collection class. The right set of frames can maximize execution speed and efficiency.

All collection classes in the Java Collection Class library begin with the collection interface and the map interface, which represents a single-value collection, which represents a collection of mappings (a two-valued collection).

Abstract classes that begin with abstract in the Java collection framework are actually defined by JDK developers to implement specific collection classes and are intended for JDK developers, not JDK-oriented users. In these abstract classes, it is common practice to implement some common methods of a collection of the same type to avoid repeated implementations in a specific collection class, such as the Contains method.

2. Interface System

In this structural system, I simply list the interface inheritance structure terminating in the specific collection type interface, from which we can clearly see the location of each interface, let's focus on some of these interfaces, their location and meaning.

2.1 Iterable Interface

This is a simple interface, the class that implements the interface can use the Foreach loop iteration, in fact the foreach underlying principle or the iterator iterator loop. As you can see from the interface above, only a single-valued collection can use this iterative approach.

It has only one method inside:

1 iterator<t> Iterator ();

This method is used to return an iterator iterator based on a collection of elements of type T.

2,2 Iterator interface

This interface is an iterator interface, and the iterator interface defines the methods commonly used by iterators:

1     Boolean Hasnext (); 2     E Next (); 3     void Remove ();

The meanings of these three methods vary, but they are dependent on each other in use.

The first method of the Hasnext () method is used to verify that there are elements in the iterator and returns true if there are elements, otherwise false

The second method next () method is used to get the next element of the iterator, and if the end of the iterator is currently reached, calling the method again throws a Nosuchelementexception exception, so we can use this method in conjunction with the previous Hasnext () method. Determine if you have the next element and then get the element, so that if no next element exists, it is no longer available.

We can think of the next () method as a pointer that does not point to a specific element, but rather to the gap between the element and the elements, initially, when the pointer is positioned before the first element, calls next () once, the pointer crosses the first element, points to the gap between the first element and the second element, The method returns the element that is crossed.

The third method is the Remove () method, which removes the element returned by the iterator in the collection, which is used on the basis of the second method, which requires the next () method to be executed before the Remove () method is executed. And the two remove method cannot be executed at the same time (even if the first element is removed, it must first be executed next to return the first element before the deletion can be performed)

2.3 Listiterator Interface

The Listiterator is a sub-interface of the iterator, and is specifically extended to the sequence on its original basis, which defines the following methods:

1     BooleanHasnext ();2 E Next ();3     Booleanhasprevious ();4 E Previous ();5     intNextindex ();6     intPreviousindex ();7     voidremove ();8     voidset (e e);9     voidAdd (e e);

The first method: with iterator

The second method: with iterator

The third method: Hasprevious () is used to reverse the duration of the query whether there are remaining elements, acting as the first method, but the opposite direction

The fourth method: previous () is used to get the next element in the reverse query, passing over the next element and returning the element, acting as the second method, in the opposite direction

Fifth method: Nextindex () returns the next position, if it is to the end, returns the length of the list

Sixth method: Previousindex () is used to return to the previous position, if it is in the starting position, 1 is returned.

Seventh method: Remove (), with iterator

Eighth method: Set (e) is used to replace the newly returned element (which may be returned by the next method or it may be returned by the previous method)

Nineth method: Add (e) to insert a new element into the current sequence of next or previous will return the elements of the front/rear

2.4 Collection Interface

The collection interface is the root interface of a single-valued collection, which provides a basic method definition for a collection of this type:

1     intsize ();2     BooleanisEmpty ();3     Booleancontains (Object o);4Iterator<e>iterator ();5 object[] ToArray ();6<T>t[] ToArray (t[] a);7     BooleanAdd (e e);8     BooleanRemove (Object o);9     BooleanContainsall (collection<?>c);Ten     BooleanAddAll (collection<?extendsE>c);  One     BooleanRemoveAll (collection<?>c); A     BooleanRetainall (collection<?>c); -     voidClear (); -     Booleanequals (Object o); the     intHashcode ();

Here are a list of all the methods defined in the collection interface, in total 15:

The first method: thesize () method is used to return the number of elements in the current collection

The second method:isEmpty () is used to verify that the collection contains elements and returns False if the containing element returns true if no elements are included

The third method:contains (Object o) verifies that the specified element is contained in the collection and returns True if it is included, otherwise false

Fourth method:iterator () returns an iterator based on the current collection iterator

The fifth method:ToArray () is used to get an array that contains all the elements in the current collection, and if the collection is specified in the order, then the elements of the array will be arranged in this order, and the array will no longer have any contact with the collection, and any modifications to the array will not affect the collection.

The sixth method:ToArray (t[] a) is used to get an array containing all the elements in the current collection, unlike the method above, where this method is given an array to hold the elements in the collection, if the array is large enough to put all the elements of the collection, The remaining position of the array is null, and if the number of array bits is not sufficient to hold all the collection elements, recreate an array (which is consistent with the type of the given array) to hold the collection element, and the second case is the same as the fifth method above. Because this method is given an array to hold the collection elements, the efficiency is slightly higher than the above.

The Seventh method: Add(E) adds a new element to the collection, returns False if the collection does not allow duplicate elements and the element already exists, returns true if the collection structure has changed (indicates added to the collection)

The eighth method: Remove(Object o) is used to remove an element from the collection, and deleting the element in the collection will result in partial changes to the collection structure. Returns True if the element is contained in the collection

Nineth method:containsall (collection<?> c) verifies that all elements in the given collection are contained in the current collection and returns True if included, otherwise false

The Tenth method:AddAll (collection<? extends e> c) is used to add all the elements in the specified collection to the current collection, and the result becomes indeterminate if the specified collection changes during the addition process.

11th method:RemoveAll (collection<?> c) is used to remove all elements from the specified collection contained in the current collection, and returns True if the current collection changes

12th method:Retainall (collection<?> c) is used to preserve elements in the current collection that are also present in the specified collection, removing all elements that are not contained in the specified collection, and returning True if the collection changes

13th method:Clear () removes all elements from the current collection, after which the element is empty

14th method:equals (object o) is used to compare two sets of objects for the same

15th method:hashcode () returns the hash value of the current collection object

2.5 List Interface

A list is an ordered set, also known as a sequence. Has a subscript that allows duplicate values. It defines some proprietary methods of operation for the sequence:

1E Get (intindex);2E Set (intindex, E Element);3     voidAddintindex, E Element);4E Remove (intindex);5     intindexOf (Object o);6     intlastIndexOf (Object o);7Listiterator<e>listiterator ();8Listiterator<e> Listiterator (intindex);9List<e> sublist (intFromIndex,intToindex);

First method: Get (int index) to get the element that performs the subscript

The second method: set (int index,e element) sets the element that executes the subscript, replacing the element at the specified position with the given element

The third method: Add (int index, E Element) is used to insert a new element at the specified position, and the subsequent element is all moved back to a

The fourth method: remove (int index) removes the element at the specified position, and all subsequent elements move forward one

The Fifth method: IndexOf (Object o) Gets the position of the specified element in the sequence (this refers to where the first occurrence occurs) and returns 1 if it does not exist.

Sixth method: LastIndexOf (Object o) Gets the position of the top element in the sequence, starting at the End (reverse) (the first occurrence of the position), or 1 if there is no return

Seventh method: Listiterator () gets a sequence iterator based on the elements in the sequence

Eighth method: Listiterator (int index) gets a sequence iterator based on the elements in the sequence, starting at the specified position

Nineth method: sublist (int fromIndex, int toindex) gets a subsequence that is the sequence segment that intercepts the specified starting position from the current sequence to the specified end position, if the start position = end position, the subsequence is empty

2.6 Set Interface

View source The set interface is exactly the same as the method defined in the collection interface, and the set set represents an unordered collection, which cannot have duplicate elements.

2.7 Queue Interface

Queue is a queue interface that holds the elements that are about to be processed, and in addition to the basic collection-defined methods, queues provide additional insertions, extraction, and removal operations. Each method has two forms: one that throws an exception (when an error occurs), one that returns a special value (null or FALSE), which is specifically designed for a queue that has a capacity limit.

1     Boolean Add (e e); 2     Boolean Offer (e e); 3     E Remove (); 4     E poll (); 5     E Element (); 6     E Peek ();

The first method: Add (E E) is used to insert the specified element into the queue, if there is no capacity limit. If execution succeeds returns true if execution fails, throws IllegalStateException exception

The second method: Offer (E-e) is used to insert the specified element into the queue, if there is no capacity limit. Use this method better than the Add method when using a capacity-constrained queue. Returns true if execution succeeds, otherwise false.

The third method: Remove () removes the element at the top of the queue and returns the removed element, and throws a Nosuchelementexception exception if the queue is empty

Fourth method: Poll () removes the element at the top of the queue and returns the removed element, or null if the queue is empty

Fifth method: Element (), gets the elements at the top of the queue (not removed) throws a Nosuchelementexception exception if the queue is empty

Sixth method: Peek () gets the element at the top of the queue (not removed) returns null if the queue is empty

The above method corresponds to 22, which is used to deal with two different situations.

2.8 Map Interface

Map is a set of key-value pairs for one-to-one key-value storage, keys cannot be duplicated, values can be duplicated, and a key can only find a single value.

Map has three collection views: Key collection (set), Value collection (Collection), and key-value mapping collection (set).

The order of the map collection is defined as the order in which the iterator for its value set (Collection) is returned.

The keys of the map collection generally use immutable values, but the values can use variable values. Because mutable keys can affect the equals comparison.

All implementations of the map collection should provide two constructors: one is the parameterless constructor used to create an empty map collection, and one is a constructor with a map set parameter that creates a new map collection corresponding to the given set one by one.

The methods defined in the Map collection are as follows:

1     intsize (); 2     BooleanisEmpty ();3     BooleanContainsKey (Object key);4     BooleanContainsvalue (Object value);5 V get (Object key);6 v put (K key, v value);7 V Remove (Object key);8     voidPutall (map<?extendsK?extendsV>m);9     voidClear ();TenSet<k>KeySet (); OneCollection<v>values (); ASet<map.entry<k, v>>EntrySet (); -     Booleanequals (Object o); -     intHashcode ();

The first method: size () returns the number of key-value mappings in the Map collection

The second method: IsEmpty () is used to verify that the collection contains a key-value mapping, returns True if there is no representation that the collection is empty, or false to indicate that the collection is not empty

The third method: ContainsKey (Object key) is used to verify that the collection contains a mapping of the specified key, and returns False if it contains true (verifies that the collection has a mapping of the same key as the specified key, if the specified key is null, Finds a mapping of NULL for a key, otherwise looks for a key that is consistent with the specified key)

Fourth method: Containsvalue (Object value) is used to verify that a mapping of the specified value exists in the collection (can only be repeated, so there is at least one), if at least one is present returns True, there is no return false

The Fifth method: Get (Object key) is used to get the value of the specified key in the collection map, or null if the specified key does not exist, otherwise the corresponding value is returned. If the set allows null values, then returning a null value is not sure if there is no mapping or mapping, only the value is NULL, you can use the third method to differentiate (by using the ContainsKey (Object key) method to verify that the specified key exists, Returns a normal saved null value if it exists, otherwise it means no mapping exists.

The sixth method: put (K key, V value) is used to save the specified map to the collection, if the specified key already exists (the existence of the key is judged by the ContainsKey (Object key) method), then the corresponding value of the substitution operation is performed.

The Seventh method: remove (Object key) removes the mapping of the specified key from the collection and returns the value of the removed map, or null if there is no mapping for the specified key, which again creates ambiguity for the set of allowed null values, as in the workaround.

Eighth method: Putall (map<? extends K,? extends v> m) copies all the mappings in the specified map collection to the current map collection.

The Nineth method: Clear () clears all mappings in the current map collection,

The Tenth Method: KeySet () returns the collection of all the keys in the map collection, which is a set set, which is the mapping of the keys in the map collection, so changing any of them can be found in the other.

The 11th Method: values () is used to get a collection of all the values in the map contained in the map collection, which is a mapping of the values in the Map collection, so changing any of them can be found in the other.

The 12th method: EntrySet () is used to get a collection of all the mappings contained in the map collection, which is a set set, which is the map mapping in the map collection, so changing any of them can be found in the other.

2.9 Entry<k,v> Internal Interface

The interface is defined inside the map interface and is dedicated to map collections that represent the mappings in the Map collection, which represent key-value pairs.

1         K GetKey (); 2         V getValue (); 3         v setValue (v value); 4         Boolean equals (Object o); 5         int hashcode ();

The first method: the GetKey () method is used to get the keys in the key-value mappings

The second method: the GetValue () method is used to get the values in the key-value mappings.

Third method: The SetValue (V value) method is used to reset the value in the current key-value map

3. Summary

All of the above interfaces simply make a distinction between collection types, dividing four classes of collections, queue queues, list sequences, set sets, and map map sets. A simple method definition of each type of collection is used to establish the characteristics of such a collection. There are several implementations for each collection in Java to handle different scenarios.

Java Basic Series--Collection class library (i)

Related Article

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.