scala code prints out weird Strings

776 views
Skip to first unread message

Jimit Raithatha

unread,
Oct 4, 2015, 4:54:12 PM10/4/15
to scala-user
Hi All,

Below is a small snippet i am working on that takes a list and splits it on character character "h".

On purpose, I'm trying to return an RDD of lists in the below code because i want to explore differences between map() and flatMap(). However, when i run the below code:

val sentences = sc.parallelize(List("hello how","are you","doing sir"))
val words = sentences.map(x=>x.split("h"))
words.foreach(println)

I get weird output as seen below:

[Ljava.lang.String;@6479f067
[Ljava.lang.String;@1962dba
[Ljava.lang.String;@2a940f26

How do i print out the actual lists?

Jimit

Clint Gilbert

unread,
Oct 4, 2015, 8:55:45 PM10/4/15
to scala...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"[Ljava.lang.String;@6479f067" etc is the way the JVM prints out raw
arrays. Note that String.split() returns an array of Strings. If you
want the result of split() to have a nicer String representation, try
turning those to, say, Lists with .toList .

On 10/04/2015 04:54 PM, Jimit Raithatha wrote:
> Hi All,
>
> Below is a small snippet i am working on that takes a list and
> splits it on character character "h".
>
> On purpose, I'm trying to return an RDD of lists in the below code
> because i want to explore differences between map() and flatMap().
> However, when i run the below code:
>
> valsentences = sc.parallelize(List("hello how","are you","doing
> sir")) val words = sentences.map(x=>x.split("h"))
> words.foreach(println)
>
>
> I get weird output as seen below:
>
> [Ljava.lang.String;@6479f067 [Ljava.lang.String;@1962dba
> [Ljava.lang.String;@2a940f26
>
> How do i print out the actual lists?
>
> Jimit
>
> -- 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
> <mailto:scala-user+...@googlegroups.com>. For more options,
> visit https://groups.google.com/d/optout.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)

iEYEARECAAYFAlYRyokACgkQ0GFaTS4nYxs/wACffbujBMD4rGcWNvtwVEBjC2jS
u+EAnjtJmOrf7b4Ii6FgdPFARta7WmNN
=dXaF
-----END PGP SIGNATURE-----

martin odersky

unread,
Oct 5, 2015, 12:05:18 PM10/5/15
to Clint Gilbert, scala-user
On Mon, Oct 5, 2015 at 2:55 AM, Clint Gilbert
<clint_...@hms.harvard.edu> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> "[Ljava.lang.String;@6479f067" etc is the way the JVM prints out raw
> arrays. Note that String.split() returns an array of Strings. If you
> want the result of split() to have a nicer String representation, try
> turning those to, say, Lists with .toList .

Or, much more efficiently, use `deep`:

words.foreach(word => println(word.deep))

or, equivalently

for (word <- words) println(word.deep)

See: http://www.scala-lang.org/api/current/index.html#scala.Array

- Martin
> 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.



--
Martin Odersky
EPFL

Clint Gilbert

unread,
Oct 5, 2015, 12:24:20 PM10/5/15
to martin odersky, scala-user
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Cool, thanks! I never knew that method existed.
>> -- 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.
>
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIcBAEBAgAGBQJWEqQpAAoJEBnktRkJHUgsTaAP/i93G1UFYANdVNDxq+ApvMfz
bK2vq94Sgj3tEk34NxZbFlHgIjkGWS1Vten0yZVVftQW0/V9oUP73nwyWqIhpgsU
IqwgMJemtwyEWVGUlyHCNkkv4QkWiLV8xyjtfaQtMGIAOHH3MesGnc3qbV52GeFI
qJ/F66dUsnlWn/Whzgj/TvofX4VaaoYkCwbppY9jqt+oup0drLSUy/Ye9KU+RVxg
AlpPdkCuMFMYUfZ7SAtt2hDMEOW4G5MvEQd6CIfW2j/M2aO7QaPSA3PzA0TgKx+9
5ORch+dwJbnaTRCreP3Nn8etpdgFjl2Ar1uscoMPglQEDoMlztDRJI1PZfMUJoCP
DpSuN465X2HGXYW9ViKr8do+vwnc0jl0U3m+SD7h6WSOqCrk6bwuEIevLI2mHcFz
RQ2fDPBtflXRbb9NeXC9rWg1xAs3VaLC1qRTY/zoWixiZlzq1uM1VC1AYsIVgPpU
GzxnDwr2JjvKVLMA+FM86uA1HcPdhFzFYVnkHrUiKI2BsXEmNzfuFLBOUK1DgSJR
MuPyz/Uey2uFRlayLn9JKs85n+rTiyVaDqB7QS3zT6b3vfE78iIPKJfVfFdmcbLB
dGzgNyIu2L5lgPHFOwsVX1VnfewNCNu/VqmH30gKy9thW2tq8ru1Gfg+BRGOfMYx
tcHM7efZn+5SC53wC9Ve
=fEQG
-----END PGP SIGNATURE-----

Jimit Raithatha

unread,
Oct 10, 2015, 9:09:45 PM10/10/15
to scala-user, martin....@epfl.ch
Small update if it's of any interest:

deep() prints out Arrays whereas toList() prints out lists. 

Is there a reason why deep() would be more efficient?

Jimit
>>>> <mailto:scala-user+unsub...@googlegroups.com>. For more
Reply all
Reply to author
Forward
0 new messages