scala.collection.immutable

class List

[source: scala/collection/immutable/List.scala]

sealed abstract class List[+A]
extends LinearSeq[A] with Product with GenericTraversableTemplate[A, List] with LinearSeqLike[A, List[A]]
A class representing an ordered collection of elements of type a. This class comes with two implementing case classes scala.Nil and scala.:: that implement the abstract members isEmpty, head and tail.
Author
Martin Odersky and others
Version
2.8
Since
2.8
Direct Known Subclasses:
Nil, ::

Method Summary
override def ++ [B >: A, That](that : Iterator[B])(implicit bf : CanBuildFrom[List[A], B, That]) : That
Create a new list which contains all elements of this list followed by all elements of Iterator `that'
override def ++ [B >: A, That](that : Traversable[B])(implicit bf : CanBuildFrom[List[A], B, That]) : That
Create a new list which contains all elements of this list followed by all elements of Traversable `that'
def - [B >: A](x : B) : List[B]
Computes the difference between this list and the given object x.
def -- [B >: A](that : List[B]) : List[B]
Computes the difference between this list and the given list that.
def :: [B >: A](x : B) : List[B]
def ::: [B >: A](prefix : List[B]) : List[B]
override def companion : GenericCompanion[List]
The factory companion object that builds instances of class CC
override def drop (n : Int) : List[A]
Returns the list without its n first elements. If this list has less than n elements, the empty list is returned.
override def dropWhile (p : (A) => Boolean) : List[A]
Returns the longest suffix of this list whose first element does not satisfy the predicate p.
abstract def head : A
Returns this first element of the list.
abstract def isEmpty : Boolean
Returns true if the list does not contain any elements.
def mapConserve [B >: A](f : (A) => B) : List[B]
Like xs map f, but returns xs unchanged if function f maps all elements to themselves (wrt ==).
override def reverse : List[A]
A list consisting of all elements of this list in reverse order.
def reverse_::: [B >: A](prefix : List[B]) : List[B]
Reverse the given prefix and append the current list to that. This function is equivalent to an application of reverse on the prefix followed by a call to :::, but is more efficient.
override def slice (start : Int, end : Int) : List[A]
Returns the list with elements belonging to the given index range.
def sort (lt : (A, A) => Boolean) : List[A]
override def span (p : (A) => Boolean) : (List[A], List[A])
Returns the longest prefix of the list whose elements all satisfy the given predicate, and the rest of the list.
override def splitAt (n : Int) : (List[A], List[A])
Split the list at a given point and return the two parts thus created.
override def stringPrefix : java.lang.String
Defines the prefix of this object's toString representation.
abstract def tail : List[A]
Returns this list without its first element.
override def take (n : Int) : List[A]
Returns the n first elements of this list, or else the whole list, if it has less than n elements.
override def takeRight (n : Int) : List[A]
Returns the rightmost n elements from this list.
override def takeWhile (p : (A) => Boolean) : List[A]
Returns the longest prefix of this list whose elements satisfy the predicate p.
override def toList : List[A]
Overrides the method in Iterable for efficiency.
override def toStream : Stream[A]
Returns a stream with all elements in this iterable object.
Methods inherited from Product
productElement (abstract), productArity (abstract), productIterator, productElements, productPrefix
Methods inherited from LinearSeqLike
thisCollection, toCollection, length, apply, iterator, foreach, forall, exists, count, find, foldLeft, foldRight, reduceLeft, reduceRight, last, dropRight, sameElements, lengthCompare, isDefinedAt, segmentLength, indexWhere, lastIndexWhere
Methods inherited from SeqLike
size, prefixLength, indexWhere, findIndexOf, indexOf, indexOf, lastIndexOf, lastIndexOf, lastIndexWhere, reverseMap, reverseIterator, reversedElements, startsWith, startsWith, endsWith, indexOfSeq, indexOfSeq, lastIndexOfSeq, lastIndexOfSeq, contains, union, diff, intersect, removeDuplicates, patch, updated, +:, :+, padTo, sortWith, sortWith, sortBy, toSeq, indices, view, view, hashCode, equals, toString, findLastIndexOf, equalsWith, containsSlice, projection
Methods inherited from PartialFunction
orElse, andThen
Methods inherited from Function1
compose
Methods inherited from IterableLike
elements, toIterable, copyToArray, zip, zipAll, zipWithIndex, canEqual, first, firstOption
Methods inherited from GenericTraversableTemplate
newBuilder, genericBuilder, unzip, flatten, transpose
Methods inherited from TraversableLike
repr, nonEmpty, hasDefiniteSize, map, flatMap, filter, filterNot, partialMap, remove, partition, groupBy, /:, :\, reduceLeftOption, reduceRightOption, sum, product, min, max, headOption, lastOption, init, copyToBuffer, copyToArray, toArray, toIndexedSeq, toSet, mkString, mkString, mkString, addString, addString, addString, withFilter
Methods inherited from AnyRef
getClass, clone, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
override def companion : GenericCompanion[List]
The factory companion object that builds instances of class CC
Overrides
LinearSeq.companion, GenericTraversableTemplate.companion

abstract def isEmpty : Boolean
Returns true if the list does not contain any elements.
Returns
true, iff the list is empty.
Overrides
GenericTraversableTemplate.isEmpty, LinearSeqLike.isEmpty

