Scala List FAQ: How do I merge a List in Scala?

NOTE: I wrote the solutions shown below a long time ago, and they are not optimal. I'll update this article when I have more time. The best approach is to prepend one List to the beginning of another List with the :: method.

There are at least three ways to merge/concatenate Scala List instances, as shown in the examples below.

1) The Scala List ::: method

First, you can merge two Scala lists using the ::: method of the List class, as demonstrated here at the Scala command prompt:

scala> val a = List(1,2,3)
a: List[Int] = List(1, 2, 3) scala> val b = List(4,5,6)
b: List[Int] = List(4, 5, 6) scala> val c = a ::: b
c: List[Int] = List(1, 2, 3, 4, 5, 6)

This operation is said to have O(n) speed, where n is the number of elements in the first List.

2) The Scala List concat method

You can also merge two Scala lists using the List class concat method:

scala> val a = List(1,2,3)
a: List[Int] = List(1, 2, 3) scala> val b = List(4,5,6)
b: List[Int] = List(4, 5, 6) scala> val c = List.concat(a, b)
c: List[Int] = List(1, 2, 3, 4, 5, 6)

3) The Scala List ++ method

You can also use the List ++ method to concatenate Scala Lists, as shown here:

scala> val a = List(1,2,3)
a: List[Int] = List(1, 2, 3) scala> val b = List(4,5,6)
b: List[Int] = List(4, 5, 6) scala> val c = a ++ b
c: List[Int] = List(1, 2, 3, 4, 5, 6)

I'll try to add more information on these three approaches as I learn about them, but for now, I just wanted to share these three approaches.

How to merge Scala Lists的更多相关文章

  1. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  2. LeetCode第[21][23]题(Java):Merge Sorted Lists

    题目:合并两个已排序链表 难度:Easy 题目内容: Merge two sorted linked lists and return it as a new list. The new list s ...

  3. Can you share some Scala List class examples?

    Scala List FAQ: Can you share some Scala List class examples? The Scala List class may be the most c ...

  4. LeetCode: Merge k Sorted Lists 解题报告

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  5. Merge k Sorted Lists——分治与堆排序(需要好好看)

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 1 ...

  6. Beginning Scala study note(6) Scala Collections

    Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...

  7. Scala详解---------数组、元组、映射

    一.数组 1.定长数组 声明数组的两种形式: 声明指定长度的数组 val 数组名= new Array[类型](数组长度) 提供数组初始值的数组,无需new关键字 Scala声明数组时,需要带有Arr ...

  8. Leetcode: Merge k Sorted List

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 参 ...

  9. How do I add elements to a Scala List?

    Scala List FAQ: How do I add elements to a Scala List? This is actually a trick question, because yo ...

随机推荐

  1. navicat 使用

    sql是操作数据库中数据的语句,在不同的数据库中会略有不同,如mysql,postgreSQL,oracle,sqlserver,sqlite等等,但是sql的基础select.insert.upda ...

  2. Eclipse 如何创建Web项目

      Eclipse 如何创建Web项目 CreateTime--2018年3月8日16:43:33 Author:Marydon 第一步: 右键-->New-->Dynamic Web P ...

  3. Word基础总结

    Word文本的操作 一.文 ◎Backspace(退格键) 删除光标以左的内容    ◎Delete (删除键)    删除光标以右的内容     #实话之前一直没在意,一直用backspace删除 ...

  4. js createElement appendChild createTextNode用法

    xml不支持innerHTML 1 <p id="bj">北京</p> <script type="text/javascript" ...

  5. Java并发编程,多线程[转]

    Java并发编程 转自:http://www.cnblogs.com/dolphin0520/category/602384.html 第一个例子(没有阻塞主线程,会先输出over): package ...

  6. 【js】indexOf()

    /** **位置方法indexOf()和lastIndexOf() **这两个方法都接收两个参数:要查找的项和(可选的)表示查找起点位置的索引 **indexOf()方法从数组的开头(位置0)开始向后 ...

  7. Qt WebView改造成 QML App

    这是去年的一个项目,虽然研究出来了,解了一时之需,但随后束之高阁.当时Qt的版本是4.8.现在整理如下: 把QT HTML5 APP改造成 QML App 方案 新建一个QML自定义控件,该控件包含Q ...

  8. Android 界面间传参数

    登陆页 //声明Intent对象,并启动 LoginActivity Activity Intent intent = new Intent(); intent.setClass(LoginActiv ...

  9. Error parsing XML: not well-formed (invalid token) 报错

    鼠标右键选择Source然后再选Format 就可以解决此问题

  10. 固定尺寸内存块的缓冲队列类及C++实现源代码

    -------------------------------------------------------------------------------- 标题: 固定尺寸内存块的缓冲队列类及实 ...