泛型集合(经典)

                                           泛型集合(经典)

1, 使用迭代器Iterator的方式。

2, 使用增强for循环的方式。

3, 如果有下标,则可以使用下标的方式。


(1)遍历数组

  1. String [] arr=new String[] {"aa","bb","cc"};  
  2.         //1.增强的for循环  
  3.         for(String elt:arr){  
  4.             System.out.println(elt);  
  5.         }  
  6.         //2.下标的方式  
  7.         for (int i = 0; i < arr.length; i++) {  
  8.             System.out.println(arr[i]);  
  9.         }  


(2)遍历List集合

  1. ArrayList<String> list=new ArrayList<String>();  
  2.         list.add("haha");  
  3.         list.add("hehe");  
  4.         list.add("xixi");  
  5.         //1.增强的for循环  
  6.         for(String elt:list){  
  7.             System.out.println(elt);  
  8.         }  
  9.         //2.下标的方式  
  10.         for (int i = 0; i < list.size(); i++) {  
  11.             System.out.println(list.get(i));  
  12.         }  
  13.         //3.迭代器的方式  
  14.         Iterator<String> it=list.iterator();  
  15.         while (it.hasNext()) {  
  16.             System.out.println(it.next());  
  17.               
  18.         }  



(3)遍历Set集合

  1. HashSet<String> set=new HashSet<String>();  
  2.         set.add("zhangsan");  
  3.         set.add("lisi");  
  4.         set.add("wangqu");  
  5.         //1.增强的for循环  
  6.         for(String elt:set){  
  7.             System.out.println(elt);  
  8.         }  
  9.         //2.迭代器  
  10.         Iterator<String> it=set.iterator();  
  11.         while (it.hasNext()) {  
  12.             System.out.println(it.next());  
  13.               
  14.         }  



(4)遍历Map集合

    1. HashMap<Integer , String> map=new HashMap<Integer, String>();  
    2.         map.put(1, "alice");  
    3.         map.put(2, "bob");  
    4.         map.put(3, "lily");  
    5.         //1.增强的for循环(entry集合)  
    6.         for(Entry<Integer, String> entry:map.entrySet()){  
    7.             System.out.println(entry);  
    8.         }  
    9.         //2.增强的for循环(key集合)  
    10.         for(Integer key:map.keySet()){  
    11.             System.out.println(key+" = "+map.get(key));  
    12.         }  
    13.         //3.遍历值的集合  
    14.         for(String value:map.values()){  
    15.             System.out.println(value);  
    16.         } 

    

泛型 的好处:
1. 把运行时出现 的问题提前至了编译时。
2. 避免了无谓的强制类型转换。

注意: 在泛型中没有多态的概念,两边的数据必须要一致。 或者是只写一边 的泛型类型。


推荐使用: 两边的数据类型都写上一致的。

泛型:

  1. import java.util.ArrayList;  
  2.   
  3. public class demo1 {  
  4.     public static void main(String[] args) {  
  5.         ArrayList<String> list=new ArrayList<String>();  
  6.         list.add("aa");  
  7.         list.add("bb");  
  8.         //list.add(12);  
  9.         for (int i = 0; i < list.size(); i++) {  
  10.             String str=list.get(i);  
  11.             System.out.println(str.toUpperCase());  
  12.         }  
  13.     }  
  14. }  


泛型方法:

  当函数中使用了一个不明确的数据类型,那么在函数上就可以进行泛型的定义。

        public <泛型的声明> 返回值类型 函数名( 泛型 变量名 ){

        

        

         }

注意:
1. 在方法上的自定义泛型的具体数据类型是调用该方法的时候传入实参的时候确定的。
2. 自定义泛型使用的标识符只要符合标识符的命名规则即可。

  1. public class demo2 {  
  2.     public static void main(String[] args) {  
  3.         Integer i=print(12);  
  4.         String str=print("abc");  
  5.           
  6.   
  7.     }  
  8.     public static <T> T print(T o){  
  9.         return o;  
  10.     }  
  11. }  


泛型类:

泛型类的定义格式:

class 类名<声明自定义的泛型>{




注意的事项: 
1. 在类上自定义的泛型的具体数据类型是在创建对象的时候指定的。
2. 在类上自定义了泛型,如果创建该类的对象时没有指定泛型的具体类型,那么默认是Object类型。

  1. //自定义泛型类  
  2. class Father<T>{  
  3.     private T t;  
  4.     public Father() {  
  5.         // TODO Auto-generated constructor stub  
  6.     }  
  7.     public Father(T t){  
  8.         super();  
  9.         this.t=t;  
  10.     }  
  11.     public void setT(T t) {  
  12.         this.t = t;  
  13.     }  
  14.     public T getT() {  
  15.         return t;  
  16.     }  
  17. }  
  18. //泛型类的继承方式1:子类也需要使用泛型  
  19. class Son1<T> extends Father<T>{  
  20.       
  21. }  
  22. //泛型类的继承方式2:子类指定了具体的类型  
  23. class Son2 extends Father<String>{  
  24.       
  25. }  
  26. //错误写法,父类上定义有泛型需要进行处理  
  27. /*class Son3 extends Father<T>{ 
  28.      
  29. }*/  
  30.   
  31. public class demo3 {  
  32.     public static void main(String[] args) {  
  33.           
  34.         //在类上自定义的泛型的具体数据类型是在创建对象的时候指定的。  
  35.         Father<String> f1=new Father<String>("jack");  
  36.         System.out.println(f1.getT());  
  37.         Father<Integer> f2=new Father<Integer>(20);  
  38.         System.out.println(f2.getT());  
  39.           
  40.           
  41.     }  
  42. }  


泛型接口:


泛型接口的定义格式:

interface 接口名<声明自定义的泛型>{

}


在接口上自定义泛型要注意的事项:
1. 在接口上自定义泛型的具体数据类型是在实现该接口的时候指定的。
2. 如果一个接口自定义了泛型,在实现该接口的时候没有指定具体的数据类型,那么默认是Object数据类型。 

