Java Collection Summary

Source: Internet
Author: User


A Java array is a Java object. The length of an array is fixed. Only data of the same type can be stored in the same array. An array can store data of the basic type, you can also store reference data. When creating an array object, you must specify the length of the array object. Once an array is created, its length cannot be changed.

(1) When creating an array object (when a new array is created), you must specify the length of the array. Once the length of the array is specified, it cannot be changed;

(2) The data types in the same array must be the same, which can be basic or reference type.

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

Java collections

(1) A set of data that can be stored and manipulated with an unfixed number

(2) The element in the set can only be the reference type, but cannot be the basic type.

(3) The set is divided into the following three types:

A. set is not sorted and is not repeated.

B. List sorting by index position allows you to repeatedly retrieve Objects Based on the index position of objects in the Set (this is similar to an array)

C. Each element in the map set contains a key object and a value object.

There are no duplicate key objects in the set, and the value objects can be repeated.

(4) Collection interface (pay attention to the concept of reference)

A. The collection interface declaration applies to general methods of Java collections (only set and list ).

Add () adds an object reference to the set

Clear () deletes all objects in the set, that is, the set no longer holds references to these objects.

Contains () determines whether the collection holds a reference to a specific object

Isempty () determines whether the set is empty

Iterator () returns an iterator-type object through which all elements in the set can be traversed.

Remove () deletes an object reference from the set

Size () returns the number of elements in the set.

Toarray () returns an array containing all elements in the set.

Both the list and set interfaces inherit from the collection interface. These types of objects can use the above methods, but they cannot call the above methods for map objects.

 

(5) iterator Interface

A. The iterator interface hides the data structure of the underlying set and provides a unified interface for Traversing various types of Sets

B. The iterator interface declares the following methods:

Hasnext ()

Next ()

Remove ()

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------

1. Set

(1) set is the simplest set, which is not sorted and has no repeated objects.

(2) set is an interface with two implementation classes: hashset and treeset.

A. The hashset class uses the hash algorithm to access objects in the Set, which provides fast access;

B. hashset has a subclass of linkedhashset, which not only implements the hash algorithm, but also implements the linked table data structure to improve the performance of inserting and deleting elements;

The treeset class implements the sortedset interface and has the sorting function.

(3) reference of objects stored in the collection

(4) When the add method in the Set adds an object reference to the set, first determine whether the object already exists in the set, the add method of the set determines whether the object is the same by calling the equals method of the object.

Whether it is hashset or treeset, the set interface is implemented. The common characteristics of these interfaces are that there are no repeated elements in the Set and the set is not sorted.

""

1.1 hashset

(1) access the objects in the Set Based on the hash algorithm, with good access and search performance

(2) when an object is added to the set, hashset calls the hashcode () method of the object to obtain the hash code, and further calculates the storage location of the object in the Set Based on the hash code.

(3) in Java. lang. the hashcode () and equals () methods are defined in the object. The equals () method defined in the original object is to compare whether the object is equal according to the memory address. Therefore, for the object, if the equals method returns true, the two references actually reference the same object, and the referenced hash codes must be the same.

To ensure that the hashset can work normally, when two objects are required to use the equals () method to compare the results to true, their hash codes are also the same

If the user-defined class overwrites the equals method of the object but does not overwrite the hashcode method, the object's hash code is different when the equals method returns true, this will make the hashset unable to work normally. the user is intended to handle the same object reference, but because the hashcode () method is not overwritten, the hash code is different, and the hashset will be processed as different objects.

1.2 treeset

(1) treeset implements the sortedset interface to sort objects in the set. When a treeset adds an object to the set, it is inserted into the ordered object sequence, treeset supports two sorting methods: Natural sorting and custom sorting.

When a treeset inserts an object into the set, it will be automatically sorted: What? Call the compartto method of the object to compare the object size and then sort it in ascending order. Therefore, the object must implement the comparable interface,

1.2.1 natural sorting

A. Some classes in the Java class library implement the comparable interface, such as integer, double, and string. The comparable interface has a compareto (Object O) method and returns the integer type;

B. treeset calls the compareto () method of the object to compare the object size in the set and then sort it in ascending order. This sort method becomes a natural sort.

C. When using natural sorting, you must add objects of the same type to the treesset, and the classes of these objects must implement the comparable interface.

D. For objects already in the treeset, if their attributes are modified, the treeset will not re-order the set.

1.2.2 customized sorting

A. java. util. the comparator <type> interface provides a collective sorting method. <type> specifies the type of objects that can be compared. In this way, a class is defined to implement Java. util. comparator <type> interface, and then implement the compare method in the class definition. When creating a treeset object. util. comparator <type> type objects are passed into the treeset constructor as parameters. In this way, the compareto method is automatically called when the treeset inserts an object into the set.

========================================================== ========================================================== =

2. List

The main feature of list is that its elements are stored in the current mode, and repeated objects can be stored in the collection.

2.1 List Implementation classes include

Arraylist

A. a variable-length array;

B. Since it is an array, quick Random Access to elements is allowed;

C. It is slow to insert or delete elements to and from arraylist.

Shortlist

A. Use the Linked List Data Structure in implementation

B. Fast element insertion and deletion to the list

C. The random access speed is relatively slow. The random access means that the elements in a specific location are located based on the index.

D. addfirst (0 addlast () getfirst () Get Laster () removefirst () and removelast () methods are provided so that the queue list can be used as a stack, queue, and bidirectional queue.

2.2 access to elements in the list

Using arraylist, you can directly access list elements at a specified position through indexes.

2.3 list sorting

List only provides sorting by index location. If you want to sort objects in the list by other specific methods, you can use the comparator interface and the collections class. The collections class is an auxiliary class of the Java class library, provides various static methods to manipulate the set. The sort () method is used to sort objects in the list.

Sort (list) performs natural sorting on the list

Sort (list, comparator) customizes the list based on the compare method of the comparator implementation class

2.4 listiterator Interface

The listiterator () method of list returns a listiterator object. The listiterator interface inherits iterator, hasnext () Next ()

Add () hasprevious (0 previous ()

2.5 obtain the list object of the specified length

The Java. util. arrays tool class provides the aslist () method, which encapsulates a Java array into a list object.

2.6 fast Random Access to Java Arrays

Fast Random Access to arraylist

Fastest insert and delete in the shortlist

========================================================== ========================================================== =

3. Map

3.1 A set of ing key objects and value objects

3.2map is a set. Each element of map contains a key object and a value object.

3.3

A. when inserting elements into map, a key object and a value object must be provided.

B. When retrieving from the map set, the corresponding value object is returned as long as the key object is given.

3.4map key values cannot be repeated, but they can be repeated

3.5 methods in Map

Put ()

Get ()

Entryset () returns a set that stores map. Entry elements. Each map. Entry object represents a pair of keys and values in map.

3.6 two map implementations

A hashmap

A.1 fast access to elements based on Hash Codes

A.2 equals () and hashcode () methods must be consistent

B. treemap

When elements are inserted into a set using treemap, they are sorted. Two methods are supported: Natural sorting and custom sorting.

 

========================================================== ========================================================== =

Collections

1. provides a series of static methods used to manipulate Java collections. Some of these methods are used to manipulate list-type collections, and some can be used to manipulate all Collection types or map-type collections.

2. List is actually a variable-length array (random access is very convenient)

The method applicable to the list type in collections

Copy (list DEST, list SRC );

Fill (list, object O );

Sort (list) performs natural sorting on the list

Binarysearch (list, object)

The following methods in collections apply to collection or map collections.

Object max (Collection Coll)

Object max (Collection Coll, comparator comp)

Object min (Collection Coll)

Object min (Collection Coll, comparator comp)

Returns an unchangeable set that contains only the objects specified by a parameter. The set contains only one element and cannot be changed because the set cannot be modified, therefore, you do not need to adopt synchronization Measures to Improve concurrency performance.

Set singletonset (Object O)

List singletonlist (Object O)

Map singletonmap (Object key, object value)

Returns a thread-safe set object based on the original set. In a multi-threaded environment, one thread is not allowed to sort the set, and the other thread modifies the set.

Collection synchronizedcollection (collection C)

List synchronizedlist (list List)

Map synchronizedmap (MAP map)

Set synchronizedset (Set set)

Based on the original set, the returned set view that cannot be modified is returned. The set elements cannot be modified to generate the original set view. The thread accesses this set view and does not need to adopt synchronization measures, because the view cannot be modified, it is equivalent to an immutable class and always thread-safe. The program can read the View content but cannot modify it.

Collection unmodifiedcollection (collection C)

List unmodifiedlist (list List)

Map unmodifiedmap (MAP map)

Set unmodifiedset (set S)

 

For non-variable classes, thread synchronization is not required, because the thread does not modify the shared data, so it will not modify the operands.

 

 

 

 

 

 

 

Http://blog.csdn.net/JBuilder3/archive/2010/05/28/5630846.aspx

 

Related Article

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.