类 CollectionFactory
- java.lang.Object
- org.springframework.core.CollectionFactory
public final class CollectionFactory extends Object
Factory for collections that is aware of common Java and Spring collection types.Mainly for internal use within the framework.
- 从以下版本开始:
- 1.1.1
- 作者:
- Juergen Hoeller, Arjen Poutsma, Oliver Gierke, Sam Brannen
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static <E> Collection<E>
createApproximateCollection(Object collection, int capacity)
Create the most approximate collection for the given collection.static <K,V>
Map<K,V>createApproximateMap(Object map, int capacity)
Create the most approximate map for the given map.static <E> Collection<E>
createCollection(Class<?> collectionType, int capacity)
Create the most appropriate collection for the given collection type.static <E> Collection<E>
createCollection(Class<?> collectionType, Class<?> elementType, int capacity)
Create the most appropriate collection for the given collection type.static <K,V>
Map<K,V>createMap(Class<?> mapType, int capacity)
Create the most appropriate map for the given map type.static <K,V>
Map<K,V>createMap(Class<?> mapType, Class<?> keyType, int capacity)
Create the most appropriate map for the given map type.static Properties
createSortedProperties(boolean omitComments)
Create a variant ofProperties
that sorts properties alphanumerically based on their keys.static Properties
createSortedProperties(Properties properties, boolean omitComments)
Create a variant ofProperties
that sorts properties alphanumerically based on their keys.static Properties
createStringAdaptingProperties()
Create a variant ofProperties
that automatically adapts non-String values to String representations inProperties.getProperty(java.lang.String)
.static boolean
isApproximableCollectionType(Class<?> collectionType)
Determine whether the given collection type is an approximable type, i.e. a type thatcreateApproximateCollection(java.lang.Object, int)
can approximate.static boolean
isApproximableMapType(Class<?> mapType)
Determine whether the given map type is an approximable type, i.e. a type thatcreateApproximateMap(java.lang.Object, int)
can approximate.
方法详细资料
isApproximableCollectionType
public static boolean isApproximableCollectionType(@Nullable Class<?> collectionType)
Determine whether the given collection type is an approximable type, i.e. a type thatcreateApproximateCollection(java.lang.Object, int)
can approximate.- 参数:
collectionType
- the collection type to check- 返回:
true
if the type is approximable
createApproximateCollection
public static <E> Collection<E> createApproximateCollection(@Nullable Object collection, int capacity)
Create the most approximate collection for the given collection.Warning: Since the parameterized type
E
is not bound to the type of elements contained in the suppliedcollection
, type safety cannot be guaranteed if the suppliedcollection
is anEnumSet
. In such scenarios, the caller is responsible for ensuring that the element type for the suppliedcollection
is an enum type matching typeE
. As an alternative, the caller may wish to treat the return value as a raw collection or collection ofObject
.- 参数:
collection
- the original collection object, potentiallynull
capacity
- the initial capacity- 返回:
- a new, empty collection instance
- 另请参阅:
isApproximableCollectionType(java.lang.Class<?>)
,LinkedList
,ArrayList
,EnumSet
,TreeSet
,LinkedHashSet
createCollection
public static <E> Collection<E> createCollection(Class<?> collectionType, int capacity)
Create the most appropriate collection for the given collection type.Delegates to
createCollection(Class, Class, int)
with anull
element type.- 参数:
collectionType
- the desired type of the target collection (nevernull
)capacity
- the initial capacity- 返回:
- a new collection instance
- 抛出:
IllegalArgumentException
- if the suppliedcollectionType
isnull
or of typeEnumSet
createCollection
public static <E> Collection<E> createCollection(Class<?> collectionType, @Nullable Class<?> elementType, int capacity)
Create the most appropriate collection for the given collection type.Warning: Since the parameterized type
E
is not bound to the suppliedelementType
, type safety cannot be guaranteed if the desiredcollectionType
isEnumSet
. In such scenarios, the caller is responsible for ensuring that the suppliedelementType
is an enum type matching typeE
. As an alternative, the caller may wish to treat the return value as a raw collection or collection ofObject
.- 参数:
collectionType
- the desired type of the target collection (nevernull
)elementType
- the collection's element type, ornull
if unknown (note: only relevant forEnumSet
creation)capacity
- the initial capacity- 返回:
- a new collection instance
- 抛出:
IllegalArgumentException
- if the suppliedcollectionType
isnull
; or if the desiredcollectionType
isEnumSet
and the suppliedelementType
is not a subtype ofEnum
- 从以下版本开始:
- 4.1.3
- 另请参阅:
LinkedHashSet
,ArrayList
,TreeSet
,EnumSet
isApproximableMapType
public static boolean isApproximableMapType(@Nullable Class<?> mapType)
Determine whether the given map type is an approximable type, i.e. a type thatcreateApproximateMap(java.lang.Object, int)
can approximate.- 参数:
mapType
- the map type to check- 返回:
true
if the type is approximable
createApproximateMap
public static <K,V> Map<K,V> createApproximateMap(@Nullable Object map, int capacity)
Create the most approximate map for the given map.Warning: Since the parameterized type
K
is not bound to the type of keys contained in the suppliedmap
, type safety cannot be guaranteed if the suppliedmap
is anEnumMap
. In such scenarios, the caller is responsible for ensuring that the key type in the suppliedmap
is an enum type matching typeK
. As an alternative, the caller may wish to treat the return value as a raw map or map keyed byObject
.- 参数:
map
- the original map object, potentiallynull
capacity
- the initial capacity- 返回:
- a new, empty map instance
- 另请参阅:
isApproximableMapType(java.lang.Class<?>)
,EnumMap
,TreeMap
,LinkedHashMap
createMap
public static <K,V> Map<K,V> createMap(Class<?> mapType, int capacity)
Create the most appropriate map for the given map type.Delegates to
createMap(Class, Class, int)
with anull
key type.- 参数:
mapType
- the desired type of the target mapcapacity
- the initial capacity- 返回:
- a new map instance
- 抛出:
IllegalArgumentException
- if the suppliedmapType
isnull
or of typeEnumMap
createMap
public static <K,V> Map<K,V> createMap(Class<?> mapType, @Nullable Class<?> keyType, int capacity)
Create the most appropriate map for the given map type.Warning: Since the parameterized type
K
is not bound to the suppliedkeyType
, type safety cannot be guaranteed if the desiredmapType
isEnumMap
. In such scenarios, the caller is responsible for ensuring that thekeyType
is an enum type matching typeK
. As an alternative, the caller may wish to treat the return value as a raw map or map keyed byObject
. Similarly, type safety cannot be enforced if the desiredmapType
isMultiValueMap
.- 参数:
mapType
- the desired type of the target map (nevernull
)keyType
- the map's key type, ornull
if unknown (note: only relevant forEnumMap
creation)capacity
- the initial capacity- 返回:
- a new map instance
- 抛出:
IllegalArgumentException
- if the suppliedmapType
isnull
; or if the desiredmapType
isEnumMap
and the suppliedkeyType
is not a subtype ofEnum
- 从以下版本开始:
- 4.1.3
- 另请参阅:
LinkedHashMap
,TreeMap
,LinkedMultiValueMap
,EnumMap
createStringAdaptingProperties
public static Properties createStringAdaptingProperties()
Create a variant ofProperties
that automatically adapts non-String values to String representations inProperties.getProperty(java.lang.String)
.In addition, the returned
Properties
instance sorts properties alphanumerically based on their keys.- 返回:
- a new
Properties
instance - 从以下版本开始:
- 4.3.4
- 另请参阅:
createSortedProperties(boolean)
,createSortedProperties(Properties, boolean)
createSortedProperties
public static Properties createSortedProperties(boolean omitComments)
Create a variant ofProperties
that sorts properties alphanumerically based on their keys.This can be useful when storing the
Properties
instance in a properties file, since it allows such files to be generated in a repeatable manner with consistent ordering of properties. Comments in generated properties files can also be optionally omitted.- 参数:
omitComments
-true
if comments should be omitted when storing properties in a file- 返回:
- a new
Properties
instance - 从以下版本开始:
- 5.2
- 另请参阅:
createStringAdaptingProperties()
,createSortedProperties(Properties, boolean)
createSortedProperties
public static Properties createSortedProperties(Properties properties, boolean omitComments)
Create a variant ofProperties
that sorts properties alphanumerically based on their keys.This can be useful when storing the
Properties
instance in a properties file, since it allows such files to be generated in a repeatable manner with consistent ordering of properties. Comments in generated properties files can also be optionally omitted.The returned
Properties
instance will be populated with properties from the suppliedproperties
object, but default properties from the suppliedproperties
object will not be copied.- 参数:
properties
- theProperties
object from which to copy the initial propertiesomitComments
-true
if comments should be omitted when storing properties in a file- 返回:
- a new
Properties
instance - 从以下版本开始:
- 5.2
- 另请参阅:
createStringAdaptingProperties()
,createSortedProperties(boolean)