Reputation: 158
I have following list structure -
List(List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List({"esx":"192.168.20.52","vm":" naa.60a9800042704577762b45634476337a ","datastore":"","vNic":"","portGroupVLan":"","vSwitch":"","physicalNic":"","lunName":"lun_30102014_101347)","writeIops":44998,"readIops":1635,"latency":47008,"serialNumber":"BpEwv+EcDv3z","usedSize":0,"totalSize":4,"availableSize":4,"throughput":null}, (), ()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List((), (), ()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List((), (), ()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List((), (), ()))
I want to remove all empty lists from above . I want output as -
List({"esx":"192.168.20.52","vm":" naa.60a9800042704577762b45634476337a ","datastore":"","vNic":"","portGroupVLan":"","vSwitch":"","physicalNic":"","lunName":"lun_30102014_101347)","writeIops":44998,"readIops":1635,"latency":47008,"serialNumber":"BpEwv+EcDv3z","usedSize":0,"totalSize":4,"availableSize":4,"throughput":null})
How do I get above output by using scala??
Upvotes: 0
Views: 3605
Reputation: 4652
If your input is like that, i.e. you only have list(list(list())) depth, a double call to flatten will solve it.
val x = yourList
x.flatten.flatten
if your list has empty lists at various depths, you need to constantly flatten until you reach a fixed point:
val x = yourList
var y = x.flatten
var z = y.flatten
while (y != z) {
y = z
z = z.flatten
y
Hope that helps :)
Upvotes: 2