[OmniFaces utilities] The
mapToList()
function converts a Map<K, V>
to a List<Map.Entry<K, V>>
. Useful when you want to iterate over a Map
in for example <ui:repeat>
. Each of the entries has the usual getKey()
and getValue()
methods.Function:
Usage:
The of:mapToList() is very useful for JSF 2.2/2.0/2.1 where <h:dataTable> (and <ui:repeat>) doesn't support Map directly. For example, via this method you can easily use #{fooBean.map} in <h:dataTable>:
HashMap<String,
Players> dataHashMap = new HashMap<>();
TreeMap<String, Players> dataTreeMap = new TreeMap<>();
LinkedHashMap<String, Players> dataLinkedHashMap = new LinkedHashMap<>();
TreeMap<String, Players> dataTreeMap = new TreeMap<>();
LinkedHashMap<String, Players> dataLinkedHashMap = new LinkedHashMap<>();
Now, for JSF
2.2/2.1/2.0, you can do this:
<h:dataTable
value="#{of:mapToList(playersBean.dataHashMap)}"
var="t">
#{t.key} #{t.value}
</h:dataTable>
<h:dataTable
value="#{of:mapToList(playersBean.dataTreeMap)}"
var="t">
#{t.key} #{t.value}
</h:dataTable>
<h:dataTable
value="#{of:mapToList(playersBean.dataLinkedHashMap)}"
var="t">
#{t.key} #{t.value}
</h:dataTable>
However, starting
with JSF 2.2, UIData
(e.g. <h:dataTable>) support
Set
directly, so you don't need of:mapToList() if you prefer entrySet() (you may want to consider keySet() and values() also):
<h:dataTable
value="#{playersBean.dataHashMap.entrySet()}" var="t">
#{t.key} #{t.value}
</h:dataTable>
<h:dataTable
value="#{playersBean.dataTreeMap.entrySet()}" var="t">
#{t.key} #{t.value}
</h:dataTable>
<h:dataTable
value="#{playersBean.dataLinkedHashMap.entrySet()}"
var="t">
#{t.key} #{t.value}
</h:dataTable>
When Set
is not directly supported, but, if EL 2.2 is available, then you can use toArray()
method, so you don't need of:mapToList():
<h:dataTable
value="#{playersBean.dataHashMap.entrySet().toArray()}"
var="t">
#{t.key} #{t.value}
</h:dataTable>
<h:dataTable
value="#{playersBean.dataTreeMap.entrySet().toArray()}"
var="t">
#{t.key} #{t.value}
</h:dataTable>
<h:dataTable
value="#{playersBean.dataLinkedHashMap.entrySet().toArray()}"
var="t">
#{t.key} #{t.value}
</h:dataTable>
Well, speaking
about <ui:repeat>,
you should know that Map is not supported directly in JSF 2.2/2.1/2.0. In such
cases, you can use of:mapToList():
<ui:repeat
value="#{of:mapToList(playersBean.dataHashMap)}"
var="t">
<h:outputText value="key:#{t.key}
value:#{t.value}" />
</ui:repeat>
<ui:repeat
value="#{of:mapToList(playersBean.dataTreeMap)}"
var="t">
<h:outputText value="key:#{t.key}
value:#{t.value}" />
</ui:repeat>
<ui:repeat
value="#{of:mapToList(playersBean.dataLinkedHashMap)}"
var="t">
<h:outputText value="key:#{t.key}
value:#{t.value}" />
</ui:repeat>
Starting
with JSF 2.2, UIData
(e.g. <h:dataTable>) support
Set
directly, but <ui:repeat>
doesn't! So, if EL 2.2 is available, you can use toArray() method. Below, you
can see four approaches exemplified on dataHashMap:
<ui:repeat
value="#{playersBean.dataHashMap.entrySet().toArray()}"
var="t">
<h:outputText value="key:#{t.key}
value:#{t.value}" />
</ui:repeat>
<ui:repeat
value="#{playersBean.dataHashMap.keySet().toArray()}"
var="t">
<h:outputText value="key:#{t}
value:#{playersBean.dataHashMap.get(t)}" />
</ui:repeat>
<ui:repeat
value="#{playersBean.dataHashMap.values().toArray()}" var="t">
<h:outputText value="#{t}" />
</ui:repeat>
<ui:repeat
value="#{playersBean.dataHashMap.entrySet()}" var="t">
<ui:repeat value="#{t.toArray()}"
var="q">
<h:outputText value="key:#{q.key}
value:#{q.value}" />
</ui:repeat>
</ui:repeat>
Read further: JSF 2.0-2.3 Progress of Iterable/Map support in UIData/UIRepeat (OmniFaces support via utility functions)
Read further: JSF 2.0-2.3 Progress of Iterable/Map support in UIData/UIRepeat (OmniFaces support via utility functions)
Niciun comentariu :
Trimiteți un comentariu