Schematic Java common data structure (i)

Source: Internet
Author: User

Recently in the collation of data structure of knowledge, a systematic look at the common Java data structure, the whim of animation to draw data flow process.

Primarily based on JDK8, some features may not be the same as jdk7 before, such as the bidirectional list in LinkedList Linkedhashmap is no longer loopback.

HashMap in the single-linked list is the end of the plug, not the head insert, and so on, the text will no longer be redundant these differences, the structure of this article is as follows:

LinkedList

Classic double-linked list structure, suitable for random insertion, delete. Specifying sequence operations is less performance than ArrayList, which is also determined by its data structure.

Add (E)/AddLast (E)

Add (index, E)

There is a small optimization, he will first determine whether the index is close to the team head or the end of the team to determine from which direction to traverse the link.

1if (Index < (size >>1)) {
2 node<e> x = First;
3 for (int i = 0; i < index; i++)
4 x = X.next;
5 return x;
6} Else {
7 node<e> x = Last;
8 for (int i = size- 1; i > index; i--)
9 x = X.prev;
Ten return x;
One }

By the tail of the team

Get (Index)

The index will be judged first, but the performance is still not good, which is why it is not recommended to use the for (int i = 0; i < Lengh; i++) to traverse the LinkedList, but to traverse it using the iterator way.

Remove (E)

ArrayList

The bottom layer is an array, so the order lookup is fast, the random order is inserted, and the deletion is slow because it involves the subsequent element shift.

Add (index, E)

Expansion

The general default capacity is 10, after expansion, will be length*1.5.

Remove (E)

Loop through the array, determine whether e equals current element, delete performance is inferior to LinkedList.

Stack

Classic data structure, the bottom is also an array, inherited from the vector, advanced after the Filo, the default new Stack () capacity of 10, beyond automatic expansion.

Push (E)

Pop ()

suffix expression

A typical application of stack is the calculation of expressions such as 9 + (3-1) * 3 + 10/2, the computer converts infix expressions to suffix expressions, and then computes the suffix expression.

infix suffix

    • Digital Direct output

    • When the stack is empty, the operator is encountered, directly into the stack

    • The opening parenthesis is encountered and the stack is entered

    • The closing parenthesis is encountered, the stack operation is performed, and the element output of the stack is exported until the left parenthesis of the pop-up stack is not output.

    • Encountered operator (subtraction): POPs all top-level elements with precedence greater than or equal to the operator, and then pushes the operator into the stack

    • Finally, the elements in the stack are sequentially out of the stack, output.

Calculate suffix expression

    • Press the number into the stack when a number is encountered

    • When an operator is encountered, the top two numbers of the stack are popped, the operators are evaluated accordingly, and the results are put into the stack

    • Repeat the process until the right end of the expression

    • The value of the operation is the result of an expression

Queue

The difference with stack is that the stack is deleted and added at the end of the queue, while the queues are deleted in the team header and added at the end of the team.

Arrayblockingqueue

The usual blocking bounded queue in the production consumer, FIFO.

Put (E)

Put (E) the queue is full

1         final ReentrantLock lock = this.lock;
2         lock.lockInterruptibly();
3         try {
4             while (count == items.length)
5                 notFull.await();
6             enqueue(e);
7         } finally {
8             lock.unlock();
9         }

Take ()

When the element is removed, it does not displace the elements behind the array, but instead updates the takeindex to point to the next element.

Takeindex is a ring-shaped growth that, when moved to the tail of the queue, points to 0 and loops again.

1Private EDequeue() {
2Assert lock.getholdcount () = = 1;
3Assert Items[takeindex]! = NULL;
4Final object[] items =This.items;
5 @SuppressWarnings ("unchecked")
6 e x = (e) items[takeindex];
7 Items[takeindex] = null;
8 if (++takeindex = = items.length)
9 Takeindex = 0;
Ten count--;
One if (itrs! = null)
Itrs.elementdequeued ();
Notfull.signal ();
return x;
}

HashMap

The most commonly used hash table, the interview of the children's shoes necessary knowledge, the internal through the array + single-linked list way to achieve. Red-black trees were introduced in jdk8 to optimize the length > 8 list, and we'll talk about it in another space.

put (K, V)

Put (K, V) the same hash value

Resize Dynamic expansion

When the element in the map exceeds the set threshold, the resize (length * 2) operation is performed, the element is manipulated during the expansion process, and placed in the new position.

Here's how:

    • Rehash all elements directly into the jdk7 and place them in a new position.

    • In Jdk8, the new bit bit is 0 or 1 for determining the element's original hash value, 0 is the same index, and 1 is the "original index + Oldtable.length".

1Define two chains
2Original hash value Added bit 0 chain, head and tail
3 Node<k,v> Lohead =NULL, Lotail =Null
4Original hash value Added bit 1 chain, head and tail
5 node<k,v> Hihead =NULL, Hitail =Null
6 node<k,v> Next;
7Loop through the chain chain
8do {
9 next = E.next;
10if ((E.hash & oldcap) = =0) {
11if (Lotail = =Null
Lohead = e;
13Else
Lotail.next = e;
Lotail = e;
16}
17else {
18if (Hitail = =Null
Hihead = e;
20Else
Hitail.next = e;
22             hitail = e;
23        }
24    }  while  ((e = next)!=& nbsp null);
25      //The chain of invariant position before and after expansion
26      if  ( Lotail!=  null) {
27         lotail.next =  null;
28         NEWTAB[J] = Lohead;
29    }
30      //After expansion add the chain of the original array length
31       if  (hitail!=  null) {
32         Hitail.next =  null;
33         NEWTAB[J + oldcap] = Hihead;
34    }

Linkedhashmap

Inherited from HashMap, the bottom layer maintains a doubly linked list to maintain data order. You can set up Accessorder to implement FIFO (insert order) or LRU (Access-ordered) caches.

Put (K, V)

Get (K)

When the Accessorder is false, the element is returned directly, without the need to adjust the position.

When Accessorder is true, the most recently accessed element needs to be placed at the end of the queue.

Removeeldestentry Deleting the oldest element

PS: If feel my share is good, welcome everybody to praise, forward.

Original: cnblogs.com/xdecode/p/9321848.html

Schematic Java common data structure (i)

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.