Class NamedList<T>
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<Map.Entry<String,,T>> MapSerializable,MapWriter,NavigableObject,org.noggit.JSONWriter.Writable
- Direct Known Subclasses:
CloudSolrClient.RouteResponse,SimpleOrderedMap
Unlike Maps:
- Names may be repeated
- Order of elements is maintained
- Elements may be accessed by numeric index
- Names and Values can both be null
A NamedList provides fast access by element number, but not by name.
When a NamedList is serialized, order is considered more important than access by key, so
ResponseWriters that output to a format such as JSON will normally choose a data structure that
allows order to be easily preserved in various clients (i.e. not a straight map). If access by
key is more important for serialization, see SimpleOrderedMap, or simply use a regular
Map
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface org.apache.solr.common.MapWriter
MapWriter.EntryWriter -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty instanceNamedList(int sz) Creates a NamedList instance containing the "name,value" pairs contained in the Entry[].Creates a NamedList instance containing the "name,value" pairs contained in the Map. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a name/value pair to the end of the list.booleanIterates over the Map and sequentially adds its key/value pairsbooleanAppends the elements of the given NamedList to this one.asMap(int maxDepth) Deprecated.asShallowMap(boolean allowDps) Deprecated.useSimpleOrderedMapinstead of NamedList when a Map is required.voidclear()clone()Makes a shallow copy of the named list.booleanvoidforEach(BiConsumer<? super String, ? super T> action) Gets the value for the first instance of the specified name found.Deprecated.UseindexOf(String, int)thengetVal(int).Gets the values for the specified namegetBooleanArg(String name) Used for getting a boolean argument from a NamedList object.getName(int idx) The name of the pair at the specified List indexgetOrDefault(String name, T def) Likeget(String)but returns a default value if it would be null.getVal(int idx) The value of the pair at the specified List indexinthashCode()intScans the names of the list sequentially beginning at index 0 and returns the index of the first pair with the specified name.intScans the list sequentially beginning at the specified index and returns the index of the first pair with the specified name.iterator()Support the Iterable interfaceremove(int idx) Removes the name/value pair at the specified index.NOTE: this runs in linear time (it scans starting at the beginning of the list until it finds the first pair with the specified name).Removes and returns all values for the specified name.removeBooleanArg(String name) Used for getting a boolean argument from a NamedList object.removeConfigArgs(String name) Used for getting one or many arguments from NamedList objects that hold configuration parameters.voidModifies the name of the pair at the specified index.Modifies the value of the pair at the specified index.intsize()The total number of name/value pairsCreate SolrParams from NamedList.toString()voidWrites this object's entries out toew.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface org.apache.solr.common.NavigableObject
_forEachEntry, _forEachEntry, _forEachEntry, _get, _get, _getStr, _getStr, _size
-
Field Details
-
nvPairs
-
-
Constructor Details
-
NamedList
public NamedList()Creates an empty instance -
NamedList
public NamedList(int sz) -
NamedList
Creates a NamedList instance containing the "name,value" pairs contained in the Entry[].Modifying the contents of the Entry[] after calling this constructor may change the NamedList (in future versions of Solr), but this is not guaranteed and should not be relied upon. To modify the NamedList, refer to
add(String, Object)orremove(String).- Parameters:
nameValuePairs- the name value pairs
-
NamedList
Creates a NamedList instance containing the "name,value" pairs contained in the Map.Modifying the contents of the Map after calling this constructor may change the NamedList (in future versions of Solr), but this is not guaranteed and should not be relied upon. To modify the NamedList, refer to
add(String, Object)orremove(String).- Parameters:
nameValueMap- the name value pairs
-
-
Method Details
-
writeMap
Description copied from interface:MapWriterWrites this object's entries out toew.- Specified by:
writeMapin interfaceMapWriter- Throws:
IOException
-
size
public int size()The total number of name/value pairs -
getName
The name of the pair at the specified List index- Returns:
- null if no name exists
-
getVal
The value of the pair at the specified List index- Returns:
- may be null
-
add
Adds a name/value pair to the end of the list. -
setName
Modifies the name of the pair at the specified index. -
setVal
Modifies the value of the pair at the specified index.- Returns:
- the value that used to be at index
-
remove
Removes the name/value pair at the specified index.- Returns:
- the value at the index removed
-
indexOf
Scans the list sequentially beginning at the specified index and returns the index of the first pair with the specified name.- Parameters:
name- name to look for, may be nullstart- index to begin searching from- Returns:
- The index of the first matching pair, -1 if no match
-
indexOf
Scans the names of the list sequentially beginning at index 0 and returns the index of the first pair with the specified name.- See Also:
-
get
Gets the value for the first instance of the specified name found.NOTE: this runs in linear time (it scans starting at the beginning of the list until it finds the first pair with the specified name).
- Returns:
- null if not found or if the value stored was null.
- See Also:
-
getOrDefault
Likeget(String)but returns a default value if it would be null. -
get
Deprecated.UseindexOf(String, int)thengetVal(int).Gets the value for the first instance of the specified name found starting at the specified index.NOTE: this runs in linear time (it scans starting at the specified position until it finds the first pair with the specified name).
- Returns:
- null if not found or if the value stored was null.
- See Also:
-
getAll
Gets the values for the specified name- Parameters:
name- Name- Returns:
- List of values
-
toString
-
getImmutableCopy
-
asShallowMap
Deprecated.UseSimpleOrderedMapinstead. -
asShallowMap
Deprecated.useSimpleOrderedMapinstead of NamedList when a Map is required. -
asMap
-
toSolrParams
Create SolrParams from NamedList. Values must beString[]orList(with toString()-appropriate entries), or otherwise have a toString()-appropriate value. Nulls are retained as such in arrays/lists but otherwise will NPE. -
addAll
Iterates over the Map and sequentially adds its key/value pairs -
addAll
Appends the elements of the given NamedList to this one. -
clone
Makes a shallow copy of the named list. -
iterator
Support the Iterable interface -
remove
NOTE: this runs in linear time (it scans starting at the beginning of the list until it finds the first pair with the specified name). -
removeAll
Removes and returns all values for the specified name. Returns null if no matches found. This method will return all matching objects, regardless of data type. If you are parsing Solr config options, theremoveConfigArgs(String)orremoveBooleanArg(String)methods will probably work better.- Parameters:
name- Name- Returns:
- List of values
-
removeBooleanArg
Used for getting a boolean argument from a NamedList object. If the name is not present, returns null. If there is more than one value with that name, or if the value found is not a Boolean or a String, throws an exception. If there is only one value present, and it is a Boolean or a String, the value is removed and returned as a Boolean. If an exception is thrown, the NamedList is not modified. SeeremoveAll(String)andremoveConfigArgs(String)for additional ways of gathering configuration information from a NamedList.- Parameters:
name- The key to look up in the NamedList.- Returns:
- The boolean value found.
- Throws:
SolrException- If multiple values are found for the name or the value found is not a Boolean or a String.
-
getBooleanArg
Used for getting a boolean argument from a NamedList object. If the name is not present, returns null. If there is more than one value with that name, or if the value found is not a Boolean or a String, throws an exception. If there is only one value present, and it is a Boolean or a String, the value is returned as a Boolean. The NamedList is not modified. Seeremove(String),removeAll(String)andremoveConfigArgs(String)for additional ways of gathering configuration information from a NamedList.- Parameters:
name- The key to look up in the NamedList.- Returns:
- The boolean value found.
- Throws:
SolrException- If multiple values are found for the name or the value found is not a Boolean or a String.
-
removeConfigArgs
Used for getting one or many arguments from NamedList objects that hold configuration parameters. Finds all entries in the NamedList that match the given name. If they are all strings or arrays of strings, remove them from the NamedList and return the individual elements as aCollection. Parameter order will be preserved if the returned collection is handled as anArrayList. Throws SolrException if any of the values associated with the name are not strings or arrays of strings. If exception is thrown, the NamedList is not modified. Returns an empty collection if no matches found. If you need to remove and retrieve all matching items from the NamedList regardless of data type, useremoveAll(String)instead. TheremoveBooleanArg(String)method can be used for retrieving a boolean argument.- Parameters:
name- The key to look up in the NamedList.- Returns:
- A collection of the values found.
- Throws:
SolrException- If values are found for the input key that are not strings or arrays of strings.
-
clear
public void clear() -
hashCode
public int hashCode() -
equals
-
forEach
-
SimpleOrderedMapinstead.