Java Collection Framework

Source: Internet
Author: User

1. Java Collection Classes Overview

A. Origins of the collection

A. Processing multiple identical data we use arrays, but array space fixed does not grow dynamically, inserting or deleting elements is troublesome. We use collections to solve this problem

B. Set characteristics: The element type can be different, the set length variable, the space is not fixed

C. Some data structures and algorithms are encapsulated as collections in Java. A collection is also an object for storing, retrieving, and transmitting objects

D. JCF (Java Collections Framework) collection framework. It provides interfaces and classes for managing collections

B.java three core interfaces in a collection frame: set, List, Map2.Collection interface and iterator interface

A. The set (Collection) interface is located at the top of the set interface and the list interface, and is the parent interface of the set interface and the list interface. Defines some basic methods common to collection objects, which are divided into basic operations, bulk operations, and array operations

B. The iterator interface is an interface for iterating through a collection. Traversal refers to the process of extracting each element from a set.

3. List interface: A column of data, data content can be repeated, in the order of element placement to place elements, will not be rearranged

A. The implementation class for the list interface has a common approach:

Add (): Adds an element to the collection (increment)

Remove (): Removes the element from the collection (delete)

Get (): Gets the element from the collection (check)

Set (): Modify the elements in the collection (change)

Size (): View collection length

The choice of B.list interface implementation class

A. ArrayList: Used most widely when collection element additions or deletions are infrequent. Best for querying

B. LinkedList: Use when you need to add or remove elements frequently in the middle of a collection

C. Vector: Similar to ArrayList, but vector is thread-safe, so performance is lower than ArrayList

4. Generics: Because the elements held in the collection are of type object, when an element is taken out of the collection, it is an objects of the type, so we must cast the type. In order to solve this problem, JDK1.5 provides a new way to deal with generics.

such as:list<string> List = new arraylist<string> ();

5. for Each loop

A new loop has been added to the JDK1.5 called the For Each loop, which greatly simplifies the traversal of the collection

such as: for (String a:list) {

System.out.println (a);

}

6. Set interface: A column of data, data content can not be duplicated, using one of their own internal arrangement mechanism to place elements

A. The implementation class for the set interface has a common approach:

Add (): Adds an element to the collection (increment)

Remove (Object o): Removes the element from the collection (delete)

Size (): View collection length

The choice of B.set interface implementation class

A. HashSet: fast, not sorted

B. TreeSet: Slow, sort

7. Map interface: A list of data pairs, using one of their own internal arrangement mechanism to place elements

A. The map interface is not an inheritance of the collection interface. The map interface is used to maintain key/value pairs (Key/value pairs). Each entry consists of two separate sections:

Key

Value

B. Duplicate keys are not allowed in map.

C.key and value can be instances of any class A common method of D.map interface implementation classes

Put (): Storing key-value pairs in a collection

Get (): The value of the element is taken out according to the key

KeySet (): Remove all keys from the map to form a set

VALUES (): Remove all values from the map to form a collection

Remove (): Removes values by key

The choice of E.map interface implementation class

A. HashMap: fast, not sorted

B. TreeMap: Slow, sort

8. Tools Collections and Arrays

A. The Java Collection Framework provides a tool class collections for manipulating sets, lists, and maps, which provides a number of methods for sorting, querying, modifying, and so on for collection elements

B. The Java Collection Framework also provides the arrays class for manipulating arrays. The arrays class contains various methods for manipulating arrays (such as sorting and searching)

9. Ancient collection classes and interfaces

A. Vector, Hashtable, Stack, properties class, and enumeration interface for the Walker

B. Vector (vector) and ArrayList are almost identical, the biggest difference between them is that the vector class is thread-safe, and ArrayList is not

C. The use of Hashtable and HashMap is almost the same, and the relationship between the two is similar to the relationship between Vector and ArrayList. Hashtable is thread-safe, and HashMap is not. In addition, Hashtable does not allow the key and value to be null, while HashMap is allowed

D. Properties are also frequently used in a class. This class is used to process property files. The so-called attribute file is the map form of the key/value pair of data in a text file with the extension ". Properties", commonly used as a software configuration file

Java Collection Framework

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.