Java programming thoughts 7-Basics of generic programming

Source: Internet
Author: User

General classes and methods are for specific data types. When writing a class and method suitable for multiple data types, generic programming is required, java generic programming is similar to a template in C ++, that is, a parameterized programming method. Specifically, it abstracts information related to data types, it mainly provides general implementation and logic, and the information related to the data type is determined by the time-in-use parameters.

1. generic classes/interfaces:

(1). generic interface:

If an interface is provided to generate a specified class:

public interface Gernerator<T>{T next() ;}public class A implement Generator<A>{A next(){return new A();}}

(2). generic classes:

A stack data structure implemented using generics is as follows:

Public class internal liststack <t> {// internal class Private Static class node of the node <u> {u item; node <u> next; node () {item = NULL; next = NULL;} node (U item, node <u> next) {This. item = item; this. next = next;} Boolean end () {return item = NULL & next = NULL;} private node <t> Top = new node <t> (); public void push <t> (T item) {Top = new node <t> (item, top);} public t POP () {T result = top. item; If (! Top. End () {Top = top. Next () ;}return result ;}}

Using this generic stack, you can operate on various data types.

2. Generic method:

For example:

public class GenericMethods{public <T> void f(T x){System.out.println(x.getClass().getName()) ;}public static void main(String[] args){GenericMethods gm = new GenericMethods();gm.f(“”);gm.f(1);gm.f(1.0);……} }

Output result:

Java. Lang. String

Java. Lang. Integer

Java. Lang. Double

3. Generic set:

Java generic collections are widely used. Before Java 5, Java did not introduce a generic mechanism. When using Java Collection containers, the following two problems are often encountered:

A. java containers store objects of the object type by default. If a container contains objects of the type and B type, if you confuse object A and object B, conversion errors may occur, and type conversion exceptions may occur.

B. If you do not know the Data Type of elements in the collection container, a type conversion exception may also occur.

In view of the above problems, Java 5 introduces a generic mechanism to explicitly specify the Data Type of its elements when defining a collection container object. When using a collection container, the compiler checks whether the data type matches the data type specified by the container. If the data type does not match the data type, the compiler enforces the data type security.

(1). Common Java Collection container generic usage:

For example:

public class New{public static <K, V> Map<K, V> map(){return new HashMap<K, V>();}public static <T> List<T> list(){return new ArrayList<T>() ;}public static <T> LinkedList<T> lList(){return new LinkedList<T>();}public static <T> Set<T> set(){return new HashSet<T>();}public static <T> Queue<T> queue(){return new LinkedList<T>() ;};public static void main(String[] args){Map<String, List<String>> sls = New.map();List<String> ls = New.list();LinkedList<String> lls = New.lList();Set<String> ss = New.set();Queue<String> qs = New.queue();}}

(2). The set in Java is a set of logical meanings in mathematics. Using Generics can easily perform mathematical operations on any type of set. The Code is as follows:

Public class sets {// Union Public static <t> set <t> Union (set <t> A, set <t> B) {set <t> result = new hashset <t> (a); result. addall (B); return result;} // intersection public static <t> set <t> intersection (set <t> A, set <t> B) {set <t> result = new hashset <t> (a); result. retainall (B); return result;} // difference set public static <t> set <t> difference (set <t> A, set <t> B) {set <t> result = new hashset <t> (a); result. removeall (B); return result;} // set public static <t> set <t> complement (set <t> A, set <t> B) {Return Difference (Union (a, B), intersection (a, B ));}}

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.