score:59
In addition to what has been said before, you can use patch
function that replaces sub-sequences of a sequence:
scala> val list = List(1, 2, 3, 4)
list: List[Int] = List(1, 2, 3, 4)
scala> list.patch(2, Seq(5), 1) // replaces one element of the initial sequence
res0: List[Int] = List(1, 2, 5, 4)
scala> list.patch(2, Seq(5), 2) // replaces two elements of the initial sequence
res1: List[Int] = List(1, 2, 5)
scala> list.patch(2, Seq(5), 0) // adds a new element
res2: List[Int] = List(1, 2, 5, 3, 4)
score:0
following is a simple example of String replacement in scala List, you can do similar for other types of data
scala> val original: List[String] = List("a","b")
original: List[String] = List(a, b)
scala> val replace = original.map(x => if(x.equals("a")) "c" else x)
replace: List[String] = List(c, b)
score:1
If you do a lot of such replacements, it is better to use a muttable class or Array.
score:2
You can use map to generate a new list , like this :
@ list
res20: List[Int] = List(1, 2, 3, 4, 4, 5, 4)
@ list.map(e => if(e==4) 0 else e)
res21: List[Int] = List(1, 2, 3, 0, 0, 5, 0)
score:2
It can also be achieved using patch function as
scala> var l = List(11,20,24,31,35)
l: List[Int] = List(11, 20, 24, 31, 35)
scala> l.patch(2,List(27),1)
res35: List[Int] = List(11, 20, 27, 31, 35)
where 2 is the position where we are looking to add the value, List(27)
is the value we are adding to the list and 1 is the number of elements to be replaced from the original list.
score:12
You can use list.updated(2,5)
(which is a method on Seq
).
It's probably better to use a scala.collection.immutable.Vector
for this purpose, becuase updates on Vector
take (I think) constant time.
score:107
If you want to replace index 2, then
list.updated(2,5) // Gives 1 :: 2 :: 5 :: 4 :: Nil
If you want to find every place where there's a 2 and put a 5 in instead,
list.map { case 2 => 5; case x => x } // 1 :: 5 :: 3 :: 4 :: Nil
In both cases, you're not really "replacing", you're returning a new list that has a different element(s) at that (those) position(s).
Source: stackoverflow.com
Related Query
- Replace element in List with scala
- Scala grouping list into list tuples with one shared element
- Concatenate String to each element of a List in a Spark dataframe with Scala
- Scala replace an String with a List of Key/Values
- Scala - map function to replace negatives with previous number in list
- Replace null values in a list with another value in Scala
- How to deep compare each element with other elements in a list in scala (optional fileds)
- scala create new list by omitting the n element from existing list with tuples
- scala - Find a pair in a list only with first element value
- Scala - replace xml element with specific text
- Remove specific element on list with math logic in scala
- Combine one List element with second list list in scala
- How to find an element in a list based on a condition and return some data associated with that element in Scala
- Finding the nth element of a list in scala recursively with pattern matching
- Replacement some element with next or previous element in Scala List
- replace list element with another and return the new list
- Scala replace element pairs in list
- how to match of "n" list in SCALA every element with every element?
- How to generate a list with xml element as key and xml as value in scala
- Find an element in a List by comparing with another list in scala
- Format a string and replace %s with html element in scala play framework
- Find index locations by regex pattern and replace them with a list of indexes in Scala
- Select and remove element on list with many term in Scala
- How to merge a keySet of a Map with each List Element : Scala
- Appending an element to the end of a list in Scala
- How to create a list with the same element n-times?
- Scala check if element is present in a list
- Finding the index of an element in a list scala
- Add element to a list In Scala
- Convert Scala List to List with another type
More Query from same tag
- Default for a missing field when deserializing Json with Jerkson
- chain style + scala options
- Typesafe Config SBT Multi Module Error When Resolving Placeholder
- Heroku memory leak with Play2 scala
- Continuations and for comprehensions -- what's the incompatibility?
- How to convert all column of dataframe to numeric spark scala?
- is it possible to write a directive to match "any other query params"?
- How do you throttle Flow in the latest Akka (2.4.6)?
- How do I reject a snapshot during recovery?
- windows IntelliJ maven dependences error
- How to resolve GC overload limit exceeded in scala code
- spark foreachPartition, how to get an index of each partition?
- How To iterate List[Result] and return one Boolean/Result?
- Can I get a Scala case class definition from an Avro schema definition?
- How to implement parent-children model with Akka in Scala?
- How do I create a function that takes any Seq[T,Int] and returns the original type of the Seq.
- Why does using combinations give "java.io.NotSerializableException: scala.collection.TraversableOnce$FlattenOps$$anon$1"?
- to_timestamp with spark scala is returning null
- How do I load a avro schema in IntelliJ?
- Unit tests working well with maven but won't run when using them with IntelliJ
- JS Dictionary to Scala Map
- Using method with recoverWith
- Map for generic HList
- Case class difference with field names on matcher failure
- Scala package issue
- How do I get sbt to not try to compile a directory whose name ends in .scala as a Scala source file?
- Efficient way to return top 10% of unsorted RDD as another RDD in Spark?
- How to convert RDD[(String, Iterable[VertexId])] to DataFrame?
- How can Scala traits be stripped automatically during implicit search?
- What is the relationship between the sbt libraryDependency and the import statement in Scala?