A collection of Java basic knowledge summaries

Source: Internet
Author: User

A collection is also known as a container class. It is functionally equivalent to a container.

A collection of Java (a container), which is used to "Mount objects."

Set Set

The set set is almost identical to the collection collection. They act almost the same.

There are two ways to traverse the set:

1. Using iterators

2. Using the Foreach Loop

HashSet

Storage mechanism of HashSet:

1. When an element is added, HashSet calls the object's Hashcode () method to get an int value.

2, according to the hashcode () returned int value, calculates its storage position in the hashset (the bottom layer actually uses the array storage element index, calculates obtains in the array the index value)

3, if the join position is empty, then join directly, if the location already has elements, then this will form a linked list.

This is similar to when you take an element.

1. When going to an element, HashSet calls the object's Hashcode () method to get an int value.

2. Calculates the location (the index in the array) of the "underlying array" in HashSet, based on the int value returned by Hashcode ().

3. If the position is exactly what you are looking for, simply take it out, such as if the position is linked by a list, then search for the elements in the linked list by "one by one".

Method of construction of HashSet: HashSet (int initialcapacity, float loadfactor)

Initialcapacity: Controls the length of the underlying array, which defaults to 16

Loadfactor: The load factor, hashset the basis for determining if the underlying array is almost full. When the array is judged to be almost full, the system automatically creates an array that is twice times the original length, and copies the elements of the original array to the new array, which consists of the original number for garbage. The jargon is called "rehash (heavy hash)".

The default size of Loadfactor is 0.75.

The smaller the loadfactor, the more memory is consumed and the larger the loadfactor, the lower the performance.

HashSet how to tell if two objects are equal:

1, two objects with the same hashcode () return value

The Equals () method of 2, two objects compares a return value of true

This requires that the hashcode () and Equals () methods of our custom classes are consistent, requiring that the key attributes used for overriding equals () be consistent with the key properties of the computed Hashcode ().

Subclass of HashSet: Linkedhashset

It is the same as the HashSet storage mechanism.

However, Linkedhashset maintains a linked list to record the order in which elements are added.

TreeSet

Feature: Ensure that the elements in the set are "sorted in size".

TreeSet ———— It's a standard red-and-black tree.

Tree--two-tree, binary tree, balanced binary tree----red and black trees.

Storage mechanism of TreeSet:

The bottom consists of a "red-black tree" that stores all the data. Access performance and retrieval performance are also better.

In the absence of a large list of HashSet, hashset performance is better than treeset performance.

TreeSet requires that the object be sortable:

1, natural sort. All collection elements are required to implement the comparable interface.

Once the collection element implements the comparable interface, the collection elements themselves can be sorted.

2, custom sorting. Requires a comparator object to be passed in when the TreeSet object is created.

The comparator object is responsible for sorting the collection elements, and the collection elements do not need to implement the comparable interface.

TreeSet how to count two objects equal?

1. Only two objects that are compared by the CompareTo () method have a return value of 0,treeset to consider the two objects equal.

List Collection

The list collection encapsulates the data structure of a linear table .

It provides a large number of methods for saving and fetching elements according to the index.

Because the list saves and takes elements based on the index, it has a way to iterate over the elements.

 

Storage mechanisms for ArrayList and vectors:

Their bottom-level is array-based, and their storage of elements is entirely array-based. -So the performance is very fast.

  

The difference between ArrayList and vectors:

1, Vector is JDK1.0 set, from JDK1.2 Sun Company redesigned ArrayList to replace the vector.

2, vector is thread-safe, ArrayList is thread insecure. But ArrayList's performance is better than that of a vector.

Even in multi-threaded environments, you can use the collections method to turn ArrayList into thread-safe.

LinkedList

is both a linear table, a queue, or a stack. (Stacks and queues are constrained linear tables).

The bottom of the LinkedList is based on the linked list implementation. It is generally thought that its performance is inferior to ArrayList.

ArrayList: Because the elements are accessed based on the index of the underlying array, the performance is very fast.

When inserting or deleting an element, all subsequent elements must follow the "move whole".

LinkedList: Because the underlying uses a linked list to store elements, the performance is low because the elements are accessed according to the historical history.

When inserting or deleting an element, all subsequent elements do not need to "move the whole", so the performance is very good.

Map Collection

Comparing the above two graphs, we find that the structure of them is very familiar, and the map corresponds to set one by one. The implementation of the underlying set is essentially a method of the map subclass that can be verified by viewing the set-related API documentation. When the value of map is null, the map becomes set when only the key is considered.

The HashSet is implemented by HashMap, and HashMap controls the storage of data in the collection by means of a "hash" algorithm, similar to "a carrot and a pit".

The bottom of the TreeSet is implemented by TreeMap, TreeMap is the real red-black tree.

Note: For map, value is just its appendage, there is little requirement, so the map is mostly on key by request.

HashMap

HashMap uses the return value of the Key's Hashcode () method to calculate its deposit and fetch position.

HashMap how to count two keys repeat?

1. True for the return value compared by the Equals () method

The Hashcode () return value of 2, two keys is the same

TreeMap

The underlying red and black tree only sorts the keys.

TreeMap requires that key must be sortable:

1, natural sort. All keys are required to implement the comparable interface.

2, custom sorting. An object that is required to pass in a comparator interface when creating a TreeMap object.

How does treemap count two keys equal?

1. The return value compared by the CompareTo () method is 0, which indicates that two elements are equal.

The difference between HashMap and Hashtable:

1, Hashtable is from JDK1.0, try to use less.

2, Hashtable does not allow NULL as key, value. But HashMap allowed.

3, Hashtable is thread-safe (poor implementation)

HashMap is not thread-safe. The performance of HashMap is good.

In addition to this thought HashMap and Hashtable are almost the same.

A collection of Java basic knowledge summaries

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.