J2SE BASIC Data structure

Source: Internet
Author: User
Tags comparable

The inheritance relationships of common data structure objects in 1.j2se such as

Collection
........| --------List
........| ..........| ----------ArrayList
........| ..........| ----------Vector
........| ..........| .............| -----Stack
........| ..........| ----------LinkedList
........| --------Set
...................| ----------HashSet.
...................| .............| -----Linkedhashset
...................| ----------SortedSet
.................................| -----TreeSet
Iterator
.....| -------Listiterator
Map
.....| ------Hashtable
.....| ..........| ------Properties
.....| ------HASHMAP
.....| ..........| ------Linkedhashmap
.....| ------Weakhashmap
.....| ------SortedMap
................| ------TREEMAP

2.collection interface and its subclasses

All classes that implement the collection interface and its sub-interfaces can apply the clone () method and are serialized classes.

The list interface features the following:

    • The elements in the list can be accessed randomly;
    • The elements in the list are ordered;
    • Can be added at any position in the list, delete the element;
    • The position of the element in the list remains the same regardless of how many times it is accessed;
    • The elements in the list allow repetition;
    • The iterator can be traversed in one direction or bidirectional by Listiterator.

ArrayList:

  • Using the array as the basic data structure to implement the list;
  • element order storage;
  • When the new element changes the list size, a new array is created internally, and the original data is copied in the past, inserting new elements;
  • Random access is fast, delete non-kinsoku elements slow, new elements slow and cost resources
  • Applicable to the absence of frequent additions and deletions;
  • Less efficient than arrays, consider using array substitution if you do not need a mutable array;
  • Non-thread safe.

Vector

  • Another kind of ArrayList, with ArrayList characteristics;
  • All methods are thread-safe (two-edged swords, and the main difference between ArrayList).
  • Less efficient than ArrayList.

Stack:

  • LIFO data structure

LinkedList:

  • linked object data structure;
  • Random access is very slow, adding and deleting operations quickly, do not consume redundant resources;
  • Non-thread safe.

The features of the set interface are as follows:

    • Duplicate elements are not allowed and can have an empty element;
    • It is not possible to randomly access the contained elements;
    • Only one-way traversal can be implemented with iterator.

HashSet

  • Using HashMap as the basic data structure to store set;
  • The elements are unordered;
  • The order of the iterated access elements is inconsistent with the order of the joins;
  • Multiple iterations of access, the order of elements may be different;
  • Non-thread safe.

Linkedhashset

  • Implement storage based on HashMap and linked list;
  • Iterates through the elements in the same order as the elements they are added to;
  • Multiple iterations of access, the Order of elements unchanged (is an ordered data structure);
  • Poor performance than hashset;
  • Non-thread safe.

SortedSet interface

  • All elements that join SortedSet must implement the comparable interface;
  • The elements are ordered.

TreeSet

  • Implement storage based on TreeMap;
  • Arranges the elements in ascending order after sorting;
  • Non-thread safe.

3.Iterator Interface:

    • Implements a one-way traversal iteration over the set and List collection objects.

Listiterator interface

  • Implements a bidirectional traversal iteration over the list collection object.

4.Map interface

    • The key value pair, the key and the value one by one correspond;
    • Duplicate keys are not allowed.

HashTable

  • The object used as the key must implement the Hashcode (), the Equals method, that is, only object and its subclasses can do the key;
  • Neither the key nor the value can make the empty object;
  • Multiple accesses, the same order of mapping elements;
  • Thread safety.

Properties

  • Both the key and the value are string types.

HashMap

  • The keys and values can be empty objects;
  • The order of the mappings is not guaranteed;
  • Multiple accesses, the order of the mapped elements may be different;
  • Non-thread safe;

Linkedhashmap

  • Multiple accesses, the order of the mapping elements is consistent;
  • Poor performance and HashMap.

Weakhashmap

  • When a key is no longer in normal use, the garbage collector removes it, even if there is a mapping relationship;
  • Non-thread safe.

SortedMap Interface:

  • Keys are sorted in ascending order;
  • All keys must implement the comparable interface.

TreeMap

  • The element storage is realized based on the red-black tree (balanced sort binary tree);
  • Non-thread safe

Additional notes:

Binary Search trees (Ordered binary tree)

    • If the left subtree of any node is not empty, the value of all nodes on the left subtree is less than the value of its root node;
    • If the right subtree of any node is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
    • The left and right subtrees of any node are also two-fork lookup trees, respectively.
    • There are no nodes with key values equal (no duplicate nodes).

Note: If a two-fork lookup tree is degraded into a linear chain with n nodes, the worst-case run time for these operations is O (n).

Red and black Trees:

Although it is essentially a binary search tree, it adds shading and related properties on the basis of the binary search tree to make the red and black trees relatively balanced, thus ensuring that the time complexity of finding, inserting, and deleting red and black trees is the worst O (log n). But how does it ensure that the height of the red-black tree of a N-node remains at Logn? This leads to the 5 properties of the red and black trees:

    • Each node is either red or black.
    • The root node is black.
    • Each leaf node (the leaf node refers to the nil pointer or null node at the end of the tree) is black.
    • If a junction is red, then its two sons are black.
    • For any node, each path to the nil pointer at the end of the leaf node tree contains the same number of black nodes.

It is the red and black trees of these 5 properties, so that a n-node of the red-black tree has always maintained the height of the Logn, thus explaining the above-mentioned "red black tree Search, insertion, deletion of the time complexity of the worst O (log n)" The reason for the conclusion.

It is a red and black tree:

J2SE BASIC Data structure

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.