泛型集合与工具类

泛型概述

  • Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递
  • 常见形式有泛型类、泛型接口、泛型方法
  • 语法:
    • <T,…> T成为类型占位符,表示一种引用类型

泛型类

  • package com.gather.MyGeneric;
    
    /**
     * 泛型类
     * 语法:类名<T>
     * T 是类型占位符,表示一种引用类型 ,如果编写多个使用逗号隔开
     */
    public class MyGeneric<T> {
          
          
    //    使用泛型T
    //    创建变量
        T t;
    //    作为方法的参数
        public void show(T t){
          
          
            System.out.println(t);
    
        }
    //    泛型作为方法的返回值
        public T getT(){
          
          
            return t;
        }
    
    }
    
  • package com.gather.MyGeneric;
    
    public class TestGeneric {
          
          
        public static void main(String[] args) {
          
          
    //        使用泛型类创建对象
    //        注意:1.泛型只能是引用类型
    //               2.不同泛型类型对象之间不能互相复制
            MyGeneric<String> myGeneric = new MyGeneric<String>();
            myGeneric.t = "hello";
            myGeneric.show("大家好");
            String string = myGeneric.getT();
    
            MyGeneric<Integer> myGeneric2 = new MyGeneric<Integer>();
            myGeneric2.t = 100;
            myGeneric2.show(200);
            Integer integer = myGeneric2.getT();
    
        }
    }
    

泛型接口

  • package com.gather.MyGeneric;
    /*
    泛型接口
    语法: 接口名<T>;
    注意:不能泛型静态常量
     */
    
    public interface MyInterface <T> {
          
          
        String name = "张三";
    
        T server(T t);
    }
    
  • package com.gather.MyGeneric;
    
    public class MyInterfaceImpl implements MyInterface<String> {
          
          
        @Override
        public String server(String t) {
          
          
            System.out.println(t);
            return t;
    
        }
    }
    
  • package com.gather.MyGeneric;
    
    public class MyInterfaceImpl2<T> implements MyInterface<T> {
          
          
        @Override
        public T server(T t) {
          
          
            System.out.println(t);
            return t;
        }
    }
    
  • MyInterfaceImpl impl = new MyInterfaceImpl();
    impl.server("xxxxxxxxxxxx");
    
    MyInterfaceImpl2<Integer> impl2 = new MyInterfaceImpl2<>();
    impl2.server(1000);
    

泛型方法

  • package com.gather.MyGeneric;
    /**
     * 泛型方法
     * 语法:<T> 返回值类型
     */
    
    public class MyGenericMethod {
          
          
    //    泛型方法
        public <T> T show(T t){
          
          
            System.out.println("泛型方法"+t);
            return t;
        }
        
    }
    
  • //        泛型方法
            MyGenericMethod myGenericMethod = new MyGenericMethod();
            myGenericMethod.show("张世杰");
            myGenericMethod.show(200);
            myGenericMethod.show(3.1415926);
    

泛型好处

  • 好处:
    1. 提高代码的重用性
    2. 防止类型转化异常,提高代码的安全性

泛型集合

  • 概念:参数化类型、类型安全的集合,强制集合元素的类型必须一致

  • 特点:

    • 编译时即可检查,而非运行时抛出异常
    • 访问时,不必类型转换(拆箱)
    • 不同泛型之间引用不能相互赋值,泛型不存在多态
  • package com.gather.MyGeneric;
    
    import com.kind.chapter_1.Student;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class Demo {
          
          
        public static void main(String[] args) {
          
          
            ArrayList<String> arrayList = new ArrayList<String>();
            arrayList.add("xxx");
            arrayList.add("yyy");
    //        arrayList.add(10);
    //        arrayList.add(20);
    
            for (String string : arrayList) {
          
          
                System.out.println(string);
    
            }
    
            ArrayList<Student> arrayList2 = new ArrayList<Student>();
            Student s1 = new Student("刘德华", 20);
            Student s2 = new Student("成龙", 20);
            Student s3 = new Student("吴彦祖", 20);
            arrayList2.add(s1);
            arrayList2.add(s2);
            arrayList2.add(s3);
    
            Iterator<Student> it = arrayList2.iterator();
            while(it.hasNext()){
          
          
                Student s = it.next();
                System.out.println(s.toString());
            }
        }
    

猜你喜欢

转载自blog.csdn.net/xiaoxiaobaie/article/details/121594060