Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do you access first element in a Map using EL?

785023Apr 28 2011 — edited Apr 29 2011
Hi there,
I have a JavaBean that contains a java.util.Map data member with associated getter/setter.
I want to reference the value of the first key-value pair in the map using expression language.
I don't know/have access to the value's corresponding key in the map, so I can't just use something like:
<c:set var="myValue" value="${javaBean.map['firstEntryKey']}" />
...do something useful with myValue...
I just know that I want the first (only) value in the map.

All I've come up with is to iterate over the contents of the map, and determine if I've got the 'first' element, using LoopTagStatus.isFirst().
<c:forEach items="${javaBean.map}" var="mapEntry" varStatus="status">
    <c:if test="${status.first}" >
        <c:set var="myValue" value="${mapEntry.value}" />
        ...do something useful with myValue...
    </c:if>
</c:forEach>
but that seems a bit clunky.

Am I missing something here? Is there a more elegant way to do this?
Thanks for your help.
Regards,
Ken.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 27 2011
Added on Apr 28 2011
10 comments
3,588 views