快速并优雅的使用
如何快速创建只有单个元素的集合
利用
java.util.Collections
中的
singletonList(T o)
singleton(T o)
singletonMap(K key, V value)
可分别快速创建 长度为1的List,Set,Map
示例:
List<Long> longList = Collections.singletonList(Long.parseLong(request.getId()))
具体如下:
/**
* Returns an immutable list containing only the specified object.
* The returned list is serializable.
*
* @param <T> the class of the objects in the list
* @param o the sole object to be stored in the returned list.
* @return an immutable list containing only the specified object.
* @since 1.3
*/
public static <T> List<T> singletonList(T o) {
return new SingletonList<>(o);
}
/**
* Returns an immutable set containing only the specified object.
* The returned set is serializable.
*
* @param <T> the class of the objects in the set
* @param o the sole object to be stored in the returned set.
* @return an immutable set containing only the specified object.
*/
public static <T> Set<T> singleton(T o) {
return new SingletonSet<>(o);
}
/**
* Returns an immutable map, mapping only the specified key to the
* specified value. The returned map is serializable.
*
* @param <K> the class of the map keys
* @param <V> the class of the map values
* @param key the sole key to be stored in the returned map.
* @param value the value to which the returned map maps <tt>key</tt>.
* @return an immutable map containing only the specified key-value
* mapping.
* @since 1.3
*/
public static <K,V> Map<K,V> singletonMap(K key, V value) {
return new SingletonMap<>(key, value);
}
如何更优雅的对集合判空
org.apache.commons.collections4.CollectionUtils中有两个方法:
CollectionUtils.isEmpty(list);// list为null或size等于0时返回true
CollectionUtils.isNotEmpty(set);// set 为null或size等于0时返回true
源码:
//-----------------------------------------------------------------------
/**
* Null-safe check if the specified collection is empty.
* <p>
* Null returns true.
*
* @param coll the collection to check, may be null
* @return true if empty or null
* @since Commons Collections 3.2
*/
public static boolean isEmpty(Collection coll) {
return (coll == null || coll.isEmpty());
}
/**
* Null-safe check if the specified collection is not empty.
* <p>
* Null returns false.
*
* @param coll the collection to check, may be null
* @return true if non-null and non-empty
* @since Commons Collections 3.2
*/
public static boolean isNotEmpty(Collection coll) {
return !CollectionUtils.isEmpty(coll);
}
缺点:org.apache.commons.collections4.CollectionUtils中只能对List、set判断,不能对Map判断
替代: org.springframework.util.CollectionUtils
第一个对List、set
第二个对Map
快速把List内容转移到线程安全的集合
Java 中 ArrayList 和 LinkedList 都不是线程安全的,但可以通过java.util.Collections.synchronizedList(List list)
方法,获取一个线程安全的 List 实例对象。
/**
Returns a synchronized (thread-safe) list backed by the specified list.
In order to guarantee serial access,
it is critical that all access to the backing list is accomplished through the returned list.
It is imperative that the user manually synchronize on the returned list when iterating over it:
List list = Collections.synchronizedList(new ArrayList());
...
synchronized (list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
**/
Collections.synchronizedList(List<T> list);
详细可参考这个:Collections.synchronizedList(List list) 原理分析
List相关
ArrayList.remove()
ArrayList.remove() 抛出java.lang.UnsupportedOperationException
原因及来源:
来源:参考
String s = "a,b,c";
List<String> list = new ArrayList<>(Arrays.asList(Strings.splitList(s)));