1

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

7
  • 1
    outputList1.filter(_.nonEmpty) An empty String, i.e."", is not the same thing as null, and None is not the same type as String. Commented Feb 13, 2018 at 7:05
  • Hi @jwvh I tried null didn't work for me, outputList1.filter(_ != null).size gives the same size Commented Feb 13, 2018 at 7:10
  • 1
    I just commented that null is not the same as "" so a null test won't find empty strings. Commented Feb 13, 2018 at 7:12
  • Yes, but I am unable to understand even this is not working outputList1.map(record => if (record.size>3) record else None) since I am just ignoring any string less than 3 Commented Feb 13, 2018 at 7:27
  • 1
    None is not the same type as String. The result of that would be a mixed collection of Strings (greater than 3 characters) and Nones, which is not what you want. Try filter(_.length > 3). Commented Feb 13, 2018 at 7:33

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Are the empty strings literally ()? If that's the case, replace record.nonEmpty with record.length > 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.