1. Generic class
1.1 Ordinary generic
package test.lujianing; /** * 泛型类 * @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; } } /** * 测试泛型类 */ public class TestOne { public static void main(String[] args) { //测试Integer泛型 Test<Integer> t1 = new Test<Integer>(); t1.setValue(5); Integer i = t1.getValue(); System.out.println(i); //测试Double泛型 Test<Double> t2 = new Test<Double>(); t2.setValue(5.55D); Double d = t2.getValue(); System.out.println(d); //测试String泛型 Test<String> t3 = new Test<String>(); t3.setValue("hello world"); String str =t3.getValue(); System.out.println(str); } }
Output result:
java.lang.Integer 5 java.lang.Double 5.55 java.lang.String hello world
1.2 K/V generic
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) { 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 result :
jianing 24
2. Generic interface
package test.lujianing; /** * 泛型接口 * @param <T> */ public interface TestImpl<T> { public void setValue(T t); public T getValue(); }
Output result:
1 hello word
3. Generic method
package test.lujianing; /** * 泛型方法类 */ class TestMethod{ /** * 泛型方法 */ 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; } } /** * 泛型方法测试类 */ 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 result:
java.lang.Integer 30 java.lang.Double 40.0022
4. Restrict generics
In the above example, there is no restriction on the scope of class Test
5. Wildcard generics
package test.lujianing; import java.util.HashMap; import java.util.Map; /** * 通配泛型 */ 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:
key:Double value:class java.lang.Double key:Integer value:class java.lang.Integer
Simple example: example for 1.1
public static void main(String[] args) { //测试Integer泛型 Test<Integer> t1 = new Test<Integer>(); t1.setValue(5); fun(t1); //测试Double泛型 Test<Double> t2 = new Test<Double>(); t2.setValue(5.55D); fun(t2); } public static void fun(Test<?> t){ System.out.println("通配泛型"+t.getValue()); }
Output results:
java.lang.Integer 通配泛型5 java.lang.Double 通配泛型5.55
6. Supplement
In generics, you may encounter
For more java generic examples and related articles, please pay attention to the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment