scala.collection.immutable

class List

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

sealed abstract class List[+A]
extends LinearSequence[A] with Product with TraversableClass[A, List] with LinearSequenceTemplate[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
Direct Known Subclasses:
Nil, ::

Method Summary
override def ++ [B >: A, That](that : Traversable[B])(implicit bf : BuilderFactory[B, That, List[A]]) : 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 : BuilderFactory[B, That, List[A]]) : That
Create a new list which contains all elements of this list followed by all elements of Iterator `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 : Companion[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 reverseMap [B](f : (A) => B) : List[B]
Apply a function to all the elements of the list, and return the reversed list of results. This is equivalent to a call to map followed by a call to reverse, but more efficient. !!! should we deprecate this? Why have reverseMap, but not filterMap or reverseFilter, say?
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 traversable object.
Methods inherited from Product
productElement (abstract), productArity (abstract), productIterator, productElements, productPrefix, canEqual
Methods inherited from LinearSequenceTemplate
length, apply, iterator, foreach, forall, exists, count, find, foldLeft, foldRight, reduceLeft, reduceRight, last, dropRight, sameElements, lengthCompare, isDefinedAt, segmentLength, indexWhere, lastIndexWhere, equals
Methods inherited from Sequence
hashCode
Methods inherited from SequenceTemplate
size, zip, zipAll, zipWithIndex, prefixLength, indexWhere, findIndexOf, indexOf, indexOf, lastIndexOf, lastIndexOf, lastIndexWhere, reverseIterator, reversedElements, startsWith, startsWith, endsWith, indexOfSeq, indexOfSeq, lastIndexOfSeq, lastIndexOfSeq, contains, union, diff, intersect, removeDuplicates, patch, padTo, toSequence, indices, view, view, toString, sortWith, findLastIndexOf, slice, equalsWith, containsSlice, projection
Methods inherited from PartialFunction
orElse, andThen
Methods inherited from Function1
compose
Methods inherited from IterableTemplate
elements, toIterable, first, firstOption, toSeq
Methods inherited from TraversableClass
newBuilder, genericBuilder, unzip, flatten, transpose
Methods inherited from TraversableTemplate
thisCollection, nonEmpty, hasDefiniteSize, map, flatMap, filter, filterMap, filterNot, remove, partition, groupBy, /:, :\, reduceLeftOption, reduceRightOption, headOption, lastOption, init, copyToBuffer, copyToArray, copyToArray, toArray, toSet, mkString, mkString, mkString, addString, addString, addString
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 : Companion[List]
The factory companion object that builds instances of class CC
Overrides
LinearSequence.companion, TraversableClass.companion

abstract def isEmpty : Boolean
Returns true if the list does not contain any elements.
Returns
true, iff the list is empty.
Overrides
TraversableClass.isEmpty, LinearSequenceTemplate.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
TraversableClass.head, LinearSequenceTemplate.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
LinearSequenceTemplate.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 reverseMap[B](f : (A) => B) : List[B]
Apply a function to all the elements of the list, and return the reversed list of results. This is equivalent to a call to map followed by a call to reverse, but more efficient. !!! should we deprecate this? Why have reverseMap, but not filterMap or reverseFilter, say?
Parameters
f - the function to apply to each elements.
Returns
the reversed list of results.

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 : BuilderFactory[B, That, List[A]]) : 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 : BuilderFactory[B, That, List[A]]) : 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
LinearSequenceTemplate.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
LinearSequenceTemplate.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.

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.

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
LinearSequenceTemplate.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 traversable 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 <(e1: a, e2: a) => Boolean, which should be true iff e1 is smaller than e2. !!! todo: move sorting to IterableTemplate

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