How to convert this Map to a List

1,132 views
Skip to first unread message

Cecil Westerhof

unread,
Mar 18, 2014, 4:18:33 PM3/18/14
to scala...@googlegroups.com
I have the following Map:
    Map('4' -> 1, 'A' -> 1, '5' -> 1, '2' -> 1, '3' -> 1)

I want to convert it to the following List:
    List("14", "1A", "15", "12", "13")

How would I do that?

--
Cecil Westerhof

Dennis Haupt

unread,
Mar 18, 2014, 4:20:37 PM3/18/14
to Cecil Westerhof, scala-user
untested and could surely be done more efficiently, but should work
toList.map(e => List(e._2, e._1).mkString)


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oliver Ruebenacker

unread,
Mar 18, 2014, 4:22:38 PM3/18/14
to Dennis Haupt, Cecil Westerhof, scala-user
On Tue, Mar 18, 2014 at 4:20 PM, Dennis Haupt <d.ha...@gmail.com> wrote:
untested and could surely be done more efficiently, but should work
toList.map(e => List(e._2, e._1).mkString)
 
toList.map(e => "" + e._2 + e._1)



2014-03-18 21:18 GMT+01:00 Cecil Westerhof <cldwes...@gmail.com>:

I have the following Map:
    Map('4' -> 1, 'A' -> 1, '5' -> 1, '2' -> 1, '3' -> 1)

I want to convert it to the following List:
    List("14", "1A", "15", "12", "13")

How would I do that?

--
Cecil Westerhof

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Be always grateful, but never satisfied.

Dennis Haupt

unread,
Mar 18, 2014, 4:28:05 PM3/18/14
to Oliver Ruebenacker, Cecil Westerhof, scala-user
i got that idea too, but i try to avoid the ""+ hack at all costs :|

data.toList.map(e => s"${e._2}${e._1}")

Cecil Westerhof

unread,
Mar 18, 2014, 4:35:13 PM3/18/14
to scala...@googlegroups.com
Should have gone to the list.

---------- Forwarded message ----------
From: Cecil Westerhof <cldwes...@gmail.com>
Date: 2014-03-18 21:34 GMT+01:00
Subject: Re: [scala-user] How to convert this Map to a List
To: Dennis Haupt <d.ha...@gmail.com>


2014-03-18 21:28 GMT+01:00 Dennis Haupt <d.ha...@gmail.com>:

i got that idea too, but i try to avoid the ""+ hack at all costs :|

data.toList.map(e => s"${e._2}${e._1}")

This gives a scala.collection.immutable.Iterable[String]

So I put a .toList after. In that way I can do thisList(1) and get "1A".




--
Cecil Westerhof



--
Cecil Westerhof

Alan Burlison

unread,
Mar 18, 2014, 4:42:29 PM3/18/14
to Cecil Westerhof, scala...@googlegroups.com
On 18/03/2014 20:18, Cecil Westerhof wrote:

> How would I do that?

Yet another wrinkle:

m.map({ case (a,b) => b.toString + a })

I think the answer to your question is "lots of ways" :-)

--
Alan Burlison
--

Lanny Ripple

unread,
Mar 21, 2014, 2:42:32 PM3/21/14
to scala...@googlegroups.com, Cecil Westerhof
m.map {case (a,b) => s"$b$a"}

$0.02USD

Dennis Haupt

unread,
Mar 21, 2014, 3:23:08 PM3/21/14
to Lanny Ripple, scala-user, Cecil Westerhof
me too, me too
m.map(_.swap.productIterator.mkString) 


--

Lanny Ripple

unread,
Mar 21, 2014, 3:24:46 PM3/21/14
to scala...@googlegroups.com, Lanny Ripple, Cecil Westerhof
Reply all
Reply to author
Forward
0 new messages