Scala / Java Treemap sort order not preserved

1,167 views
Skip to first unread message

y b

unread,
Mar 29, 2012, 10:42:51 AM3/29/12
to play-fr...@googlegroups.com
Hello,

I'm new to PlayFramework. I'm currently trying to do something very simple.
I have a treemap with elements sorted by values.
When I get this treemap in my scala html page, the order is not preserved.
How could I force Scala to work with the exact same order than java ?
Or maybe I'm doing something wrong ?

The java code is ... well nothing special about it.

main.scala.html
@(title: String)(stuff: TreeMap[String, Integer])
...
 @for((key, value) <- stuff) {
                    <tr>
                        <td>@key</td>
                        <td>@value</td>
                    </tr>
                    }
...

Any idea ?

Thanks ahead,
yb

Eric

unread,
Apr 7, 2012, 5:35:58 AM4/7/12
to play-framework
I also have the same exact problem as you do but unfortunately have
not come up with a solution.

Eric

unread,
Apr 7, 2012, 6:50:13 AM4/7/12
to play-framework
In my example, I have a static method in an ebean model which
populates a java.util.TreeMap<Double, FooEntity> which is called by a
controller to be sent to the view whereby the view's signature is
@(myMap: Map[Double, FooEntity]). Also tried changing the signature to
@(myMap: TreeMap[Double, FooEntity]) to no avail.

Can confirm that the java.util.TreeMap iterates through the elements
in the correct order by printing it out to the log but the ordering is
completely incorrect after being passed to the view. Assuming there's
something funky going on with the wrapper class?

Tom Carchrae

unread,
Apr 7, 2012, 8:07:27 AM4/7/12
to play-fr...@googlegroups.com
just a wild guess, but what happens if you use java.util.TreeMap in
the template (replace the code "Treemap" with "java.util.TreeMap")

Tom

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

Eric

unread,
Apr 7, 2012, 8:15:17 AM4/7/12
to play-framework
Yep, tried doing an @import java.util.TreeMap but ended up getting a
completely different error. Couldn't even recompile after that.

Manimaran Selvan

unread,
Apr 7, 2012, 10:04:37 AM4/7/12
to play-fr...@googlegroups.com
Replying to the received email won't get posted here?

Well, Don't use @import, just at the first line, use @(title: String)(stuff: java.util.TreeMap[String, Integer]) 

Cheers,

Pascal Voitot Dev

unread,
Apr 7, 2012, 11:13:47 AM4/7/12
to play-fr...@googlegroups.com
I'm not sure to understand your problem.
Do you have an example of code of entering values from java treemap and then seeing different order in Scala?

Pascal


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/qZH1l0tKImkJ.

Guillaume Bort

unread,
Apr 8, 2012, 5:40:02 AM4/8/12
to play-fr...@googlegroups.com, play-fr...@googlegroups.com
I guess it is related to the Java->Scala conversion. Can you try to with:

stuff.asScala

(to convert is explicitely)

Eric

unread,
Apr 8, 2012, 8:40:59 AM4/8/12
to play-framework
Will post a concrete example of the problem when I get back to my
devbox on tuesday.

