Java Collection----Basic schema

Source: Internet
Author: User
Tags comparable int size

introduction of Collection Class

The origin of the collection class:

objects are used to encapsulate unique data, and objects need to be stored if the number of objects is indeterminate.

is stored using the collection container

Set Features:

1. Containers for storing objects

2. Variable length of the set

3. The base data type cannot be stored in the collection

the difference between a collection and an array:

The collection is variable-length, while the array is immutable

II. basic architecture of the collection classes provided by Java

The common use of these two interfaces, of course, is more than that.

|--collection interface (single-column collection)

|--list interface

|--arraylist

|--linkedlist

|--set interface

|--hashset

|--linkedhashset

|--treeset

|--map interface (dual-column collection)

|--hashmap

|--linkedhashmap

|--treemap

|--hashtable

|--properties

Third, collection interface

The root interface in the collection hierarchy . Collection represents a set of objects, also known as Collection elements . A few

Collection allows for duplicate elements, while others do not. Some of the collection are orderly, while others are unordered.

The JDK does not provide any direct implementation of this interface: it provides more specific sub-interfaces (such as Set and List) implementations. This interface is typically used to pass

Collection, and operate these collection in areas where the greatest universality is required.

Common methods for collection interfaces:


1. Add.

boolean Add (Object obj);

boolean addall (Collection coll);

2. Delete.

boolean remove (Object obj);

boolean removeall (Collection coll);

void Clear ();

3. Judgment:

Boolean contains (Object obj);

boolean containsall (Collection coll);

boolean isEmpty (); Determines whether there are elements in the collection. The bottom level is size ().


4. Get:

int size ();

Iterator Iterator (); How to remove elements: iterators

5. Other:

boolean retainall (Collection coll);

object[] ToArray () transfers the collection to the array.

Iv. List Interface

He is the sub-interface of the collection interface, the most characteristic of which is the angular mark, so the insertion of the collection data is orderly,

And the position of the data can be controlled precisely according to the angle mark. The following is a comparison of its implementation classes:

|--vector: The internal is the array data structure, is synchronous, almost does not have. jdk1.0 twice-fold extension (delete query is very slow)

|--arraylist: The internal array data structure is not synchronized. instead of vectors. 1.5 times times the extension (the speed is relatively slow, check

Find Fast)

|--linkedlist: The internal is a linked list data structure, is not synchronized. (delete fast, find slow) LinkedList also has a horn, because

He is a sub-class of the list interface, the most important feature of the list is the angular mark

Five, set interface

It is also a sub-interface of the collection collection, it does not have a corner label, the data is unordered, and the element does not allow repetition. Here's his implementation.

Class:

|--hashset: The internal data structure is a hash table and is out of sync.

Determine if the elements are the same:

(1): Determine whether the hash value of the two elements is the same. If the same, then determine whether the contents of two objects are the same

Note: Judging the hash value is the same, actually judging the object's Hashcode () method. The Equals method is used to determine the same content.

Note: If the hash value is different, it is not necessary to determine the equals

Hash conflict: The hash value is the same and the content is different. The hash will be deferred for storage or a location to be re-calculated

|--treeset:

Its bottom layer is a two-fork tree, which uses the natural order of the elements to sort, is out of sync

the comparable interface must be implemented when you save a custom object

The uniqueness of the element is judged by the return value, which is considered to be the same element if returned as 0

TreeSet How the elements are sorted one:

the comparable interface is required for the element to have its own comparison function. Overriding the Comparato method

TreeSet the sorting method of the collection two:

Let the collection itself have a comparison function, using a comparer, define a class to implement the comparator interface, override the Compare method, and use the class object as a parameter

Constructors passed to the TreeSet collection

|--linkedhashset: Ordered and not duplicated. It's an extension of hashset.

Six, Map interface

Add a pair of elements at once. Collection adds one element at a time, and map is also called a two-column collection. Collection is also called a single-column collection

In fact, the map collection is stored in the key-value pairs, the map set must ensure the uniqueness of the key. Here is his implementation class:

|--hashtable: The internal structure is a hash table, which is synchronous. Null is not allowed as a key as a value

|--properties: The key and value that is typically used to store the configuration file. Very common

|--hashmap: The internal structure is a hash table, not synchronous. Allow NULL as a key as a value

|--LINKEDHASHMAP: Internal structure for chain table plus hash table, orderly, how to deposit in how to take out

|--treemap: The internal structure is a two-fork tree, not synchronous. You can sort the keys in the map collection








Java Collection----Basic schema

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.