user2844511 user2844511 - 3 months ago 11
Scala Question

Type mismatch found java.util.List[String]:required List[String]

I am very new to scala. I have started using scala for my spark project. I am using some of java code. following line I am getting error.

case class docDisplay( id :String,name :String, session :String,
time :String, docguid: scala.collection.immutable.List[String]


)

Code for my ParseDocumentGuid which is the java class is as follow.

public static List<String> ParseGuid(String Data, String type,String Name) {
boolean validJson=checkValidJson(Data);
if(validJson==true)

{
try
{
JSONObject json = null;
json = new JSONObject (Data);
List<String> singleDocGuidList= new ArrayList<String>();
if(json!=null && json.getString("guid").equalsIgnoreCase("guid") )
{
singleDocGuidList.add(json.getString("guid"));
}
return singleDocGuidList;
}
catch(Exception e)
{
List<String> singleDocGuidList= new ArrayList<String>();
singleDocGuidList.add(e.getMessage());
return singleDocGuidList;
}
}
else
{
List<String> singleDocGuidList= new ArrayList<String>();
singleDocGuidList.add("unKnownDocumentGuid");
return singleDocGuidList;
}
}


Here is the code which is calling above method

def selectColumnsPerDocdisplayRows(row:Row):List[String]= {
docDisplay(
row.getAs[String]("id"),
row.getAs[String]("name"),
row.getAs[String]("session"),
row.getAs[String]("time"),
Utils.ParseDocumentGuid(row.getAs("Data"),Utils.DOCDISRELATED,row.getAs[String]("name"))
);
}


So I am getting error on my method def selectColumnsPerDocdisplayRows() saying

type mismatch; found : java.util.List[String] required: List[String]

Answer

Add the conversions:

import collection.JavaConverters._

and add .asScala to convert