国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

javaBean的各種工具方法

2019-11-14 12:14:57
字體:
來源:轉載
供稿:網友

1.listToMap將list集合轉換成map集合

	public static <K, V> Map<K, V> listToMap(String keyName, List<V> list) {		Map<K, V> map = Maps.newHashMap();		if (CollectionUtils.isNotEmpty(list)) {			for (V val : list) {				try {					PRopertyDescriptor pd = new PropertyDescriptor(keyName,							val.getClass());					Method getMethod = pd.getReadMethod();// 獲得get方法					Object o = getMethod.invoke(val);// 執行get方法返回一個Object					if (o != null) {						map.put((K) o, val);					}				} catch (IllegalaccessException | IllegalArgumentException						| InvocationTargetException | IntrospectionException e) {					e.printStackTrace();				}			}		}		return map;	}2.listToMapList將list集合轉換成Map<K, List<V>>

	// listToMap,1個key對應的是1個元素	// listToMapList,1個key對應的是1個list。list中屬性id一樣的,放到1個小的list中。	// 可以去掉k,v這2個參數	public static <K, V> Map<K, List<V>> listToMapList(String keyName,List<V> list) {		Map<K, List<V>> map = Maps.newHashMap();		if (CollectionUtils.isNotEmpty(list)) {			for (V val : list) {				try {					PropertyDescriptor pd = new PropertyDescriptor(keyName,val.getClass());					Method getMethod = pd.getReadMethod();// 獲得get方法					Object o = getMethod.invoke(val);// 執行get方法返回一個Object					if (o != null) {						List<V> valueList = map.get((K) o);						if (valueList == null) {							valueList = Lists.newArrayList();						}						valueList.add(val);						map.put((K) o, valueList);					}				} catch (IllegalAccessException | IllegalArgumentException						| InvocationTargetException | IntrospectionException e) {					e.printStackTrace();				}			}		}		return map;	}3.beanToMap將一個javaBean轉換成map集合

	public static Map<String, Object> bean2Map(Object obj) {		if (obj == null) {			return null;		}		Map<String, Object> map = Maps.newHashMap();		try {			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();			for (PropertyDescriptor property : propertyDescriptors) {				String key = property.getName();				// 過濾class屬性				if (!key.equalsIgnoreCase("class")) {					// 得到property對應的getter方法					Method getter = property.getReadMethod();					Object value = getter.invoke(obj);					map.put(key, value);				}			}		} catch (Exception e) {			e.printStackTrace();		}		return map;	}4.map批量過濾key的值

	public static Map<String, Object> MapFilter(Map<String, Object> map,			final List<String> keys) {		return Maps.filterKeys(map, new Predicate<String>() {			@Override			public boolean apply(String input) {				return keys.contains(input) ? false : true;			}		});	}5.提取集合中的對象的一個屬性, 組合成List
	public static <T> List<T> extractToList(final Collection<?> collection,			final String propertyName) {		if (CollectionUtils.isEmpty(collection)) {			return null;		}		List<T> list = new ArrayList<T>(collection.size());		CollectionUtils.collect(collection, new BeanToPropertyValueTransformer(propertyName), list);		return list;	}6.將一個字符串按指定規則分割然后類型轉換,放入list中

	public static List<Long> stringToLongList(String str,String separator){		List<Long> idList = Lists.newArrayList();		String[] idArray = str.split(separator);		if(idArray != null){			for(String id:idArray){				idList.add(Long.valueOf(id));			}		}		return idList;	}


上一篇:xml數據二級聯動

下一篇:vs2012轉成vs2010

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 慈溪市| 西吉县| 资兴市| 樟树市| 方正县| 洞口县| 天峨县| 新郑市| 广平县| 黑水县| 崇信县| 舟山市| 汉阴县| 新民市| 阜阳市| 保山市| 防城港市| 美姑县| 宾川县| 柘城县| 东乡县| 宜城市| 稷山县| 大化| 铜川市| 颍上县| 绵竹市| 成安县| 南平市| 彝良县| 河西区| 涟源市| 融水| 哈密市| 三都| 安阳市| 临夏市| 金溪县| 遂溪县| 洛隆县| 新营市|