All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ext_ext.mybatisext.helper.CollectionHelper Maven / Gradle / Ivy

package com.ext_ext.mybatisext.helper;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.ibatis.reflection.MetaClass;
import org.springframework.util.ReflectionUtils;


/**
 * 

* @author 宋汝波 * @date 2015年8月20日 * @version 1.0.0 */ @SuppressWarnings({ "unchecked", "rawtypes" }) public abstract class CollectionHelper { public static Map list2Map( List list, String property ) { if ( list == null ) { return Collections.emptyMap(); } Map map = new LinkedHashMap(list.size()); try { for ( V v : list ) { if ( Map.class.isAssignableFrom(v.getClass()) ) { Map m = (Map) v; map.put((K) m.get(property), v); continue; } MetaClass meta = MetaClass.forClass(v.getClass()); Object fieldValue = meta.getGetInvoker(property).invoke(v, new Object[ ] {}); map.put((K) fieldValue, v); } } catch ( Exception e ) { throw new RuntimeException(e); } return map; } public static Set list2Set( List list, String property ) { if ( list == null ) { return Collections.emptySet(); } Set set = new HashSet(list.size()); for ( Object v : list ) { Field field = ReflectionUtils.findField(v.getClass(), property); ReflectionUtils.makeAccessible(field); Object fieldValue = ReflectionUtils.getField(field, v); set.add((T) fieldValue); } return set; } public static Map> list2MapList( List list, String property ) { if ( list == null ) { return Collections.emptyMap(); } Map> map = new LinkedHashMap>(list.size()); for ( V v : list ) { Field field = ReflectionUtils.findField(v.getClass(), property); ReflectionUtils.makeAccessible(field); Object fieldValue = ReflectionUtils.getField(field, v); List lst = map.get(fieldValue); if ( lst == null ) { lst = new ArrayList(); map.put((K) fieldValue, lst); } lst.add(v); } return map; } public static List listProperty( List list, String property ) { if ( list == null ) { return Collections.emptyList(); } List result = new ArrayList(); for ( Object v : list ) { Field field = ReflectionUtils.findField(v.getClass(), property); ReflectionUtils.makeAccessible(field); Object fieldValue = ReflectionUtils.getField(field, v); result.add((V) fieldValue); } return result; } public static String join( Collection collection, String dot ) { StringBuilder str = new StringBuilder(); Iterator iterator = collection.iterator(); boolean addDot = false; while ( iterator.hasNext() ) { if ( addDot ) { str.append(dot); } String value = iterator.next(); if ( value != null ) { str.append(value); addDot = true; } } return str.toString(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy