- HashMap<Integer , String> map=new HashMap<Integer, String>();
- map.put(1, "alice");
- map.put(2, "bob");
- map.put(3, "lily");
-
- for(Entry<Integer, String> entry:map.entrySet()){
- System.out.println(entry);
- }
-
- for(Integer key:map.keySet()){
- System.out.println(key+" = "+map.get(key));
- }
-
- for(String value:map.values()){
- System.out.println(value);
- }
泛型 的好處:
1. 把運行時出現 的問題提早至了編譯時。
2. 避免了無謂的強制類型轉換。
注意: 在泛型中沒有多態的概念,兩邊的數據必需要一致。 或者是隻寫一邊 的泛型類型。
.net
推薦使用: 兩邊的數據類型都寫上一致的。設計
泛型:對象
- import java.util.ArrayList;
-
- public class demo1 {
- public static void main(String[] args) {
- ArrayList<String> list=new ArrayList<String>();
- list.add("aa");
- list.add("bb");
-
- for (int i = 0; i < list.size(); i++) {
- String str=list.get(i);
- System.out.println(str.toUpperCase());
- }
- }
- }
泛型方法:
當函數中使用了一個不明確的數據類型,那麼在函數上就能夠進行泛型的定義。
public <泛型的聲明> 返回值類型 函數名( 泛型 變量名 ){
}
注意:
1. 在方法上的自定義泛型的具體數據類型是調用該方法的時候傳入實參的時候肯定的。
2. 自定義泛型使用的標識符只要符合標識符的命名規則便可。
- public class demo2 {
- public static void main(String[] args) {
- Integer i=print(12);
- String str=print("abc");
-
-
- }
- public static <T> T print(T o){
- return o;
- }
- }
泛型類:
泛型類的定義格式:
class 類名<聲明自定義的泛型>{
}
注意的事項:
1. 在類上自定義的泛型的具體數據類型是在建立對象的時候指定的。
2. 在類上自定義了泛型,若是建立該類的對象時沒有指定泛型的具體類型,那麼默認是Object類型。
- class Father<T>{
- private T t;
- public Father() {
-
- }
- public Father(T t){
- super();
- this.t=t;
- }
- public void setT(T t) {
- this.t = t;
- }
- public T getT() {
- return t;
- }
- }
- class Son1<T> extends Father<T>{
-
- }
- class Son2 extends Father<String>{
-
- }
-
- public class demo3 {
- public static void main(String[] args) {
-
-
- Father<String> f1=new Father<String>("jack");
- System.out.println(f1.getT());
- Father<Integer> f2=new Father<Integer>(20);
- System.out.println(f2.getT());
-
-
- }
- }
泛型接口:
泛型接口的定義格式:
interface 接口名<聲明自定義的泛型>{
}
在接口上自定義泛型要注意的事項:
1. 在接口上自定義泛型的具體數據類型是在實現該接口的時候指定的。
2. 若是一個接口自定義了泛型,在實現該接口的時候沒有指定具體的數據類型,那麼默認是Object數據類型。
- interface Inter<T>{
- void print(T t);
- }
- class myInter<T> implements Inter<T>{
- @Override
- public void print(T t) {
-
- System.out.println("myprint:"+t);
- }
- }
- class myInter2 implements Inter<String>{
- @Override
- public void print(String t) {
-
- System.out.println("myprint2:"+t);
- }
- }
- public class demo4 {
- public static void main(String[] args) {
- myInter<String> str=new myInter<String>();
- str.print("泛型");
- myInter2 str2=new myInter2();
- str2.print("只能傳字符串");
- }
- }
1、List遍歷
Java中List遍歷有三種方法來遍歷泛型,主要爲:
1.for循環遍歷
2.iterator遍歷
3.foreach遍歷
- package com.gmail.lsgjzhuwei;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- import org.junit.Test;
-
- public class test {
-
-
- @Test
- public void test1() {
- List<String> li = new ArrayList<String>();
-
- li.add("agad");
- li.add("1234");
- li.add("good");
-
- for (int i = 0; i < li.size(); i++) {
- String s = li.get(i);
- System.out.println(s);
- }
- System.out.println("-------------------");
- }
-
-
- @Test
- public void test2() {
- List<String> li = new ArrayList<String>();
-
- li.add("agad");
- li.add("1234");
- li.add("good");
-
- Iterator iterator = li.iterator();
- while (iterator.hasNext()) {
- String s = (String) iterator.next();
- System.out.println(s);
- }
- System.out.println("-------------------");
- }
-
-
- @Test
- public void test3() {
- List<String> li = new ArrayList<String>();
-
- li.add("agad");
- li.add("1234");
- li.add("good");
-
- foreach (String s : li) {
- System.out.println(s);
- }
-
- System.out.println("-------------------");
- }
- }
2、Map遍歷
Map遍歷只要有兩種方法:
1.經過Map的KeySet進行遍歷
2.經過Map的EntrySet進行遍歷
- @Test
- public void test4() {
- Map<Integer, String> map = new HashMap<Integer, String>();
- map.put(1, "good");
- map.put(2, "morning");
-
- Set<Integer> set = map.keySet();
- for (Integer ky : set) {
- System.out.println(ky + ":" + map.get(ky));
- }
-
- System.out.println("-------------------");
- }
-
-
- @Test
- public void test5() {
- Map<Integer, String> map = new HashMap<Integer, String>();
- map.put(1, "good");
- map.put(2, "morning");
-
- Set<Map.Entry<Integer, String>> set = map.entrySet();
- for (Entry<Integer, String> entry : set) {
- System.out.println(entry.getKey() + ":" + entry.getValue());
- }
-
- System.out.println("-------------------");
- }
單例設計模式分爲懶人模式和餓人模式,若是一個枚舉中只有一個用例,它就至關於一個單例設計模式
-
-
class B {
-
-
private B() {
-
}
-
-
private static B b = new B();
-
-
public static B getInstance() {
-
return b;
-
}
-
}
-
-
-
class C {
-
-
private C() {
-
}
-
-
private static C c;
-
-
public static C getInstance() {
-
if(c == null){
-
c = new C();
-
}
-
return c;
-
}
-
}