Dark Horse Programmer-java Basics-Set frame-treeset, binary tree, generics

Source: Internet
Author: User
Tags comparable natural string set set sorted by name

First Lecture TreeSet

1. Overview

TreeSet can sort the elements in the set set, in natural order.

2. Demo Code

Output Result:

Summary: TreeSet will sort the elements naturally, with uppercase in front of lowercase.

Second lecture TreeSet storing a custom object

1. Overview:

Stores the custom object in the TreeSet collection.

2, ideas:

Customize student classes and store student objects in TreeSet , sorted by age in the stored procedure.

3. Exercise: Store custom Objects (students) in the TreeSet collection and sort them by age

Summary:

Custom objects need to override the CompareTo () method in the comparable interface: Because TreeSet is ordered, when an element is added, the new element is compared to an existing element in the TreeSet, and the comparable () in the CompareTo interface is called. Method. Custom objects need to define their own comparison principles, so override the CompareTo () method in the comparable interface.

Third Speaking two fork Tree

1. Overview:

The underlying data structure of TreeSet is a two-fork tree, which allows you to sort the elements in the set collection. Its guarantee element uniqueness is based on the following: the CompareTo () method, if return 0, the element is duplicated, the element is not added to the collection.

2, TreeSet sort

The first sort: to make the elements themselves comparable, so the elements need to implement the Comarable interface, overriding the CompareTo method.

Fourth Lecture Implementing Comparator mode ordering

1, Overview: TreeSet the second sort of way

When the element itself is not comparable, or the comparison is not required, then it is necessary to make the set itself a comparative--let the collection at the time of initialization, there is a comparison method

2. The second sort method realizes the way of thinking

Defines a class that implements the comparator interface and overrides the Compare method in the interface. Take the second middle school students as an example: the student elements themselves with the method of sorting by age, but our collection needs to be sorted by name, if you want to modify the code in the student is more troublesome, we can define a class to encapsulate the method of sorting by name, and provide the class object to treeset use.

3, the implementation code is as follows:

Exercise: Sort by string length

idea: string This province is comparable, but its comparison method is not required, then you can only use the comparator.

The implementation code is as follows:

Summary: Any object can be sorted according to the actual requirements, custom sequencer and the Sequencer object to the collection use, this method is more convenient. When you compare strings, you can call the method CompareTo in the string class to do a natural string ordering.

Sixth lecture Generics Overview

1. Definition

Generics: New features that occur after the JDK1.5 version. is a type-safe mechanism for resolving type-safety issues.

2. Benefits

1) The problem of the running period ClassCastException, transferred to the compilation period, convenient for programmers to solve problems, so that run-time problems and more secure.

2) Avoid the trouble of forcing type conversion when reading elements;

3. Code implementation

Seventh Lecture generics use

1. Application of generics in comparators

generic format: defines the reference data type to manipulate by <>.

When using generics: generics are common when used in a collection framework, as long as you see <> define generics (interfaces, classes, methods, and so on in the API documentation are identified by <>, you need to define generics when using them).

<> marker: used to receive a type, when the collection is used, the data type to be stored in the collection is passed as a parameter to <>.

2. Code implementation

Eighth lecture Generic class

1. Definition

When defining a generic class: When the reference data type to be manipulated in a class is undefined, an earlier approach is to define an object to complete the extension. After you have generics: You can define a generic class to complete the extension.

2. Code implementation

Nineth Lecture Generic methods

1. Overview

To allow different methods to manipulate different types, and the type is undefined, you can define a generic on a method. A generic method is a generic approach.

2. code example

3. Special functions of generic function

Static methods do not have access to generics defined on the class. If the application data type for a static method operation is not deterministic, you can define the generic on the method.

Tenth lecture Generic interface

A class that implements a generic interface and does not determine the type of data to be processed, this class can be defined as generic.

11th Lecture Generic qualification

1. Definition

Qualified generic types that can be received are both "up-qualified" and "down-qualified".

1)? Extends e: Can receive E type or this e word type, upper limit;

2)? Super E: Can receive the Type E or the parent type of E, the next qualification;

2. Generic Limited application

“? "--a wildcard, or placeholder.

The following code: Defines a function using the generic upper bound (person and its subclasses) and calls the function with a subclass (Student) object.

Description: Both student and worker are subclasses of person. The comparer is defined with the person class, and TreeSet, which stores the student and worker types, can call the comparer, which reflects the characteristics of the lower bound of the generic.

Summary of Knowledge points

Summary of Knowledge points

1. When you define a comparer for a TreeSet collection, you need to implement the compare (Object o1,object O2) method in the comparator interface, and to make the reverse store, you only need to compare the O1 and O2 parameters in the code to redeem the location.

2. Two sorting methods of elements in the TreeSet collection:

1) defined inside the element

The CompareTo method in the real comparable interface.

2) Custom Comparator

Implement the Compare method in the comparator interface and pass the comparer object to the collection.

Note: the "Custom comparer" takes precedence when both are present.

3. When overriding the Equals method, you cannot assign generics, so you need to force type conversions before you can use the Equals method.

4, the role of the generic class: To avoid the strong turn, and make the type conversion error occurs in the compilation period, easy to modify.

5, the generic method can improve the extensibility of the program function, make the function more general.

6, custom generic class: Can only be a custom data type, not a basic data type.

7. Static methods cannot access generics defined on a class because:

Dark Horse Programmer-java Basics-Set frame-treeset, binary tree, generics

Related Article

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.