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. linux kernel下输入输出console怎样实现

    近期工作在调试usb虚拟串口,让其作为kernel启动的调试串口,以及user空间的输入输出控制台. 利用这个机会,学习下printk怎样选择往哪个console输出以及user空间下控制台怎样选择. ...

  2. 除掉inline-block 间距

    1.现象: <!doctype html> <html lang="en"> <head> <meta charset="UTF ...

  3. ideaIU-15.0.2 注册码

    注册时选择 License server ,填 http://idea.lanyus.com ,然后点击 OK

  4. 解决Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4

    Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://uk.maven.o ...

  5. Openerp 7.0 附件存储位置

    我们知道对OpenERP中的每个内部对象(比如:业务伙伴,采购订单,销售订单,发货单,等等)我们都可以添加任意的附件,如图片,文档,视频等.那么这些附件在OpenERP内部是如何管理的呢? 默认情况下 ...

  6. exception is the version of xbean.jar correct

      CreateTime--2018年2月5日09:20:00 Author:Marydon 异常: is the version of xbean.jar correct 情景还原: 在使用Java ...

  7. maven Dynamic Web Module 3.0 requires Java 1.6 or newer

      maven Dynamic Web Module 3.0 requires Java 1.6 or newer CreateTime--2018年4月19日16:56:42 Author:Mary ...

  8. html中文乱码问题的解决

    当我试着用html写代码的时候,发现直接保存的文件用浏览器打开时中文显示是乱码的,所以我找了一些解决方法,可是原因不太明白,所以我也就不解释了,能够自己找找原因,以下提供解决方法: 在写的html的第 ...

  9. HDU 5358 First One(枚举)

    这道题假设依照表达式一个个来算肯定超时,下午时候想了一个O(nlogn*logn)的算法.可是t了.由于这道题卡的很紧几百个例子,必须nlogn的算法才干够ac 回到这道题,考虑log(sum(i,j ...

  10. 感谢CSDN赠送的图书和杂志(5月份)

    互联网的精神就是开放.就是分享.在分享的同一时候.我们也会收获到意外的回报. 近期.因为我在5月份发表了14篇博文,因此CSDN赠送了一本图书<软件系统架构>(本人自己选的)和一本< ...