Home  >  Article  >  Java  >  Simple example of Java generics

Simple example of Java generics

高洛峰
高洛峰Original
2017-01-18 11:15:521114browse

package com.chase.test;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
public class testT {
    public static <T> void main(String[] args) {
        testT classT = new testT();
        List<T> find = classT.find(0, 10);
        if (find != null && find.size()>0) {
            for (T integer : find) {
                System.out.println(integer);
            }
        }
//        showList();
    }

    public static <T> void showList() {
        testT classT = new testT();
        List<T> find = classT.find(0, 10);
        for (T t : find) {
            System.out.println(t);
        }
    }

    public <T> List<T> find(int begin, int end) {
        List<T> list = new ArrayList<T>();
        list.add((T)new Integer(222));
        list.add((T)"111");
        list.add((T)"昨天是重阳节!");
        return list;
    }
}
 
class TestGen0<K,V>{ 
      public Hashtable<K,V> h=new Hashtable<K,V>(); 
      public void put(K k, V v) { 
       h.put(k,v); 
      } 
      public V get(K k) { 
       return h.get(k); 
      } 
      public static void main(String args[]){ 
       TestGen0<String,String> t=new TestGen0<String,String>(); 
       t.put("key", "value"); 
       String s=t.get("key"); 
       System.out.println(s); 
      } 
    }

testT output:

222
111
Yesterday was the Double Ninth Festival!

TestGen0 output:
value

For more articles related to simple examples of Java generics, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn