Java Collection Framework Generic __java

Source: Internet
Author: User
The Java Collection framework is divided into two broad categories: the collection class that loads the data and the tool class that operates the data.

The Java Collection class is based on the collection interface and is classified as set, list and map three interfaces according to different requirements.

A Java collection is like a container that can be used to place references to multiple objects in a container. The elements in a container (references to objects) are of type object, so you can hold an object reference of any type by upward transition, but you must transition (force transition) to the original type when you remove the element from the container.
It is noteworthy that the elements in the collection class container do not hold the base data type, and can hold the wrapper class type.


collection classes that load data: The collection class defines some basic methods common to the collection class through the collection interface, which are divided into basic operations (add, delete, modify, and find container elements), bulk operations (bulk Add, delete container elements), and array operations (conversions between collections and arrays).


Set, list, and map interfaces
1. List interface: It inherits from the collection interface and is an ordered set that allows for duplicate elements. The implementation classes (specific classes) of the list interface are commonly used for ArrayList (sequential table sets) and LinkedList (linked list collections).
1>arraylist (Sequential table collection): ArrayList class extends Abstractlist and implements the list interface. The ArrayList class provides a dynamic array that grows with need. In Java, standard arrays are fixed-length and cannot be lengthened or shortened after an array is created, which means we have to know in advance how many elements the array can hold. However, it is not possible to know how large an array is required until run time. The ArrayList class is to solve this problem.
Feature: Efficient for using index query elements, it can use indexes to quickly locate element locations, but deleting or inserting elements is inefficient because using arrays, you need to move the following elements to adjust the order of the indexes. It is the most widely used, can be used in the collection element increase or deletion operation is not frequent, the most suitable for query operations.

Java generics

Because the elements that are saved in the collection are object types, when an element is taken out of the collection, it is a objects reference of the object type, so we have to force the transition downward, in order to solve this problem, JDK1.5 supports a new feature--java generics.


Generic refers to the definition (class definition, the definition of the method, the definition of formal parameters, the definition of member variables, etc.), specify that it is a common type, that is, the data type can be any type, the specific call to the general type to convert to the specified type use.
Generics are an extension of the type system of the Java language, to support the creation of a class that can be parameterized by type, you can think of a type parameter as a placeholder for the type you specify when you use a parameterized type, just as a method's formal argument is a placeholder for a value that is passed at run time.

Advantages of generics: The benefits of introducing generics into the Java language are simple and safe. The benefit of generics is to check type safety at compile time, and all downward forced transitions are automatic and implicit, increasing the reuse rate of code.

Note: Generics are not allowed in static methods, static initialization blocks, static member variables.

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.