How to merge Scala Lists
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的更多相关文章
- 【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 ...
- LeetCode第[21][23]题(Java):Merge Sorted Lists
题目:合并两个已排序链表 难度:Easy 题目内容: Merge two sorted linked lists and return it as a new list. The new list s ...
- 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 ...
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Merge k Sorted Lists——分治与堆排序(需要好好看)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 1 ...
- Beginning Scala study note(6) Scala Collections
Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...
- Scala详解---------数组、元组、映射
一.数组 1.定长数组 声明数组的两种形式: 声明指定长度的数组 val 数组名= new Array[类型](数组长度) 提供数组初始值的数组,无需new关键字 Scala声明数组时,需要带有Arr ...
- Leetcode: Merge k Sorted List
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 参 ...
- 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 ...
随机推荐
- UVA 357 Let Me Count The Ways(全然背包)
UVA 357 Let Me Count The Ways(全然背包) http://uva.onlinejudge.org/index.php?option=com_onlinejudge& ...
- java程序员认证考试题库
第一部分 基础知识练习 目标 本章对应于<学生指南>各章的内容分别提供了练习题集,包括: ● 第一章Java入门 ● 第二章数据类型和运算符 ● 第三章流程控制与数组 ● 第四章封 ...
- eclipse Failed to load the JNIshared library
eclipse Failed to load the JNIshared library CreateTime--2018年4月22日22:08:35 Author:Marydon 1.情景再现 ...
- 如何在MyEclipse中建立一个代理服务器
一.什么是 TCP/IP Monitor TCP/IP monitor 是可以监控在某个端口上通过 TCP/IP 协议传送的通信数据的一个工具软件. TCP/IP monitor 工具,通过一些配置, ...
- 如何使用jmeter来实现更大批量的并发的解决方案
近期在用JMeter进行负载测试的 时候,发现使用单台机器模拟测试超过比如500个进程的并发就有些力不从心或者说不能如实的反应实际情况,在执行的过程中,JMeter自身会自动关闭, 要解决这个问题,则 ...
- Ajax Control Toolkit 34个服务器端控件的使用
摘自:http://blog.csdn.net/yaoshuyun/article/details/2218633 1. Accordion[功能概述] Accordion可以让你设计多个panel ...
- HDUOJ----4006The kth great number(最小堆...)
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- Python接通图灵机器人
图灵机器人 图灵机器人特别low,问答水平并不高. import requests print("你好,我是图灵机器人") while 1: s = input() resp = ...
- pandas 的数据结构Series与DataFrame
pandas中有两个主要的数据结构:Series和DataFrame. [Series] Series是一个一维的类似的数组对象,它包含一个数组数据(任何numpy数据类型)和一个与数组关联的索引. ...
- JavaScript 中定义变量时有无var声明的区别
关于JavaScript中定义变量时有无var声明的区别 var a=5; //正确 a=5; //正确 在javascript中,以上两种方法都是定义变量的正确方法.微软的Script56.CHM中 ...