luni, 12 octombrie 2015

[OmniFaces utilities (2.0)] Convert a Set<E> to a List<E>


[OmniFaces utilities] The setToList() function converts a Set<E> to a List<E>. Useful when you want to iterate over a Set in for example <ui:repeat>.

Function:
Usage:

The of:setToList() is very useful for JSF 2.0/2.1 where <h:dataTable> (and <ui:repeat>) doesn't support Iterable (such as Set) and Map directly.  For example, via this method you can easily use #{fooBean.set} in <h:dataTable>:

HashSet<Players> dataHashSet = new HashSet<>();
TreeSet<Players> dataTreeSet = new TreeSet<>();
LinkedHashSet<Players> dataLinkedHashSet = new LinkedHashSet<>();  

Now, for JSF 2.1/2.0, you can do this:

<h:dataTable value="#{of:setToList(playersBean.dataHashSet)}" var="t">
 ...
</h:dataTable>

<h:dataTable value="#{of:setToList(playersBean.dataTreeSet)}" var="t">
 ...
</h:dataTable>

<h:dataTable value="#{of:setToList(playersBean.dataLinkedHashSet)}" var="t">
 ...
</h:dataTable>

Starting with JSF 2.2 the <h:dataTable> support Set directly, so you don't need of:setToList() anymore. You can simply write:

<h:dataTable value="#{playersBean.dataHashSet}" var="t">
 ...
</h:dataTable>

<h:dataTable value="#{playersBean.dataTreeSet}" var="t">
 ...
</h:dataTable>

<h:dataTable value="#{playersBean.dataLinkedHashSet}" var="t">
 ...
</h:dataTable>

Well, <ui:repeat> still cannot directly use Set in JSF 2.2, so you can use of:setToList() or, via EL 2.2, toArray() method. Check the below examples for dataHashSet:

//via of:setToList()
<ui:repeat value="#{of:setToList(playersBean.dataHashSet)}" var="t">
 ...
</ui:repeat>

//via toArray()
<ui:repeat value="#{playersBean.dataHashSet.toArray()}" var="t">
 ...

Niciun comentariu:

Trimiteți un comentariu