A list is a collection which contains immutable data. List represents linked list in Scala. A List is immutable, if we need to create a list that is constantly changing, the preferred approach is to use a ListBuffer.
The Scala List class holds a sequenced, linear list of items. A List can be built up efficiently only from back to front. the ListBuffer object is convenient when we want to build a list from front to back. It supports efficient prepend and append operations.
Once we are done creating our list, call the
toList method. To convert the ListBuffer into a List, Time taken will be constant. To use ListBuffer,
scala.collection.mutable.ListBuffer class is imported, an instance of ListBuffer is created.
var name = new ListBuffer[datatype]() // empty buffer is created var name = new ListBuffer("class", "gfg", "geeksforgeeks")
In the above example, first, an empty buffer is created here datatype indicates the type of data such as integer, string. Then created a buffer with three elements, of type string.
Below operation can be performed on ListBuffer –
- By using L += e we can appends the element e in constant time.
- By using e +=: L we can prepends the element e in constant time.
- L.toList In constant time, It returns a list with the contents of the ListBuffer . We should not use the ListBuffer once changing it to a list.
Operations on ListBuffer
Creating instance of ListBuffer:
ListBuffer(GeeksForGeeks, gfg, Class)
Access element from ListBuffer:
Element is accessed same as list,
ListBuffer(i) is used to accessed ith index element of list.
Adding elements in ListBuffer:
- Add single element to the bufferListBuffer+=( element)
- Add two or more elements (method has a varargs parameter)ListBuffer+= (element1, element2, ..., elementN )
- Append one or more elements (uses a varargs parameter)ListBuffer.append( elem1, elem2, ... elemN)
ListBuffer(GeeksForGeeks, gfg, class, Scala, Article)
Deleting ListBuffer Elements:
- Remove one elementListBuffer-= (element)
- Remove multiple elementsListBuffer-= (elem1, elem2, ....., elemN)
Deleting ListBuffer Elements using
remove() method is used to delete one element by its position in the
ListBuffer, or a series of elements beginning at a starting position.
ListBuffer(gfg, class, Scala, Article) ListBuffer(gfg)
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala | Functions Call-by-Name
- Program to convert Java list to an iterator in Scala
- Scala Set &() method with example
- Scala | Type Inference
- Program to convert Java set of Shorts to an Indexed Sequence in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.