小人菜的一B
近日寫了個list轉map的小的不能再小的小方法……
雖然沒有派上用處,但是我感覺也挺不錯的,自己的孩子再無用再磕磣只要不危害社會都是寶啊!!哇哈哈!!
package test;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 工具類-練習
* @author lemon
* @since 20130925
*/
public class Util {
public static void main(String args[]) throws SecurityException, NoSuchFieldException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
/**原諒我下面幼稚的置值,恩恩,new一個list來作爲測試,裏面放着Person*/
List<Person> list = new ArrayList<Person>();
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
p1.setName("lanchenghao");
p1.setAge("12");
p1.setSex("男");
p3.setName("xiongxiong");
p3.setAge("12");
p3.setSex("男");
p2.setName("pengpeng");
p2.setAge("12");
p2.setSex("男");
list.add(p1);
list.add(p2);
list.add(p3);
/**進行轉換,這裏也可以用泛型強制轉換下*/
Map map = Util.listToMap(list,"name", Person.class);
/**獲取對象,並輸出*/
System.out.println(((Person)map.get("pengpeng")).getAge());
/**原始思路,然後把這個方法抽象就成了下面的*/
Map<String,Person> map2 = new HashMap<String,Person>();
for(int i=0;i<list.size();i++){
map2.put(list.get(i).getName(), list.get(i));
}
}
/**
* List轉Map工具方法
* 原諒我拋了那麼多異常……額 僅做測試之用
* @param list 欲轉換list
* @param key 指定list中對象某個屬性,作爲轉換後map的key
* @param clazz list中作爲轉換後value的對象的類型
* @return Map<Object,Object> 轉換後的map 返回
* @throws SecurityException
* @throws NoSuchFieldException
* @throws NoSuchMethodException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Map<Object,Object> listToMap(List<?> list,String key,Class<?> clazz) throws SecurityException, NoSuchFieldException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Map<Object,Object> map = new HashMap<Object,Object>();//聲明一個map
Class<?> classType = clazz;//獲取類型
Method getter = Util.obtainGetter(classType, key);//獲取getter方法
for(int i=0;i<list.size();i++){
//將指定的屬性值作爲key,對象最爲value放入map
map.put(getter.invoke(list.get(i)), list.get(i));//invoke執行list中對象的getter方法
}
return map;
}
/**
* 獲取getter方法
* @param clazz 欲進行反射獲取的類
* @param key 欲獲取的getter方法 這裏是某一個,也可以擴展成所有的,只需要遍歷該對象的所有field
* @return Method 返回方法
* @throws SecurityException
* @throws NoSuchFieldException
* @throws NoSuchMethodException
*/
public static Method obtainGetter(Class<?> clazz,String key) throws SecurityException, NoSuchFieldException, NoSuchMethodException{
Class<?> classType = clazz;//獲取類
Field field = classType.getDeclaredField(key);//獲取屬性,Declared可以獲取所有聲明的屬性,包括私有的,如果你用getField則只能獲得public修飾的
String fieldName = field.getName();//獲取屬性的名稱
String firstChar = fieldName.substring(0,1).toUpperCase();//getter方法 形如getName 這裏是獲取固定格式的大寫字母
String getterName = "get"+firstChar+fieldName.substring(1);//拼裝方法名稱 這裏的fieldName.substring(1)表示截取 從第一個之後的字符
Method getter = classType.getDeclaredMethod(getterName);//獲取方法
return getter;
}
}