Java tutorial
//package com.java2s; import java.lang.reflect.Field; import java.util.Collection; import java.util.Iterator; public class Main { public static <T> T getObjInList(Class<T> clazz, Collection<T> all, String valueStr, String propName) throws Exception { if (all.isEmpty() || valueStr == null) { return null; } T obj = null; Iterator<T> iter = all.iterator(); for (; iter.hasNext();) { T temp = iter.next(); Object match = null; if (propName == null) { match = temp.toString(); } else { Field field = clazz.getField(propName); field.setAccessible(true); match = field.get(temp); } if (valueStr.equals(match)) { obj = temp; break; } } return obj; } }