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. Python3.2官方文档翻译--标准库概览(一)

    7.1 操作系统接口 Os模块提供主要很多与操作系统交互的函数. >>> import os >>> os.getcwd() # Return the curren ...

  2. Java动态加载属性文件.properties

    当我们使用如下语句加载.properties时: ClassLoader classLoader = this.getClass().getClassLoader(); Properties prop ...

  3. [C#]记录程序耗时的方法【转发】

    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); // H ...

  4. SettingsSVNPlugin

      迁移时间:2017年5月20日11:24:50CreateTime--2016年9月18日17:53:20Author:Marydonmyeclipse/eclipse中配置svn插件参考链接:h ...

  5. JDBC实例--通过连接工具类DBUtil +配置文件来获取连接数据库,方便又快捷

    根据前面的连接方法,还有缺点就是,如果人家要换数据库,还得改动源代码,然后还要编译什么的.这样客户修改也不容易. 做法:我们写一个配置文件,把该数据写在配置文件上面,然后通过类来加载改文件,然后读取相 ...

  6. aop注解 自定义切面的注解写法

    spring.xml中 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  7. C#抽象方法

    在覆盖父类的方法时,必须显式说明是在覆盖父类方法,否则相当于没有覆盖. class Animal { public void run() { Console.WriteLine("anima ...

  8. android离线地图源码

    最近一直在玩Android手机,当然也忘不了在这个平台下搞些和地图相关的东西. Android手机自带了Google的地图软件,不过原来不支持离线浏览,所以很费流量,5.0版本以后可以支持离线浏览,需 ...

  9. android开发学习之ViewPager滑动事件讲解

    android ViewPager滑动事件讲解 今天在做项目的时候,由于要处理viewPager页面滑动的事件,所以对其进行了一个小小的研究: 首先ViewPager在处理滑动事件的时候要用到OnPa ...

  10. java JAXB + STAX(是一种针对XML的流式拉分析API)读取xml

    JDK1.5需要添加jar包,1.6以后就不需要了<dependency> <groupId>stax</groupId> <artifactId>st ...