1 /**2 * Grouped by parameter LS for given object properties3 * @paramLS Collection4 * @parama property in the PropertyName object5 * @returnHashMap (key=propertyvalue,value=arraylist)6 * @throwsillegalaccessexception7 * @throwsinvocationtargetexception8 * @throwsnosuchmethodexception9 */Ten@SuppressWarnings ("Unchecked") One Public Static<T,E> hashmap<t,list<e>> groupbyproperty (list<e> ls,string PropertyName)throwsillegalaccessexception, invocationtargetexception, nosuchmethodexception{ AHashmap<t,list<e>> result=NewHashmap<t,list<e>>(); -List<e> list=NULL; - for(Iterator<e> iter =ls.iterator (); Iter.hasnext ();) { theE element =Iter.next (); -T provalue=(T) Propertyutils.getproperty (element, PropertyName); - if(provalue==NULL) - Throw NewNullPointerException ("PropertyValue is null"); + if(Result.containskey (Provalue)) { -List= (list<e>) Result.get (provalue); +}Else{ Alist=NewArraylist<e>(); at result.put (Provalue, list); - } - List.add (element); - } - returnresult; -}
1 /**2 * Extracts the properties of the objects in the collection and combines them into a list.3 * 4 * @paramCollection source collection.5 * @paramPropertyName The name of the property to extract.6 */7@SuppressWarnings ({"Unchecked", "Rawtypes" })8 Public StaticList Fetchelementpropertytolist (FinalCollection Collection,FinalString PropertyName)throwsException {TenList List =NewArrayList (); One for(Object obj:collection) { A List.add (Propertyutils.getproperty (obj, PropertyName)); - } - the returnlist; -}
Collection Processing Tools