package com.taover.util; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class UtilCollection{ public static Map listToMap(List elements, String keyFieldName){ Map result = new HashMap(); if(elements==null || keyFieldName==null || keyFieldName.isEmpty()){ return null; } for(int i=0; i data, String key){ if(data == null){ return null; } Object value = data.get(key); if(value == null){ return null; }else{ if(value.getClass().isArray()){ return String.valueOf(Array.get(value, 0)); }else{ return String.valueOf(value); } } } public static void main(String args[]){ List data = new ArrayList(); data.add(new TestColl("1","2")); data.add(new TestColl("3","4")); data.add(new TestColl("5","6")); Map temp = UtilCollection.listToMap(data, "a"); System.out.println(temp.get("1")); } } class TestColl { String a; String b; public TestColl(String a, String b){ this.a = a; this.b = b; } }