0
   val value = Array["id","sd","cd"]  -- List of columns 
   val cols_list = Array["cd","id","tm","no","in","ts","nm"] -  -- List of columns 

i want a list with columns not in cols_list.

code i tried as below :

     val newcol = for (x <- cols_list if x.toString.toUpperCase() not in value )

it's throwing error as value not is not a member of String. is there a way that we can achieve? Please suggest.

2
  • val finallist = cols_list.filterNot(value.contains(_)) you can use Commented Apr 24, 2020 at 2:39
  • 1
    "not in " ... Scala is not Python. I'm also wondering why you added tags for "Apache Spark"? What is the relation to it? Commented Apr 24, 2020 at 3:06

2 Answers 2

2

simplest is filterNot method (Apart from diff) that returns all elements from a list for which your function returns false.

val value = Array("id","sd","cd")  // List of columns
     val cols_list = Array("cd","id","tm","no","in","ts","nm")

     val finallist = cols_list.filterNot(value.contains(_)) //cols_list.par.filterNot also you can use
     println(finallist.mkString(" "))
 }

Result : tm no in ts nm


How it works...

filter creates a collection with those elements that do not satisfy the predicate p and discarding the rest. This is collection level and will work for all Collections API in scala signature :

  def filterNot(p: (A) => Boolean): Collection[A]

enter image description here

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

1 Comment

@ Ram Ghadiyaram. Thanks for your help. I need to convert value,col_list to upper case and then get the final list. Also i don't want to use map operations. Since map operation will take longer time and write everything to a single partition.
0

use .diff to get list of columns not in cols_list

val value = Array("id","sd","cd")
val cols_list = Array("cd","id","tm","no","in","ts","nm")
value.diff(cols_list)
//Array[String] = Array(sd)

//case insensitive 
value.map(x => x.toUpperCase).diff(cols_list.map(x => x.toUpperCase)).map(x => x.toLowerCase)
//Array[String] = Array(sd)

UPDATE:

cols_list.diff(value)
//Array[String] = Array(tm, no, in, ts, nm)

cols_list.map(x => x.toUpperCase).diff(value.map(x => x.toUpperCase)).map(x => x.toLowerCase)
//Array[String] = Array(tm, no, in, ts, nm)

cols_list.map(x => x.toUpperCase).diff(value.map(x => x.toUpperCase)).map(x => "\"a." + x.toLowerCase + "\"").mkString(",")
//String = "a.tm","a.no","a.in","a.ts","a.nm"

5 Comments

Thank you Shu for your prompt response. I tried but i am not seeing the expected output. I am expecting the values not in value should be displayed as below. "tm","no","in","ts","nm"
Change this - value.diff(cols_list) to cols_list.diff(value) you will get above values.
@sparkscala, Please check the updated answer, we just need to inter change cols_list and value..!
Actually i missed one thing. I want to add something like below for the output. "a.tm","a.no","a.in","a.ts","a.nm" .Any help is appreciated.
@sparkscala, use cols_list.map(x => x.toUpperCase).diff(value.map(x => x.toUpperCase)).map(x => "\"a." + x.toLowerCase + "\"").mkString(",") if the answer helped Click on Upvote and Accept!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.