Java container (list, set, map), javamap

Source: Internet
Author: User

Java container (list, set, map), javamap
Simplified diagram of java container Class Library:

(Dotted boxes represent interfaces, while solid boxes represent common classes,

The hollow arrow indicates that a specific class implements the interface, and the solid arrow indicates that a Class Object indicated by the arrow can be generated for a class)

 

The main types of inherited collections are Set and List.

 

List:
 

ArrayListThe internal implementation is to use arrays to speed up random access and slow the deletion or insertion of elements.

 

ShortlistThe internal implementation is to use a linked list. The random access speed is slow, and the deletion and insertion of elements are relatively fast.

Generally, the best practice may be to use ArrayList as the default preferred option. You can select the rule list only when you need additional features or when the program performance is deteriorated due to frequent insertion or deletion of elements from the table.

Vector and Stack are legacy classes. They are only intended to support old programs and should be avoided during programming.

 

Set:

Hash

 

HashSet: Designed for fast search, which can be considered to be based onHash table. Elements stored in HashSetHashCode () must be defined ().

 

LinkedHashSet:The query speed with HashSet and the order in which elements are maintained using the linked list (in the order of insertion or the least recently used order)

 

Tree

 

TreeSet:Keep the order of the Set, bottom layer is the tree structure (Red/black tree ). It can be used to extract ordered sequences from the Set. ElementThe Comparable interface must be implemented.You can also input the Comparator parameter when constructing a TreeSet.

Class Stone {private int volume; public Stone (int volume) {this. volume = volume;} public int getVolume () {return volume ;}//........... omitted .......... //} class Stick implements Comparable {private int length; public Stick (int length) {this. length = length;} public int getLength () {return length;} @ Override public int compareTo (Object o) {Stick st = (Stick) o; return this. getLength ()-st. getLength ();}//........... omitted .......... //} public class MyComparator implements Comparator <Stone >{@ Override public int compare (Stone st1, Stone st2) {return st1.getVolume ()-st2.getVolume ();} public static void main (String [] args) {new TreeSet <Stone> (new MyComparator (); // input Comparator new TreeSet <Stick> (); // Stick implements Comparable }}

 

The equals () method must be implemented for elements, whether in hash or tree storage..Although hashCode () is mandatory only when HashSet or javashashset is put in at the time, for a good programming style, it is necessary to overwrite the equals () method and the hashCode () method.

 

SortedSet (Interface) ensures that elements are sorted by object comparison functions. The main methods are as follows:

Comparator comparator () returns the Comparator used by the current Set. If null is returned, it indicates that the Comparator is in the natural order (that is, in the Comparable implemented by the element)

CompareTo () method sorting ).

Object first () returns the first element of the container.

Object last () returns the last element of the container.

SortedSet subSet (fromElement, toElement) generates a subSet of this Set, range from fromElement (included) to toElement (not included)

SortedSet headSet (toElement) generates a subset of this Set, composed of elements smaller than toElement

SortedSet tailSet (fromElement) generates a subset of this Set, which consists of elements greater than or equal to fromElement.

 

TreeSet implements SortedSet, including the SortedSet method.

 

 

Optional

You can add or remove methods in the Collection interface. That is to say, classes that implement the Collection interface do not need to provide meaningful functions for these methods. This violates the rules in the object-oriented design (no matter how you choose to implement the interface, you should ensure that these messages can be sent to the interface ).

OptionalDeclaring that some methods are called will not execute meaningful actions. On the contrary, they will throw an exception.

OptionalLogically, when implementing an interface, you still need to override the so-called optional method of the interface, but you do not have to provide meaningful implementation. If this method is not supported, you can throw UnsupportedOperationException in this method.

 

SoWhy do methods need to be defined as optional?

This prevents too many interfaces in the design. For example:

The following three requirements are available)

The container of the set content cannot be modified,

Only containers with elements can be added,

Only containers with elements can be deleted.

Now we need to define an interface for each of the above three requirements. Obviously, this is better than the currentCollectionTo define more interfaces. This will cause too many interfaces, and the entire container class library will be more complex.

Note: UnsupportedOperationException must be a rare exception. That is to say, for most classes, all operations should work. Only in the special column will there be unsupported operations.

 

Map

HashMap is implemented based on a hash (it replaces HashTable, and HashTable is an outdated class ).

LinkedHashMap is similar to HashMap, but the iteration duration. The order in which key-value pairs are obtained is the insertion order.

Or the least recently used sequence (if accessOrder = true is input when LiskedHashMap is constructed ).

It is a little slower than HashMap, but faster during iterative access because it uses a linked list to maintain internal order.

TreeMap is implemented based on the red and black trees. Check whether keys or key-values are sorted (the order is determined by Comparator or Comparable)

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.