forked from tianshb/JavaLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericTest.java
More file actions
55 lines (46 loc) · 1.98 KB
/
Copy pathGenericTest.java
File metadata and controls
55 lines (46 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.crow;
/**
* Created by CrowHawk on 17/2/16.
*/
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class GenericTest {
public static void main(String[] args) throws Exception{
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("Tom", 1);
hashMap.put("Jerry", 2);
hashMap.put("Nick", 3);
printHashmap(hashMap);
String[] array = new String[]{"crow","martin","pattie"};
swap(array, 1, 2);
System.out.println("array[2] = " + array[2]);
//用反射的方法获取printHashmap方法的参数并打印
Method printMethod = GenericTest.class.getMethod("printHashmap", HashMap.class);
Type[] types = printMethod.getGenericParameterTypes();
ParameterizedType parameterizedType = (ParameterizedType)types[0];
Type[] actualTypes = parameterizedType.getActualTypeArguments();
System.out.println("The paratype is " + parameterizedType.getRawType() + "<" + actualTypes[0] + "," + actualTypes[1] + ">");
}
public static void printHashmap(HashMap<String, Integer> hashMap){//遍历HashMap并打印其内容
Set<Map.Entry<String, Integer>> entrySet = hashMap.entrySet();
for(Map.Entry<String, Integer> entry: entrySet){
System.out.println(entry.getKey() + "," + entry.getValue());
}
Iterator<Map.Entry<String, Integer>> iter = entrySet.iterator();
while(iter.hasNext()){
Map.Entry<String, Integer> entry = iter.next();
System.out.print("Key is " + entry.getKey() + ",");
System.out.println("Value is " + entry.getValue());
}
}
public static <T> void swap(T[] arr, int i, int j){//交换泛型数组中的任意两个元素
T tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}