[JavaSE] Java generic mechanism, javasejava generic

Source: Internet
Author: User

[JavaSE] Java generic mechanism, javasejava generic
**************************************** ********************************* *** Original article: blog.csdn.net/clark_xu Xu changliang's column**************************************** ********************************Java generic Mechanism

Generic is a feature introduced by ipve5. the essence of generic is parameterized type. Application Scenario: During the definition of classes, interfaces, and methods, the Data Type of the operation is specified by the input parameter.

For example, in the definition of the ArrayList class, E in <E> is a generic parameter, and the type can be passed as a parameter when an object is created, in this case, all E in the definition will be replaced with the input parameter;

ArrayList <String> list = new ArrayList <String> (); creates an object. The parameter type of add and the return values of get are replaced with String.

If list. add (100) is used, Java compilation is incorrect because the add parameter is of the String type.

All set types have generic parameters. In this way, you can specify the object type to be put into the marriage when creating the set object. In addition, the java compiler checks the type based on this type.

You can create a List set that stores a specific object through generics, such

List <Point> pointList = new shortlist <Point> ();

Create iterator:

Iterator <Point> it = pointList. iterator ();

5.1 enhanced For loop statements

JavaSE5 introduces an enhanced For loop that can be used to traverse arrays and collections.

For (Point P: pointList ){

P. toString

}

It is understood that a Point object is retrieved from pointList each time, and then assigned to loop traversal p. In fact, during compilation, the enhanced For loop is converted to the iterator mode, but cannot be deleted from the loop.

5.2 List advanced 5.2.1 subList Method

The subList method of List obtains the sub-List. The sub-List and the original List occupy the same storage space. operations on the sub-List will affect the original List, for example:

SubList = list. subList (3,8 );

SubList. set (I, subList. get (I) * 10 );

5.2.2 Queue interface and Queue List Implementation

The Queue is a special linear table. The restricted access mode is FIFO, that is, the element can only be added from one end of the offer and retrieved from the other end of the poll.

JavaSE provides the Queue interface, which is implemented by colleagues using the Queue list. Queue often performs insert and delete operations, for example:

Queue <String> queue = new Queue list <String> ();

Boolean offer (E e); adds an object to the end of the team. If the addition is successful, true is returned. For example:

Queue. offer ("A"); queue. offer ("B ");

E peek (); returns the first element of the queue, but does not delete it.

E poll (); the first element of the team is displayed, that is, the first element is deleted from the team and the return value is

5.2.3 Deque interface and merge list implementation

Deque is a sub-interface of Queue. It defines the so-called "dual-end Queue", that is, the two ends of the Queue are queued and queued with chalk.

If Deque is restricted to only one end of the queue and the data structure of the Stack can be implemented.

5.3 List common algorithms 5.3.1Comparable Interface

When sorting elements in an object array or set, you need to confirm the comparison logic of the elements of the object.

Public interface Comparable <T> {

Public int compareTo (T t)

}

The compareTo method defined by the Comparable interface. The logic is:

L f returns a positive number, indicating that this is greater than the parameter object;

L return a negative number indicating that this is less than the parameter object

L 0 is returned, indicating that this is equal to the parameter object.

Two application scenarios;

(1) Some JDK APIs call methods in the Compareable interface to understand the object size relationship.

(2) Java class implements the COmparable interface to compile the comparison logic of such objects;

Example:

If Point implements the Comparable interface, the generic parameters are of the Point type, so that the parameters of the compareTo method are also of the Point type,

Public class Point implements Comparable <Point> {

Private int x, int y;

Public int compareTo (Point p ){

Int r1 = x * x + y * y;

Int r2 = p. x * p. x + p. y * p. y;

Return r1-r2;

}

}

Note: The consistency with the equals method should be noted when compiling the compareTo method;

**************************************** ********************************* *** Original article: blog.csdn.net/clark_xu Xu changliang's column**************************************** ********************************

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.