On Apr 8, 11:40 am, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> I guess it is related to the Java->Scala conversion. Can you try to with:
>
> stuff.asScala
>
> (to convert is explicitely)
>
> On 7 avr. 2012, at 17:13, Pascal Voitot Dev <pascal.voitot....@gmail.com> wrote:
>
>
>
>
>
>
>
> > I'm not sure to understand your problem.
> > Do you have an example of code of entering values from java treemap and then seeing different order in Scala?
>
> > Pascal
>
> > On Thu, Mar 29, 2012 at 4:42 PM, y b <ysbe...@gmail.com> wrote:
> > Hello,
>
> > I'm new to PlayFramework. I'm currently trying to do something very simple.
> > I have a treemap with elements sorted by values.
> > When I get this treemap in my scala html page, the order is not preserved.
> > How could I force Scala to work with the exact same order than java ?
> > Or maybe I'm doing something wrong ?
>
> > The java code is ... well nothing special about it.
>
> > main.scala.html
> > @(title: String)(stuff: TreeMap[String, Integer])
> > ...
> >  @for((key, value) <- stuff) {
> >                     <tr>
> >                         <td>@key</td>
> >                         <td>@value</td>
> >                     </tr>
> >                     }
> > ...
>
> > Any idea ?
>
> > Thanks ahead,
> > yb
> > --
> > You received this message because you are subscribed to the Google Groups "play-framework" group.
> > To view this discussion on the web visithttps://groups.google.com/d/msg/play-framework/-/qZH1l0tKImkJ.
> > To post to this group, send email to play-fr...@googlegroups.com.
> > To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/play-framework?hl=en.

Manimaran Selvan

unread,
Apr 7, 2012, 8:39:24 AM4/7/12
to play-fr...@googlegroups.com
No. Don't import it. Just at the first line, try to use @(map : java.util.Treemap)

Cheers,
Sent from BlackBerry® on Airtel
Message has been deleted

Sam Spycher

unread,
May 4, 2012, 10:34:19 AM5/4/12
to play-fr...@googlegroups.com
Yes, this seems to be related to java->scala conversion.

The following modification of the for loop is a workaround to the issue:

@for((property, value) <- SortedMap.empty[String, String] ++ properties) { ... }

