Qualcuno conosce un buon metodo per attivare la seguente lista input nell'elenco di output desiderato di seguito?Non so come eseguire questa trasformazione Elenco in Scala in modo ottimale
La funzione Sto cercando di creare
def transformList(input:List[(String,String)]):List[(String,String)] = ???
ingresso
val inputList = List(
("class","testClass1"),
("class","testClass2"),
("id","testId1"),
("class","testClassRepeat"),
("class","testClassRepeat"),
("id","testId2"),
("href","testHref1")
)
desiderata uscita
List(
("class","testClass1 testClass2 testClassRepeat testClassRepeat"),
("id","testId1 testId2"),
("href","testHref1")
)
ho una soluzione, ma non credo che lo sto facendo in un modo buono/efficiente. La soluzione che ho attualmente sto usando è:
- creare una mappa mutevole vuoto
- loop attraverso l'inputList con un .foreach
- Spingendo valori chiave/basati sul inputList nella mappa mutevole. Poi aggiungendo ai valori di chiavi esistenti, se del caso (per esempio, ci sono 4 "classi" nel mio esempio inputList.)
Grazie, Phil
Usa 'sortWith',' 'groupBy' e mkString' - nessuna mutevolezza richiesto. –