  1. interface Inter<T>{  
  2.     void print(T t);  
  3. }  
  4. //实现不知为何类型时可以这样定义  
  5. class myInter<T> implements Inter<T>{  
  6.     @Override  
  7.     public void print(T t) {  
  8.         // TODO Auto-generated method stub  
  9.         System.out.println("myprint:"+t);  
  10.     }  
  11. }  
  12. //使用接口时明确具体类型。  
  13. class myInter2 implements Inter<String>{  
  14.     @Override  
  15.     public void print(String t) {  
  16.         // TODO Auto-generated method stub  
  17.         System.out.println("myprint2:"+t);  
  18.     }  
  19. }  
  20. public class demo4 {  
  21.     public static void main(String[] args) {  
  22.         myInter<String> str=new myInter<String>();  
  23.         str.print("泛型");  
  24.         myInter2 str2=new myInter2();  
  25.         str2.print("只能传字符串");  
  26.     }  
  27. }  



一、List遍历

Java中List遍历有三种方法来遍历泛型,主要为:

1.for循环遍历

2.iterator遍历

3.foreach遍历

  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6.   
  7. import org.junit.Test;  
  8.   
  9. public class test {  
  10.   
  11.     //第一种方法:for循环遍历  
  12.     @Test  
  13.     public void test1() {  
  14.         List<String> li = new ArrayList<String>();  
  15.   
  16.         li.add("agad");  
  17.         li.add("1234");  
  18.         li.add("good");  
  19.   
  20.         for (int i = 0; i < li.size(); i++) {  
  21.             String s = li.get(i);  
  22.             System.out.println(s);  
  23.         }  
  24.         System.out.println("-------------------");  
  25.     }  
  26.   
  27.     //第二种方法:iterator遍历  
  28.     @Test  
  29.     public void test2() {  
  30.         List<String> li = new ArrayList<String>();  
  31.   
  32.         li.add("agad");  
  33.         li.add("1234");  
  34.         li.add("good");  
  35.   
  36.         Iterator iterator = li.iterator();  
  37.         while (iterator.hasNext()) {  
  38.             String s = (String) iterator.next();  
  39.             System.out.println(s);  
  40.         }  
  41.         System.out.println("-------------------");  
  42.     }  
  43.   
  44.     //第三种方法:foreach方法遍历  
  45.     @Test  
  46.     public void test3() {  
  47.         List<String> li = new ArrayList<String>();  
  48.   
  49.         li.add("agad");  
  50.         li.add("1234");  
  51.         li.add("good");  
  52.   
  53.         foreach (String s : li) {  
  54.             System.out.println(s);  
  55.         }  
  56.   
  57.         System.out.println("-------------------");  
  58.     }  
  59. }  

二、Map遍历

Map遍历只要有两种方法: 

1.通过Map的KeySet进行遍历

2.通过Map的EntrySet进行遍历

    1. // Map的遍历方法一:通过map的KeySet进行遍历  
    2.     @Test  
    3.     public void test4() {  
    4.         Map<Integer, String> map = new HashMap<Integer, String>();  
    5.         map.put(1, "good");  
    6.         map.put(2, "morning");  
    7.   
    8.         Set<Integer> set = map.keySet();  
    9.         for (Integer ky : set) {  
    10.             System.out.println(ky + ":" + map.get(ky));  
    11.         }  
    12.   
    13.         System.out.println("-------------------");  
    14.     }  
    15.   
    16.     // Map的遍历方法二:通过map的entrySet进行遍历  
    17.     @Test  
    18.     public void test5() {  
    19.         Map<Integer, String> map = new HashMap<Integer, String>();  
    20.         map.put(1, "good");  
    21.         map.put(2, "morning");  
    22.   
    23.         Set<Map.Entry<Integer, String>> set = map.entrySet();  
    24.         for (Entry<Integer, String> entry : set) {  
    25.             System.out.println(entry.getKey() + ":" + entry.getValue());  
    26.         }  
    27.   
    28.         System.out.println("-------------------");  
    29.     } 

单例设计模式分为懒人模式和饿人模式,如果一个枚举中只有一个用例,它就相当于一个单例设计模式

  1. // 饿汉  

  2. class B {  

  3.     // 1、私有构造器  

  4.     private B() {  

  5.     }  

  6.     // 2、private static 对象成员  

  7.     private static B b = new B();  

  8.     // 3、提供public static 获取成员方法,获取唯一实例  

  9.     public static B getInstance() {  

  10.         return b;  

  11.     }  

  12. }  

  13.   

  14. //懒汉  

  15. class C {  

  16.     // 1、私有构造器  

  17.     private C() {  

  18.     }  

  19.     // 2、private static 对象成员  

  20.     private static C c;  

  21.     // 3、提供public static 获取成员方法,获取唯一实例  

  22.     public static C getInstance() {  

  23.         if(c == null){  

  24.             c = new C();  //懒汉式  

  25.         }  

  26.         return c;  

  27.     }  

  28. }  

原文地址:https://www.cnblogs.com/fqwsndc1314-5207788/p/7431654.html