Java object-Oriented programming--15th Chapter Collection Framework

Source: Internet
Author: User

1, set characteristics: Different types of elements, set length variable, space is not fixed

2, in Java, some data structures and algorithms are encapsulated as a collection. A collection is also an object used to store, retrieve, manipulate, and transfer objects.

3. JCF (Java Collections framework) is a Java collection framework consisting of a set of classes and interfaces contained in Javase, whose primary function is to organize the stored data in a certain structure and to access the data in a specific way. The goal is to provide a common framework for working with object collections, reducing the amount of code that programmers handle when dealing with different sets of objects.

4, the collection framework contains three content: interface, implementation class, algorithm.

Collection is the parent interface for list and set.

List interface features: linear, elements can be repeated.

Features of the SET interface: elements are not repeatable.

Map collection: An element contains two objects (key objects and value objects), and keys cannot be duplicated.

5. The difference between a set and an array:

    • The collection can only hold references to objects, and arrays may be basic types, which can be reference types;
    • The length of the set can vary with the number of elements, and the length of the array creation cannot be changed;
    • The collection algorithm is rich, can save the developer many coding, the array algorithm is limited.

6, for loop traversal, can only be used for list collection, iterator mode (Iterator), the algorithm interface of the iterator, for each traversal loop.

7, list three implementation class differences:

    • ArrayList: The bottom part is implemented by an array, with fast traversal and slow insertion or deletion of elements in the middle.
    • Vectors: Similar to ArrayList, the bottom layer is also implemented by arrays, but it is a thread-safe class with low efficiency.
    • LinkedList: Two-way link implementation, stored in a discontinuous memory space, each element in addition to storing data, but also to store the address of the previous and next element. Slow traversal, intermediate insertion, fast deletion.

8, the difference of the set implementation class:

    • HashSet: The elements are sorted by hash code;
    • Linkedhashset: The elements are added in a sequential order;
    • TreeSet: You can sort the elements by the specified rules.

9, the difference between the map implementation class:

    • HASHMAP: The hash code arrangement of the element key object, the thread is unsafe, the null key and the null value are allowed;
    • Hashtable: Hash code arrangement of element Key object, thread safe, NULL key and null value are not allowed;
    • TreeMap: The elements are arranged according to the specified rules.

10, the advantages of map interface: Easy to find (based on the key object, find the Value object, if the key does not exist, return null).

11. When we need to store objects in HashSet and HashMap based on the hashing algorithm, we need to rewrite both the Equals () method and the Hashcode () method, and ensure that when two objects compare the result to true with the Equals () method, both objects have The return value of the Hcode () method is equal.

12, the realization of the comparable interface class, you can achieve natural ordering. If you want to customize the sort, use the comparator interface.

13. If the class being written implements the comparable interface, then this class should override the CompareTo (), Equals (), and Hashcode () methods to ensure that the CompareTo () and Equals methods use the same comparison rules to compare two objects No equal.

14, generics: is allowed to define the class, the interface when the type parameter is specified, the type parameter will be declared when the variable, the creation of the object (that is, the actual parameters passed). After the parameter is typed by generics, we can force the program to check the type of data obtained from the collection at compile time to achieve the purpose of data security.

Java object-Oriented programming--15th Chapter 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.