Java Collection Framework Objects
Entries can also be read as Java Collection Framework objects.
The Type,Type...
arguments allow you to specify the component types for Maps
and Collections
.
List<T>
class arguments can be followed by zero or one arguments representing the entry types.
Map
class arguments can be followed by zero or two arguments representing the key and value types.
The arguments can be chained to produce any data structure consisting of Maps
, Collections
, or POJOs.
Examples are shown below:
Method Call | Produces |
---|---|
to(List.class) | List<?> |
to(LinkedList.class) | LinkedList<?> |
to(HashSet.class, Integer.class) | HashSet<Integer> |
to(Map.class) | Map<?,?> |
to(HashMap.class) | HashMap<?,?> |
to(LinkedHashMap.class, String.class, MyBean.class) | LinkedHashMap<String,MyBean> |
to(HashMap.class, Integer.class, ArrayList.class, MyBean[].class) | HashMap<Integer,ArrayList<MyBean[]>> |
Example
addresses =
[
{
street: '123 Main Street',
city: 'Anywhere',
state: 'NY',
zip: 12345
},
{
street: '456 Main Street',
city: 'Anywhere',
state: 'NY',
zip: 12345
}
]
List addresses = config.get("addresses").as(ArrayList.class, Address.class).orElse(null);
Oftentimes, it might be useful to parse into the JsonList and JsonMap classes that provide the various convenience methods for working with JSON-like data structures:
JsonMap map = config.get("key1").asMap().orElse(null);
JsonList list = config.get("key2").asList().orElse(null);