Multiple Java generic sample sharing _java

Source: Internet
Author: User

1. Generic type

1.1 Generic type

Copy Code code as follows:

Package test.lujianing;
/**
* Generic class
* @param <T>
*/
Class test<t>{
Private T obj;
public void SetValue (T obj) {
This.obj =obj;
}
Public T GetValue () {
System.out.println (Obj.getclass (). GetName ());
return obj;
}
}
/**
* Test generic class
*/
public class Testone {
public static void Main (string[] args) {
Test integer generics
test<integer> T1 = new test<integer> ();
T1.setvalue (5);
Integer i = T1.getvalue ();
System.out.println (i);
Test double generics
test<double> t2 = new test<double> ();
T2.setvalue (5.55D);
Double d = t2.getvalue ();
System.out.println (d);
Test string generics
test<string> t3 = new test<string> ();
T3.setvalue ("Hello World");
String str =t3.getvalue ();
System.out.println (str);
}
}

Output results:

Copy Code code as follows:

Java.lang.Integer
5
Java.lang.Double
5.55
Java.lang.String
Hello World

1.2-k/v Generics

Copy Code code as follows:

Package test.lujianing;
Import Java.util.HashMap;
Import Java.util.Map;

/**
 * Created by Administrator on 14-3-30.
 */
Class testkv<k,v>{
    private map<k,v> map=new hashmap<k, v> ();
    public void put (k K, v V) {
        map.put (k,v);
   }
    public V get (k k) {
        return Map.get (k);
   }
}
public class testfour{
    public static void Main (string[] args) {
   & nbsp;    testkv<string,string> t = new testkv<string, string> ();
        t.put ("name", "jianing");
        System.out.println (t.get ("name"));

testkv<string,integer> t2 = new testkv<string, integer> ();
T2.put ("Age", 24);
System.out.println (T2.get ("Age"));
}
}

Output results:

Copy Code code as follows:

Jianing
24

2. Generic interface

Copy Code code as follows:

Package test.lujianing;
/**
* Generic interface
* @param <T>
*/
Public interface Testimpl<t> {
public void SetValue (T-t);
Public T getValue ();
}

Output results:

Copy Code code as follows:

1
Hello Word

3. Generic method

Copy Code code as follows:

Package test.lujianing;

/**
* Generic Method class
*/
Class testmethod{
/**
* Generic method
*/
Public <t>t getValue (Object s,class<t> clazz) {
System.out.println (Clazz.getname ());
T T =null;
if (Clazz.getname (). Equals ("Java.lang.Integer")) {
Double d = double.parsedouble (S.tostring ());
int i =d.intvalue ();
T= (T) New Integer (i);
}
if (Clazz.getname (). Equals ("Java.lang.Double")) {
T= (T) New Double (S.tostring ());
}
return t;
}
}

/**
* Generic method Test class
*/
public class Testthree {
public static void Main (string[] args) {
TestMethod t = new TestMethod ();
int i =t.getvalue ("30.0011", Integer.class);
System.out.println (i);
Double D =t.getvalue ("40.0022", Double.class);
System.out.println (d);
}
}

Output results:

Copy Code code as follows:

Java.lang.Integer
30
Java.lang.Double
40.0022

4. Restricting generics

In the above example, the scope of class test<t> type holder T is not restricted, and the default qualified type is equivalent to object. For example, we want to limit T to the digital interface type. Just do this: class Test<t extends Number>, such that the generic T in class can only be the implementation class of number interface, the incoming non-number interface compilation will be wrong.

5. Generic type

Copy Code code as follows:

Package test.lujianing;
Import Java.util.HashMap;
Import Java.util.Map;
/**
* Wildcard generics
*/
public class Testfive {
public static void Main (string[] args) {
map<string,class<? Extends number>> map = new Hashmap<string,class<? Extends Number>> ();
Map.put ("Integer", Integer.class);
Map.put ("Double", Double.class);

for (map.entry<string,class<? extends Number>> Entry:map.entrySet ()) {
System.out.println ("key:" + entry.getkey () + "value:" + entry.getvalue ());
}
}
}

Output results:

Copy Code code as follows:

Key:double Value:class java.lang.Double
Key:integer Value:class Java.lang.Integer

Simple example: an example for 1.1

Copy Code code as follows:

public static void Main (string[] args) {
Test integer generics
test<integer> T1 = new test<integer> ();
T1.setvalue (5);
Fun (t1);
Test double generics
test<double> t2 = new test<double> ();
T2.setvalue (5.55D);
Fun (T2);
}
public static void Fun (Test<?> t) {
SYSTEM.OUT.PRINTLN ("Wildcard generic" +t.getvalue ());
}

Output results:

Copy Code code as follows:

Java.lang.Integer
Pass-and-match generics 5
Java.lang.Double
Pass-and-match generics 5.55

6. Supplementing

In generics, you may encounter <T><E>, and T and E are the first letters of type and element respectively. E is typically used to represent the types of elements in a collection type, such as the definition of the List interface, public interface list<e> extends collection<e>. This is just a naming convention, there is no essential difference between the two.

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.