Java Data structure

Source: Internet
Author: User

This article draws on the excellent article: http://blog.csdn.net/zhangerqing/article/details/8122075

Data:



The table below may be a more direct representation of their differences and connections:

Interface

Briefly

Realize

Operating characteristics

Member Requirements

Set

Members cannot be duplicated

HashSet

Traverse members outside of an unordered manner

Members can be objects of any object subclass, but if the Equals method is overridden, Also pay attention to modifying the Hashcode method.

treeset

The members are traversed in an orderly manner, and additional operations that implement the order of sortedset,  supporting subsets are implemented

< Span style= "font-family: Song body; FONT-SIZE:12PT "> Members require the implementation of the Caparable interface, or use &NBSP; comparator constructs TreeSet. Members are generally of the same type.

Linkedhashset

External traversal of members by member insertion Order

Members are similar to HashSet members

List

Provides index-based random access to members

ArrayList

Provides fast, index-based member access with better support for tail-member additions and deletions

Members can be objects of any object subclass

LinkedList

Better support for adding and removing members from any location in the list, but poor performance for index-based member access

Members can be objects of any object subclass

Map

Save key-value pairs of members, sort keys based on key-find operations, CompareTo or Compare methods

HashMap

To meet the user's common needs for map

Key members can be objects of any object subclass, but if the Equals method is overridden, notice that the Hashcode method is modified.

treemap

supports an orderly traversal of keys, it is recommended to use HashMap to add and remove members, and finally generate treemap from HashMap; Additional implementation of SORTEDMAP interface, supporting the order of sub-map operations

key members require the implementation of the Caparable interface, or use comparator to construct treemap. Key members are generally of the same type.

linkedhashmap

preserve the insertion order of keys, check the equality of keys and values with the equals  method

Members can be objects of any object subclass, but if the Equals method is overridden, Also pay attention to modifying the Hashcode method.

identityhashmap

use ==  to check the equality of keys and values.

members are using strictly equal

Weakhashmap

Its behavior relies on garbage collection threads, and there is no absolute reason to use less



HASHMAP Internal storage structure

Java data storage in the bottom of the two structures, one is an array, the other is linked list, the characteristics of the array: continuous space, addressing quickly, but in the deletion or addition of elements need to have a larger movement, so the inquiry speed, and delete more slowly. And the chain list is just the opposite, because the space is not continuous, addressing difficult, adding and removing elements only need to modify the pointer, so query slow, and delete fast. Is there a data structure that synthesizes arrays and linked lists in order to play their respective strengths? The answer is YES! is: Hash table. Hash tables have a fast (constant-level) query speed, and relatively fast delete and deletion speed, so it is very suitable for use in the environment of massive data. In general, the method of implementing a hash table uses the "Zipper method", which we can understand as "an array of linked lists",



Second, ArrayList

ArrayList is based on an array implementation, so it has the characteristics of the array, that is, the query is faster, but the change, the speed of the insertion is a bit slow, but the following will be introduced in the LinkedList is to solve the problem,LinkedList based on the linked list, and ArrayList complementary, so in the actual development we should according to their own needs to decide which one to use.


Third, LinkedList

The LinkedList is implemented in a two-way loop list with high speed for insert and delete operations, and we can also use LinkedList to implement queues and stacks.


Iv. Weakhashmap

Before understanding the collection class, it is recommended to understand the Java garbage collection mechanism, Weakhashmap is used in the cache system, that is, when the system memory is tight, can be at any time GC, but if the memory is not tense can be used to store some cache data. Because if you use HashMap, it is basically a strong reference, even if there is not enough memory, it will not be GC, so the system will be reported different


Wu, HashSet

MAP, Set interface implementation class Hashmap,hashset the biggest difference is

HashMap must keep key key must be unique but stored objects can be the same

HashSet is a storage interface that is also based on HashMap, but HashSet stores objects as keys in Map.put (), so HashSet stored objects must be unique


Vi. Array of arrays

In the Java language, an array is one of the simplest composite data types. An array is a collection of ordered data, and each element in the array has the same data type, and it is possible to uniquely determine the element in the array with a uniform array name and subscript . Arrays have one-dimensional arrays and multidimensional arrays.

(2). Since the bottom of the ArrayList is implemented with arrays, what is the difference between the array and the group? Or where is the greatest benefit?

A: ArrayList bottom is variable-length array maintenance, do not need to define its size, if the length is not enough to automatically expand to the original length of one times, the size of the array at the time of definition is a fixed value, does not automatically expand, the efficiency of the array is higher than the efficiency of the set, each has a focus

The biggest benefit is that you don't need to re-write a ArrayList, you need the array when ArrayList provides ToArray () is the fastest


EG://string composite data type string[] array = new STRING[5]; Array[0] = "a"; Object composite data type father[] Father = new FATHER[5]; Father[0] = people; Interface composite data type animal[] Animal = new ANIMAL[5];  

Seven JAVA data type Basics

Simple data types include:
Integral type (Interger): Byte, short, int, long
Floating-point type (floating): float, double
Character type (textual): Char
Boolean type (Logical): Boolean

Composite data types include:
Class
Interface
Array
String


Eight, queue queues

Brief introduction

Queue is a very common type of data structure, in Java the queue is an interface, it just defines a basic queue should have what the function of the Protocol. In fact, there are many implementations of queue, some are implemented by linear table, and some are based on linked list. There are also applications for multi-threaded environments. There are several classes in Java that have the queue function: Abstractqueue,arrayblockingqueue, Concurrentlinkedqueue, Linkedblockingqueue, Delayqueue, LinkedList, Priorityblockingqueue, Priorityqueue and Arraydqueue. In this article, we mainly discuss two common implementations: LinkedList and Arraydeque.

(1) arraydeque

With the foundation of our previous analyses, we can easily guess the internal implementation mechanism of Arraydeque. It internally uses an array to hold the specific elements, and then uses head, tail, to indicate the head and tail of the queue. Their definition is as follows:

Java code

1. Private transient e[] elements;

2.

3. Private transient int head;

4.

5. private transient int tail;

6.

7. private static final int min_initial_capacity = 8;

The default length of Arraydeque is 8, so the exponential value defined as 2 has some benefit. We'll see when we adjust the length of the array later. One thing to note about tail is that the index position where tail is located is a null value, and the element in front of it is the last element in the queue.






Java 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.