abstract def head : A
Returns this first element of the list.
Returns
the first element of this list.
Throws
Predef.NoSuchElementException - if the list is empty.
Overrides
GenericTraversableTemplate.head, LinearSeqLike.head

abstract def tail : List[A]
Returns this list without its first element.
Returns
this list without its first element.
Throws
Predef.NoSuchElementException - if the list is empty.
Overrides
LinearSeqLike.tail

def ::[B >: A](x : B) : List[B]

Add an element x at the beginning of this list.

Parameters
x - the element to prepend.
Returns
the list with x added at the beginning.
Examples
1 :: List(2, 3) = List(2, 3).::(1) = List(1, 2, 3)

def :::[B >: A](prefix : List[B]) : List[B]

Returns a list resulting from the concatenation of the given list prefix and this list.

Parameters
prefix - the list to concatenate at the beginning of this list.
Returns
the concatenation of the two lists.
Examples
List(1, 2) ::: List(3, 4) = List(3, 4).:::(List(1, 2)) = List(1, 2, 3, 4)

def reverse_:::[B >: A](prefix : List[B]) : List[B]
Reverse the given prefix and append the current list to that. This function is equivalent to an application of reverse on the prefix followed by a call to :::, but is more efficient.
Parameters
prefix - the prefix to reverse and then prepend
Returns
the concatenation of the reversed prefix and the current list.

def mapConserve[B >: A](f : (A) => B) : List[B]
Like xs map f, but returns xs unchanged if function f maps all elements to themselves (wrt ==).
Notes
Unlike `map`, `mapConserve` is not tail-recursive.

override def ++[B >: A, That](that : Traversable[B])(implicit bf : CanBuildFrom[List[A], B, That]) : That
Create a new list which contains all elements of this list followed by all elements of Traversable `that'

override def ++[B >: A, That](that : Iterator[B])(implicit bf : CanBuildFrom[List[A], B, That]) : That
Create a new list which contains all elements of this list followed by all elements of Iterator `that'

override def toList : List[A]
Overrides the method in Iterable for efficiency.
Returns
the list itself

override def take(n : Int) : List[A]
Returns the n first elements of this list, or else the whole list, if it has less than n elements.
Parameters
n - the number of elements to take.
Returns
the n first elements of this list.
Overrides
LinearSeqLike.take

override def drop(n : Int) : List[A]
Returns the list without its n first elements. If this list has less than n elements, the empty list is returned.
Parameters
n - the number of elements to drop.
Returns
the list without its n first elements.
Overrides
LinearSeqLike.drop

override def slice(start : Int, end : Int) : List[A]
Returns the list with elements belonging to the given index range.
Parameters
start - the start position of the list slice.
end - the end position (exclusive) of the list slice.
Returns
the list with elements belonging to the given index range.
Overrides
LinearSeqLike.slice

override def takeRight(n : Int) : List[A]
Returns the rightmost n elements from this list.
Parameters
n - the number of elements to take
Returns
the suffix of length n of the list

override def splitAt(n : Int) : (List[A], List[A])
Split the list at a given point and return the two parts thus created.
Parameters
n - the position at which to split
Returns
a pair of lists composed of the first n elements, and the other elements.

override def takeWhile(p : (A) => Boolean) : List[A]
Returns the longest prefix of this list whose elements satisfy the predicate p.
Parameters
p - the test predicate.
Returns
the longest prefix of this list whose elements satisfy the predicate p.
Overrides
LinearSeqLike.takeWhile

override def dropWhile(p : (A) => Boolean) : List[A]
Returns the longest suffix of this list whose first element does not satisfy the predicate p.
Parameters
p - the test predicate.
Returns
the longest suffix of the list whose first element does not satisfy the predicate p.

override def span(p : (A) => Boolean) : (List[A], List[A])
Returns the longest prefix of the list whose elements all satisfy the given predicate, and the rest of the list.
Parameters
p - the test predicate
Returns
a pair consisting of the longest prefix of the list whose elements all satisfy p, and the rest of the list.
Overrides
LinearSeqLike.span

override def reverse : List[A]
A list consisting of all elements of this list in reverse order.

override def stringPrefix : java.lang.String
Defines the prefix of this object's toString representation.

override def toStream : Stream[A]
Returns a stream with all elements in this iterable object.

@deprecated("use `diff' instead")

def --[B >: A](that : List[B]) : List[B]
Computes the difference between this list and the given list that.
Parameters
that - the list of elements to remove from this list.
Returns
this list without the elements of the given list that.

@deprecated("use `diff' instead")

def -[B >: A](x : B) : List[B]
Computes the difference between this list and the given object x.
Parameters
x - the object to remove from this list.
Returns
this list without occurrences of the given object x.

@deprecated("use `sortWith' instead")

def sort(lt : (A, A) => Boolean) : List[A]

Sort the list according to the comparison function lt(e1: a, e2: a) => Boolean, which should be true iff e1 precedes e2 in the desired ordering. !!! todo: move sorting to IterableLike

Parameters
lt - the comparison function
Returns
a list sorted according to the comparison function lt(e1: a, e2: a) => Boolean.
Examples
      List("Steve", "Tom", "John", "Bob")
        .sort((e1, e2) => (e1 compareTo e2) < 0) =
      List("Bob", "John", "Steve", "Tom")