How to create list of list by grouping on the basis of one list element using scala? -
i have following list structure-
list(("localhost","esx","192.168.1.1","33.36","93.80", "vms can not added"), ("star","esx","192.168.1.2","62.61","47.20","vms can added"), ("mars","esx","192.168.1.3","27.41","47.38","vms can added), ("moon","esx","192.168.1.4","23.58","69.40", "vms can not added"))
i want following output -
list(("vms can added",list(("star","esx","192.168.1.2","62.61","47.20"), ("mars","esx","192.168.1.3","27.41","47.38"))), ("vms can not added",list(("localhost","esx","192.168.1.1","33.36","93.80"), ("moon","esx","192.168.1.4","23.58","69.40"))))
how above output using scala???
the first part of question quite easy
val l=list( ("localhost","esx","192.168.1.1","33.36","93.80", "vms can not added"), ("star","esx","192.168.1.2","62.61","47.20","vms can added"), ("mars","esx","192.168.1.3","27.41","47.38","vms can added"), ("moon","esx","192.168.1.4","23.58","69.40", "vms can not added")) l.groupby(_._6).toseq
which leads result:
arraybuffer( (vms can not added, list( (localhost,esx,192.168.1.1,33.36,93.80,vms can not added), (moon,esx,192.168.1.4,23.58,69.40,vms can not added))), (vms can added, list( (star,esx,192.168.1.2,62.61,47.20,vms can added), (mars,esx,192.168.1.3,27.41,47.38,vms can added))))
this quite efficient, because changes containing structures, leaves values intact.
if want change values, can quite easily, using shapeless library:
import shapeless._ import syntax.std.tuple._ l.groupby(_._6).mapvalues(_.map(_.take(5))).tolist
which gives this:
list( (vms can not added, list( (localhost,esx,192.168.1.1,33.36,93.80), (moon esx,192.168.1.4,23.58,69.40))), (vms can added, list( (star,esx,192.168.1.2,62.61,47.20), (mars,esx,192.168.1.3,27.41,47.38))))
but change more values , hence pressure on garbage collector higher. should this, when additional value in each tuple problem.
remark:
intellij 14.0.1 gets hickup shapeless expression above. highlights line erroneous, compiles (and runs) quite fine.
Comments
Post a Comment