user4398985
user4398985

Reputation:

Unable to remove empty strings in scala list

Why none of these operations are working when I am trying to remove empty strings in my list?

This is my list:

(yuhindmklwm00409219,958193628,0305delete,2700)
(yuhindmklwm00409219,958193628,0305delete,800)
(yuhindmklwm00409219,959262446,0219delete,62)
()
(yuhindmklwm00437293,752013801,0220delete,2700)
()
()
()
(yuhindmklwm00437293,85382,0126delete,500)
()
(yuhindmklwm00437293,863056514,0326delete,-2700)
(yuhindmklwm00437293,863056514,0326delete,2700)
()
()
()
()
()
(yuhindmklwm00437293,85258,0313delete,1000)
(yuhindmklwm00437293,85012,0311delete,1000)
(yuhindmklwm00437293,85718,0311delete,2700)
()
()
()
()
()
()
()
(yuhindmklwm00437293,744622574,0322delete,90)
(yuhindmklwm00437293,83704,0215delete,17)
()
()
(yuhindmklwm00437293,85253,0331delete,-2700)
(yuhindmklwm00437293,85253,0331delete,2700)
()
()
()
()
()
()
()
()
(yuhindmklwm00437293,752013801,0305delete,2700)
(yuhindmklwm00437293,33165,0315delete,1000)
(yuhindmklwm00437293,85018,0319delete,100)
(yuhindmklwm00437293,85018,0219delete,100)
(yuhindmklwm00437293,85018,0118delete,100)
(yuhindmklwm00437293,90265,0312delete,6)
(yuhindmklwm00437293,02465,0312delete,25)
(yuhindmklwm00437293,857164939,0313delete,15)
(yuhindmklwm00437293,22102,0313delete,4)
(yuhindmklwm00437293,55423,0313delete,100)
(yuhindmklwm00437293,02465,0314delete,1)
(yuhindmklwm00437293,90265,0312delete,1)
(yuhindmklwm00437293,93108,0315delete,25)
(yuhindmklwm00437293,220432304,0315delete,35)
(yuhindmklwm00437293,701211570,0315delete,35)
(yuhindmklwm00437293,28801,0315delete,10)
(yuhindmklwm00437293,93108,0211delete,3)
(yuhindmklwm00437293,93108,02)

This is type of list: List[String] = List((yuhindmklwm00437293,93108,02)..........))

I tried filter:

outputList1.filter(_.isNotNull).size

and

outputList1.map(record => if (record.size>3) record) else None)

both these approaches are unable to remove empty strings. The size of the list remains same for both

Upvotes: 1

Views: 6166

Answers (2)

jwvh
jwvh

Reputation: 51271

outputList1.filter(_.isNotNull).size

There is no such library method isNotNull and if there were this isn't what you want anyway because an empty string isn't the same thing as a null.

Besides that, it looks like you might not be dealing with actual empty strings. The string "()" is not empty. It has 2 characters in it. Try outputList1.filter(_.length > 2) (or whatever length is your actual limit).

Upvotes: 2

tintin.92
tintin.92

Reputation: 33

Your first approach doesn't work because, as mentioned by jwvh, an empty string "" isn't null. Your code only filters out null strings.

Your second approach isn't giving you the expected result (i.e. expected length of result) because you are replacing the empty records with Nones. Replacing some items in a list with others, but not actually removing those items results in a list of the same size. None is an object, and can be one of the value of an Option type.

Some ways to achieve your desired result are:

  1. outputList1.filter(_.nonEmpty) Simple enough, filters out empty strings.

  2. outputList1.map(record => if (record.nonEmpty) Some(record)) else None).flatten A little more complex, this replaces every non empty record with Some(<record>) and every empty string with a None. The resultant List[Option[String]] (which is a mix of Somes and Nones, both Option types) can be easily flattened out using .flatten. This will extract the value from the Somes, and get rid of the Nones from the resultant list.

    1. outputList1.collect(case record if (record.nonEmpty) => record) Collect works similar to map, expect it doesn't throw an exception in case of a match error with it's partial function.

Upvotes: 2

Related Questions