Java Collection framework and java Collection

Source: Internet
Author: User

Java Collection framework and java Collection

1. Java Collection class overview

A. Origins of Collections

A. We use arrays to process multiple identical data, but the array space cannot grow dynamically. It is troublesome to insert or delete elements. We use a set to solve this problem.

B. Set features: the element types can be different, the set length can be variable, and the space is not fixed

C. Java encapsulates some data structures and algorithms as a set. A set is also an object used to store, retrieve, and transmit objects.

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

B. Three core interfaces in the Java Collection framework: Set, List, and Map 2. Collection interface and Iterator Interface

A. The Collection interface is located at the top of the Set interface and List interface, and is the parent interface of the Set interface and List interface. Defines some basic methods of Collection objects. These methods include basic operations, batch operations, and Array Operations.

B. The Iterator interface is an interface used to traverse a set. Traversal refers to the process of extracting each element from the set.

3. List interface: a column of data. The data content can be repeated and the elements are placed in the order of element inserts without being rearranged.

A. The implementation classes of the List interface share the same method:

Add (): add (add) elements to the set)

Remove (): remove (delete) elements from the set)

Get (): get elements from the set (query)

Set (): Modify the elements in the set)

Size (): view the length of a set

B. Select the implementation class of the List Interface

A. ArrayList: it is the most widely used and is used when collection elements are added or deleted infrequently. Most Suitable for queries

B. Sort list: used when elements need to be added or deleted frequently in the middle of the set.

C. Vector: similar to ArrayList, but Vector is thread-safe, so its performance is lower than that of ArrayList.

4. Generic: because all elements saved in the set are of the Object type, when an element is retrieved from the set, it is of the Object type. Therefore, we must perform forced type conversion on it. To solve this problem, JDK1.5 provides a new processing method generic.

For example, List <String> list = new ArrayList <String> ();

5. for each loop

JDK1.5 adds a new type of loop called for each loop, which greatly simplifies the traversal operation on the set.

For example: for (String a: list ){

System. out. println ();

}

6. Set interface: a column of data. The data content cannot be duplicated. You can use an internal arrangement mechanism to place elements.

A. The implementation classes of the Set interface share the same method:

Add (): add (add) elements to the set)

Remove (Object o): remove (delete) elements from the set)

Size (): view the length of a set

B. Select the Set interface implementation class

A. HashSet: fast and unordered

B. TreeSet: slow speed and sorting

7. Map interface: uses an internal arrangement mechanism to place elements in a column of data pairs.

A. The Map interface is not inherited from the Collection interface. The Map interface is used to maintain key/value pairs (key/value pairs ). Each entry contains two separate parts:

Key

Value

B. Duplicate keys are not allowed in Map.

C. key and value can be the common methods of the implementation class of any class instance D. Map Interface

Put (): saves key-value pairs to the set.

Get (): extracts the element value based on the key.

KeySet (): extracts all the keys in the Map to form a Set.

Values (): extracts all values from the Map to form a Collection.

Remove (): remove a value based on the key

E. Selection of Map interface implementation classes

A. HashMap: fast and unordered

B. TreeMap: slow speed and sorting

8. Tool classes Collections and Arrays

A. the Java Collection framework provides a collection tool class for operations such as Set, List, and Map. This tool class provides a large number of methods for sorting, querying, and modifying Set elements.

B. the Java Collection framework also provides the Arrays class for operating Arrays. The Arrays class contains various methods used to operate Arrays (such as sorting and searching ).

9. Ancient collection classes and interfaces

A. Vector, Hashtable, Stack, Properties class, and Enumeration interface of the traversal Tool

B. The usage of Vector and ArrayList is almost identical. The biggest difference between them is that the Vector class is thread-safe, while the ArrayList is not

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

D. Properties is a class that is frequently used till now. This class is used to process attribute files. Attribute files include key/value pairs in Map format in a text file with the extension ". properties". These files are commonly used as software configuration files.

 

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.