(properties is a java.util.TreeMap<String, String>. Don't forget to import scala.collection.SortedMap)

Looking at JavaConverters, I get the impression that all Map objects are converted to scala.collection.mutable.Map, which does not have the sorting properties of TreeMap. I imagine the @for dynamic statement uses this or similar functionality when converting java collections, which is what causes the error.

Cheers,
Sam




On Sunday, April 8, 2012 11:40:02 AM UTC+2, Guillaume Bort wrote:
I guess it is related to the Java->Scala conversion. Can you try to with:

stuff.asScala

(to convert is explicitely)

On 7 avr. 2012, at 17:13, Pascal Voitot Dev wrote:

I'm not sure to understand your problem.
Do you have an example of code of entering values from java treemap and then seeing different order in Scala?

Pascal

On Thu, Mar 29, 2012 at 4:42 PM, y b  wrote:
Hello,

I'm new to PlayFramework. I'm currently trying to do something very simple.
I have a treemap with elements sorted by values.
When I get this treemap in my scala html page, the order is not preserved.
How could I force Scala to work with the exact same order than java ?
Or maybe I'm doing something wrong ?

The java code is ... well nothing special about it.

main.scala.html
@(title: String)(stuff: TreeMap[String, Integer])
...
 @for((key, value) <- stuff) {
                    <tr>
                        <td>@key</td>
                        <td>@value</td>
                    </tr>
                    }
...

Any idea ?

Thanks ahead,
yb

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/qZH1l0tKImkJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

On Sunday, April 8, 2012 11:40:02 AM UTC+2, Guillaume Bort wrote:
I guess it is related to the Java->Scala conversion. Can you try to with:

stuff.asScala

(to convert is explicitely)

On 7 avr. 2012, at 17:13, Pascal Voitot Dev  wrote:

I'm not sure to understand your problem.
Do you have an example of code of entering values from java treemap and then seeing different order in Scala?

Pascal

On Thu, Mar 29, 2012 at 4:42 PM, y b wrote:
Hello,

I'm new to PlayFramework. I'm currently trying to do something very simple.
I have a treemap with elements sorted by values.
When I get this treemap in my scala html page, the order is not preserved.
How could I force Scala to work with the exact same order than java ?
Or maybe I'm doing something wrong ?

The java code is ... well nothing special about it.

main.scala.html
@(title: String)(stuff: TreeMap[String, Integer])
...
 @for((key, value) <- stuff) {
                    <tr>
                        <td>@key</td>
                        <td>@value</td>
                    </tr>
                    }
...

Any idea ?

Thanks ahead,
yb

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/qZH1l0tKImkJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

On Sunday, April 8, 2012 11:40:02 AM UTC+2, Guillaume Bort wrote:
I guess it is related to the Java->Scala conversion. Can you try to with:

stuff.asScala

(to convert is explicitely)

On 7 avr. 2012, at 17:13, Pascal Voitot Dev  wrote:

I'm not sure to understand your problem.
Do you have an example of code of entering values from java treemap and then seeing different order in Scala?

Pascal

On Thu, Mar 29, 2012 at 4:42 PM, y b wrote:
Hello,

I'm new to PlayFramework. I'm currently trying to do something very simple.
I have a treemap with elements sorted by values.
When I get this treemap in my scala html page, the order is not preserved.
How could I force Scala to work with the exact same order than java ?
Or maybe I'm doing something wrong ?

The java code is ... well nothing special about it.

main.scala.html
@(title: String)(stuff: TreeMap[String, Integer])
...
 @for((key, value) <- stuff) {
                    <tr>
                        <td>@key</td>
                        <td>@value</td>
                    </tr>
                    }
...

Any idea ?

Thanks ahead,
yb

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/qZH1l0tKImkJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

On Sunday, April 8, 2012 11:40:02 AM UTC+2, Guillaume Bort wrote:
I guess it is related to the Java->Scala conversion. Can you try to with:

stuff.asScala

(to convert is explicitely)

On 7 avr. 2012, at 17:13, Pascal Voitot Dev wrote:

I'm not sure to understand your problem.
Do you have an example of code of entering values from java treemap and then seeing different order in Scala?

Pascal

On Thu, Mar 29, 2012 at 4:42 PM, y b wrote:
Hello,

I'm new to PlayFramework. I'm currently trying to do something very simple.
I have a treemap with elements sorted by values.
When I get this treemap in my scala html page, the order is not preserved.
How could I force Scala to work with the exact same order than java ?
Or maybe I'm doing something wrong ?

The java code is ... well nothing special about it.

main.scala.html
@(title: String)(stuff: TreeMap[String, Integer])
...
 @for((key, value) <- stuff) {
                    <tr>
                        <td>@key</td>
                        <td>@value</td>
                    </tr>
                    }
...

Any idea ?

Thanks ahead,
yb

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/qZH1l0tKImkJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

jon hanson

unread,
May 28, 2012, 5:06:28 PM5/28/12
to play-fr...@googlegroups.com
Hi,

Just wanted to point out that this is not limited to parameters which are Java types. I have a case where the parameter type is a Scala SortedMap, and I see the same problem. A Java map was involved in the creation of the SortedMap  (i.e. in the Scala code that calls the template), so I don't know if the original Java object has somehow remained intact. The original map was actually a Java TreeMap, which I then converted to a Scala TreeMap:

TreeMap(jm.toArray:_*)

and did a foldLeft operation over that map to build another Scala TreeMap. Unfortunately when I tried to create a cutdown sample to illustrate the problem, the problem doesn't occur.

Jon

jgatcher

unread,
Dec 28, 2012, 7:22:52 AM12/28/12
to play-fr...@googlegroups.com
I tried using the asScala method but that also didn't work.adding my Java TreeMap to the scala Sorted Map worked but is there an alternative solution.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

Ludovic Pierrat

unread,
Sep 21, 2013, 3:31:43 PM9/21/13
to play-fr...@googlegroups.com
worked for me: instead of TreeMap, use ImmutableMap (from Google Guava)

Ludovic

Ben McCann

unread,
Sep 23, 2013, 3:14:17 AM9/23/13
to play-fr...@googlegroups.com
Here's the best solution I've seen:


val users = TreeSet("foo", "bar", "zzz", "abc")    
@for(name <- usernames.iterator) {
  @name ,
}

-Ben
Reply all
Reply to author
Forward
0 new messages