ArrayUtils,ListUtils,MapUtils,ObjectUtils,SystemUtils
来源:互联网 发布:魔兽世界网络延迟 编辑:程序博客网 时间:2024/05/17 08:30
ArrayUtils
数组工具类,可用于数组常用操作,如:
isEmpty(V[] sourceArray) 判断数组是否为空或长度为0
getLast(V[] sourceArray, V value, V defaultValue, boolean isCircle) 得到数组中某个元素前一个元素,isCircle表示是否循环
getNext(V[] sourceArray, V value, V defaultValue, boolean isCircle) 得到数组中某个元素下一个元素,isCircle表示是否循环
/** * Array Utils * <ul> * <li>{@link #isEmpty(Object[])} is null or its length is 0</li> * <li>{@link #getLast(Object[], Object, Object, boolean)} get last element of the target element, before the first one * that match the target element front to back</li> * <li>{@link #getNext(Object[], Object, Object, boolean)} get next element of the target element, after the first one * that match the target element front to back</li> * <li>{@link #getLast(Object[], Object, boolean)}</li> * <li>{@link #getLast(int[], int, int, boolean)}</li> * <li>{@link #getLast(long[], long, long, boolean)}</li> * <li>{@link #getNext(Object[], Object, boolean)}</li> * <li>{@link #getNext(int[], int, int, boolean)}</li> * <li>{@link #getNext(long[], long, long, boolean)}</li> * </ul> */public class ArrayUtils { private ArrayUtils() { throw new AssertionError(); } /** * is null or its length is 0 * * @param <V> * @param sourceArray * @return */ public static <V> boolean isEmpty(V[] sourceArray) { return (sourceArray == null || sourceArray.length == 0); } /** * get last element of the target element, before the first one that match the target element front to back * <ul> * <li>if array is empty, return defaultValue</li> * <li>if target element is not exist in array, return defaultValue</li> * <li>if target element exist in array and its index is not 0, return the last element</li> * <li>if target element exist in array and its index is 0, return the last one in array if isCircle is true, else * return defaultValue</li> * </ul> * * @param <V> * @param sourceArray * @param value value of target element * @param defaultValue default return value * @param isCircle whether is circle * @return */ public static <V> V getLast(V[] sourceArray, V value, V defaultValue, boolean isCircle) { if (isEmpty(sourceArray)) { return defaultValue; } int currentPosition = -1; for (int i = 0; i < sourceArray.length; i++) { if (ObjectUtils.isEquals(value, sourceArray[i])) { currentPosition = i; break; } } if (currentPosition == -1) { return defaultValue; } if (currentPosition == 0) { return isCircle ? sourceArray[sourceArray.length - 1] : defaultValue; } return sourceArray[currentPosition - 1]; } /** * get next element of the target element, after the first one that match the target element front to back * <ul> * <li>if array is empty, return defaultValue</li> * <li>if target element is not exist in array, return defaultValue</li> * <li>if target element exist in array and not the last one in array, return the next element</li> * <li>if target element exist in array and the last one in array, return the first one in array if isCircle is * true, else return defaultValue</li> * </ul> * * @param <V> * @param sourceArray * @param value value of target element * @param defaultValue default return value * @param isCircle whether is circle * @return */ public static <V> V getNext(V[] sourceArray, V value, V defaultValue, boolean isCircle) { if (isEmpty(sourceArray)) { return defaultValue; } int currentPosition = -1; for (int i = 0; i < sourceArray.length; i++) { if (ObjectUtils.isEquals(value, sourceArray[i])) { currentPosition = i; break; } } if (currentPosition == -1) { return defaultValue; } if (currentPosition == sourceArray.length - 1) { return isCircle ? sourceArray[0] : defaultValue; } return sourceArray[currentPosition + 1]; } /** * @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} defaultValue is null */ public static <V> V getLast(V[] sourceArray, V value, boolean isCircle) { return getLast(sourceArray, value, null, isCircle); } /** * @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} defaultValue is null */ public static <V> V getNext(V[] sourceArray, V value, boolean isCircle) { return getNext(sourceArray, value, null, isCircle); } /** * @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} Object is Long */ public static long getLast(long[] sourceArray, long value, long defaultValue, boolean isCircle) { if (sourceArray.length == 0) { throw new IllegalArgumentException("The length of source array must be greater than 0."); } Long[] array = ObjectUtils.transformLongArray(sourceArray); return getLast(array, value, defaultValue, isCircle); } /** * @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} Object is Long */ public static long getNext(long[] sourceArray, long value, long defaultValue, boolean isCircle) { if (sourceArray.length == 0) { throw new IllegalArgumentException("The length of source array must be greater than 0."); } Long[] array = ObjectUtils.transformLongArray(sourceArray); return getNext(array, value, defaultValue, isCircle); } /** * @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} Object is Integer */ public static int getLast(int[] sourceArray, int value, int defaultValue, boolean isCircle) { if (sourceArray.length == 0) { throw new IllegalArgumentException("The length of source array must be greater than 0."); } Integer[] array = ObjectUtils.transformIntArray(sourceArray); return getLast(array, value, defaultValue, isCircle); } /** * @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} Object is Integer */ public static int getNext(int[] sourceArray, int value, int defaultValue, boolean isCircle) { if (sourceArray.length == 0) { throw new IllegalArgumentException("The length of source array must be greater than 0."); } Integer[] array = ObjectUtils.transformIntArray(sourceArray); return getNext(array, value, defaultValue, isCircle); }}
ListUtils
List工具类,可用于List常用操作,如:
isEmpty(List sourceList) 判断List是否为空或长度为0
join(List list, String separator) List转换为字符串,并以固定分隔符分割
addDistinctEntry(List sourceList, V entry) 向list中添加不重复元素
import java.util.ArrayList;import java.util.List;import android.text.TextUtils;/** * List Utils */public class ListUtils { /** default join separator **/ public static final String DEFAULT_JOIN_SEPARATOR = ","; private ListUtils() { throw new AssertionError(); } /** * get size of list * * <pre> * getSize(null) = 0; * getSize({}) = 0; * getSize({1}) = 1; * </pre> * * @param <V> * @param sourceList * @return if list is null or empty, return 0, else return {@link List#size()}. */ public static <V> int getSize(List<V> sourceList) { return sourceList == null ? 0 : sourceList.size(); } /** * is null or its size is 0 * * <pre> * isEmpty(null) = true; * isEmpty({}) = true; * isEmpty({1}) = false; * </pre> * * @param <V> * @param sourceList * @return if list is null or its size is 0, return true, else return false. */ public static <V> boolean isEmpty(List<V> sourceList) { return (sourceList == null || sourceList.size() == 0); } /** * compare two list * * <pre> * isEquals(null, null) = true; * isEquals(new ArrayList<String>(), null) = false; * isEquals(null, new ArrayList<String>()) = false; * isEquals(new ArrayList<String>(), new ArrayList<String>()) = true; * </pre> * * @param <V> * @param actual * @param expected * @return */ public static <V> boolean isEquals(ArrayList<V> actual, ArrayList<V> expected) { if (actual == null) { return expected == null; } if (expected == null) { return false; } if (actual.size() != expected.size()) { return false; } for (int i = 0; i < actual.size(); i++) { if (!ObjectUtils.isEquals(actual.get(i), expected.get(i))) { return false; } } return true; } /** * join list to string, separator is "," * * <pre> * join(null) = ""; * join({}) = ""; * join({a,b}) = "a,b"; * </pre> * * @param list * @return join list to string, separator is ",". if list is empty, return "" */ public static String join(List<String> list) { return join(list, DEFAULT_JOIN_SEPARATOR); } /** * join list to string * * <pre> * join(null, '#') = ""; * join({}, '#') = ""; * join({a,b,c}, ' ') = "abc"; * join({a,b,c}, '#') = "a#b#c"; * </pre> * * @param list * @param separator * @return join list to string. if list is empty, return "" */ public static String join(List<String> list, char separator) { return join(list, new String(new char[] {separator})); } /** * join list to string. if separator is null, use {@link #DEFAULT_JOIN_SEPARATOR} * * <pre> * join(null, "#") = ""; * join({}, "#$") = ""; * join({a,b,c}, null) = "a,b,c"; * join({a,b,c}, "") = "abc"; * join({a,b,c}, "#") = "a#b#c"; * join({a,b,c}, "#$") = "a#$b#$c"; * </pre> * * @param list * @param separator * @return join list to string with separator. if list is empty, return "" */ public static String join(List<String> list, String separator) { return list == null ? "" : TextUtils.join(separator, list); } /** * add distinct entry to list * * @param <V> * @param sourceList * @param entry * @return if entry already exist in sourceList, return false, else add it and return true. */ public static <V> boolean addDistinctEntry(List<V> sourceList, V entry) { return (sourceList != null && !sourceList.contains(entry)) ? sourceList.add(entry) : false; } /** * add all distinct entry to list1 from list2 * * @param <V> * @param sourceList * @param entryList * @return the count of entries be added */ public static <V> int addDistinctList(List<V> sourceList, List<V> entryList) { if (sourceList == null || isEmpty(entryList)) { return 0; } int sourceCount = sourceList.size(); for (V entry : entryList) { if (!sourceList.contains(entry)) { sourceList.add(entry); } } return sourceList.size() - sourceCount; } /** * remove duplicate entries in list * * @param <V> * @param sourceList * @return the count of entries be removed */ public static <V> int distinctList(List<V> sourceList) { if (isEmpty(sourceList)) { return 0; } int sourceCount = sourceList.size(); int sourceListSize = sourceList.size(); for (int i = 0; i < sourceListSize; i++) { for (int j = (i + 1); j < sourceListSize; j++) { if (sourceList.get(i).equals(sourceList.get(j))) { sourceList.remove(j); sourceListSize = sourceList.size(); j--; } } } return sourceCount - sourceList.size(); } /** * add not null entry to list * * @param sourceList * @param value * @return <ul> * <li>if sourceList is null, return false</li> * <li>if value is null, return false</li> * <li>return {@link List#add(Object)}</li> * </ul> */ public static <V> boolean addListNotNullValue(List<V> sourceList, V value) { return (sourceList != null && value != null) ? sourceList.add(value) : false; } /** * @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} defaultValue is null, isCircle is true */ @SuppressWarnings("unchecked") public static <V> V getLast(List<V> sourceList, V value) { return (sourceList == null) ? null : (V)ArrayUtils.getLast(sourceList.toArray(), value, true); } /** * @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} defaultValue is null, isCircle is true */ @SuppressWarnings("unchecked") public static <V> V getNext(List<V> sourceList, V value) { return (sourceList == null) ? null : (V)ArrayUtils.getNext(sourceList.toArray(), value, true); } /** * invert list * * @param <V> * @param sourceList * @return */ public static <V> List<V> invertList(List<V> sourceList) { if (isEmpty(sourceList)) { return sourceList; } List<V> invertList = new ArrayList<V>(sourceList.size()); for (int i = sourceList.size() - 1; i >= 0; i--) { invertList.add(sourceList.get(i)); } return invertList; }}
MapUtils
Map工具类,可用于Map常用操作,如: isEmpty(Map<K, V> sourceMap) 判断map是否为空或长度为0 parseKeyAndValueToMap(String source, String keyAndValueSeparator, String keyAndValuePairSeparator, boolean ignoreSpace) 字符串解析为maptoJson(Map<String, String> map) map转换为json格式
import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;/** * Map Utils */public class MapUtils { /** default separator between key and value **/ public static final String DEFAULT_KEY_AND_VALUE_SEPARATOR = ":"; /** default separator between key-value pairs **/ public static final String DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR = ","; private MapUtils() { throw new AssertionError(); } /** * is null or its size is 0 * * <pre> * isEmpty(null) = true; * isEmpty({}) = true; * isEmpty({1, 2}) = false; * </pre> * * @param sourceMap * @return if map is null or its size is 0, return true, else return false. */ public static <K, V> boolean isEmpty(Map<K, V> sourceMap) { return (sourceMap == null || sourceMap.size() == 0); } /** * add key-value pair to map, and key need not null or empty * * @param map * @param key * @param value * @return <ul> * <li>if map is null, return false</li> * <li>if key is null or empty, return false</li> * <li>return {@link Map#put(Object, Object)}</li> * </ul> */ public static boolean putMapNotEmptyKey(Map<String, String> map, String key, String value) { if (map == null || StringUtils.isEmpty(key)) { return false; } map.put(key, value); return true; } /** * add key-value pair to map, both key and value need not null or empty * * @param map * @param key * @param value * @return <ul> * <li>if map is null, return false</li> * <li>if key is null or empty, return false</li> * <li>if value is null or empty, return false</li> * <li>return {@link Map#put(Object, Object)}</li> * </ul> */ public static boolean putMapNotEmptyKeyAndValue(Map<String, String> map, String key, String value) { if (map == null || StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { return false; } map.put(key, value); return true; } /** * add key-value pair to map, key need not null or empty * * @param map * @param key * @param value * @param defaultValue * @return <ul> * <li>if map is null, return false</li> * <li>if key is null or empty, return false</li> * <li>if value is null or empty, put defaultValue, return true</li> * <li>if value is neither null nor empty,put value, return true</li> * </ul> */ public static boolean putMapNotEmptyKeyAndValue(Map<String, String> map, String key, String value, String defaultValue) { if (map == null || StringUtils.isEmpty(key)) { return false; } map.put(key, StringUtils.isEmpty(value) ? defaultValue : value); return true; } /** * add key-value pair to map, key need not null * * @param map * @param key * @param value * @return <ul> * <li>if map is null, return false</li> * <li>if key is null, return false</li> * <li>return {@link Map#put(Object, Object)}</li> * </ul> */ public static <K, V> boolean putMapNotNullKey(Map<K, V> map, K key, V value) { if (map == null || key == null) { return false; } map.put(key, value); return true; } /** * add key-value pair to map, both key and value need not null * * @param map * @param key * @param value * @return <ul> * <li>if map is null, return false</li> * <li>if key is null, return false</li> * <li>if value is null, return false</li> * <li>return {@link Map#put(Object, Object)}</li> * </ul> */ public static <K, V> boolean putMapNotNullKeyAndValue(Map<K, V> map, K key, V value) { if (map == null || key == null || value == null) { return false; } map.put(key, value); return true; } /** * get key by value, match the first entry front to back * <ul> * <strong>Attentions:</strong> * <li>for HashMap, the order of entry not same to put order, so you may need to use TreeMap</li> * </ul> * * @param <V> * @param map * @param value * @return <ul> * <li>if map is null, return null</li> * <li>if value exist, return key</li> * <li>return null</li> * </ul> */ public static <K, V> K getKeyByValue(Map<K, V> map, V value) { if (isEmpty(map)) { return null; } for (Entry<K, V> entry : map.entrySet()) { if (ObjectUtils.isEquals(entry.getValue(), value)) { return entry.getKey(); } } return null; } /** * parse key-value pairs to map, ignore empty key * * <pre> * parseKeyAndValueToMap("","","",true)=null * parseKeyAndValueToMap(null,"","",true)=null * parseKeyAndValueToMap("a:b,:","","",true)={(a,b)} * parseKeyAndValueToMap("a:b,:d","","",true)={(a,b)} * parseKeyAndValueToMap("a:b,c:d","","",true)={(a,b),(c,d)} * parseKeyAndValueToMap("a=b, c = d","=",",",true)={(a,b),(c,d)} * parseKeyAndValueToMap("a=b, c = d","=",",",false)={(a, b),( c , d)} * parseKeyAndValueToMap("a=b, c=d","=", ",", false)={(a,b),( c,d)} * parseKeyAndValueToMap("a=b; c=d","=", ";", false)={(a,b),( c,d)} * parseKeyAndValueToMap("a=b, c=d", ",", ";", false)={(a=b, c=d)} * </pre> * * @param source key-value pairs * @param keyAndValueSeparator separator between key and value * @param keyAndValuePairSeparator separator between key-value pairs * @param ignoreSpace whether ignore space at the begging or end of key and value * @return */ public static Map<String, String> parseKeyAndValueToMap(String source, String keyAndValueSeparator, String keyAndValuePairSeparator, boolean ignoreSpace) { if (StringUtils.isEmpty(source)) { return null; } if (StringUtils.isEmpty(keyAndValueSeparator)) { keyAndValueSeparator = DEFAULT_KEY_AND_VALUE_SEPARATOR; } if (StringUtils.isEmpty(keyAndValuePairSeparator)) { keyAndValuePairSeparator = DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR; } Map<String, String> keyAndValueMap = new HashMap<String, String>(); String[] keyAndValueArray = source.split(keyAndValuePairSeparator); if (keyAndValueArray == null) { return null; } int seperator; for (String valueEntity : keyAndValueArray) { if (!StringUtils.isEmpty(valueEntity)) { seperator = valueEntity.indexOf(keyAndValueSeparator); if (seperator != -1) { if (ignoreSpace) { MapUtils.putMapNotEmptyKey(keyAndValueMap, valueEntity.substring(0, seperator).trim(), valueEntity.substring(seperator + 1).trim()); } else { MapUtils.putMapNotEmptyKey(keyAndValueMap, valueEntity.substring(0, seperator), valueEntity.substring(seperator + 1)); } } } } return keyAndValueMap; } /** * parse key-value pairs to map, ignore empty key * * @param source key-value pairs * @param ignoreSpace whether ignore space at the begging or end of key and value * @return * @see {@link MapUtils#parseKeyAndValueToMap(String, String, String, boolean)}, keyAndValueSeparator is * {@link #DEFAULT_KEY_AND_VALUE_SEPARATOR}, keyAndValuePairSeparator is * {@link #DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR} */ public static Map<String, String> parseKeyAndValueToMap(String source, boolean ignoreSpace) { return parseKeyAndValueToMap(source, DEFAULT_KEY_AND_VALUE_SEPARATOR, DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR, ignoreSpace); } /** * parse key-value pairs to map, ignore empty key, ignore space at the begging or end of key and value * * @param source key-value pairs * @return * @see {@link MapUtils#parseKeyAndValueToMap(String, String, String, boolean)}, keyAndValueSeparator is * {@link #DEFAULT_KEY_AND_VALUE_SEPARATOR}, keyAndValuePairSeparator is * {@link #DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR}, ignoreSpace is true */ public static Map<String, String> parseKeyAndValueToMap(String source) { return parseKeyAndValueToMap(source, DEFAULT_KEY_AND_VALUE_SEPARATOR, DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR, true); } /** * join map * * @param map * @return */ public static String toJson(Map<String, String> map) { if (map == null || map.size() == 0) { return null; } StringBuilder paras = new StringBuilder(); paras.append("{"); Iterator<Map.Entry<String, String>> ite = map.entrySet().iterator(); while (ite.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>)ite.next(); paras.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\""); if (ite.hasNext()) { paras.append(","); } } paras.append("}"); return paras.toString(); }}
ObjectUtils
Object工具类,可用于Object常用操作,如: isEquals(Object actual, Object expected) 比较两个对象是否相等 compare(V v1, V v2) 比较两个对象大小 transformIntArray(int[] source) Integer 数组转换为int数组
public class ObjectUtils { private ObjectUtils() { throw new AssertionError(); } /** * compare two object * * @param actual * @param expected * @return <ul> * <li>if both are null, return true</li> * <li>return actual.{@link Object#equals(Object)}</li> * </ul> */ public static boolean isEquals(Object actual, Object expected) { return actual == expected || (actual == null ? expected == null : actual.equals(expected)); } /** * null Object to empty string * * <pre> * nullStrToEmpty(null) = ""; * nullStrToEmpty("") = ""; * nullStrToEmpty("aa") = "aa"; * </pre> * * @param str * @return */ public static String nullStrToEmpty(Object str) { return (str == null ? "" : (str instanceof String ? (String)str : str.toString())); } /** * convert long array to Long array * * @param source * @return */ public static Long[] transformLongArray(long[] source) { Long[] destin = new Long[source.length]; for (int i = 0; i < source.length; i++) { destin[i] = source[i]; } return destin; } /** * convert Long array to long array * * @param source * @return */ public static long[] transformLongArray(Long[] source) { long[] destin = new long[source.length]; for (int i = 0; i < source.length; i++) { destin[i] = source[i]; } return destin; } /** * convert int array to Integer array * * @param source * @return */ public static Integer[] transformIntArray(int[] source) { Integer[] destin = new Integer[source.length]; for (int i = 0; i < source.length; i++) { destin[i] = source[i]; } return destin; } /** * convert Integer array to int array * * @param source * @return */ public static int[] transformIntArray(Integer[] source) { int[] destin = new int[source.length]; for (int i = 0; i < source.length; i++) { destin[i] = source[i]; } return destin; } /** * compare two object * <ul> * <strong>About result</strong> * <li>if v1 > v2, return 1</li> * <li>if v1 = v2, return 0</li> * <li>if v1 < v2, return -1</li> * </ul> * <ul> * <strong>About rule</strong> * <li>if v1 is null, v2 is null, then return 0</li> * <li>if v1 is null, v2 is not null, then return -1</li> * <li>if v1 is not null, v2 is null, then return 1</li> * <li>return v1.{@link Comparable#compareTo(Object)}</li> * </ul> * * @param v1 * @param v2 * @return */ @SuppressWarnings({"unchecked", "rawtypes"}) public static <V> int compare(V v1, V v2) { return v1 == null ? (v2 == null ? 0 : -1) : (v2 == null ? 1 : ((Comparable)v1).compareTo(v2)); }}
SystemUtils
系统信息工具类,可用于得到线程池合适的大小,如:
getDefaultThreadPoolSize() 得到跟系统配置相符的线程池大小
public class SystemUtils { /** recommend default thread pool size according to system available processors, {@link #getDefaultThreadPoolSize()} **/ public static final int DEFAULT_THREAD_POOL_SIZE = getDefaultThreadPoolSize(); private SystemUtils() { throw new AssertionError(); } /** * get recommend default thread pool size * * @return if 2 * availableProcessors + 1 less than 8, return it, else return 8; * @see {@link #getDefaultThreadPoolSize(int)} max is 8 */ public static int getDefaultThreadPoolSize() { return getDefaultThreadPoolSize(8); } /** * get recommend default thread pool size * * @param max * @return if 2 * availableProcessors + 1 less than max, return it, else return max; */ public static int getDefaultThreadPoolSize(int max) { int availableProcessors = 2 * Runtime.getRuntime().availableProcessors() + 1; return availableProcessors > max ? max : availableProcessors; }}
0 0
- ArrayUtils,ListUtils,MapUtils,ObjectUtils,SystemUtils
- 集合工具类CollectionUtils、ListUtils、SetUtils、MapUtils探究
- ObjectUtils
- ObjectUtils
- systemutils
- ArrayUtils
- ArrayUtils
- ArrayUtils
- ArrayUtils
- objectUtils.js
- MapUtils.getString(map, "ALARMREGION", "")
- 每周Apache commons-SystemUtils
- ArrayUtils 笔记
- ArrayUtils.java
- ArrayUtils用法
- ArrayUtils用法
- ArrayUtils学习
- arrayUtils.js
- 自定义View 画圆
- 远程不到桌面了
- Android五大存储之数据库事物篇
- [noip2007pj] 纪念品分组
- html 问题随记+素材积累
- ArrayUtils,ListUtils,MapUtils,ObjectUtils,SystemUtils
- java String sql语句中的双引号 单引号 转义符
- hdu5510 Bazinga 暴力+剪枝
- NOIP数论总结
- linux ubuntu 16.04 flash插件安装
- 最简单解决 Ubuntu 16.* 在基于 Qt 为界面的程序中无法输入中文(Latex\Pycharm)
- PHP框架CI 学习笔记<二>
- 复杂网络综述
- web打印