Today we will learn some knowledge of the internal source code of the Scala list method.
First, take the method. The Take method is the first n elements of the list that start at the beginning of the second element. For example, List.take (3), which is the first 3 elements of the list, returns a list of the top 3 elements of the list. The principle is to take the head of the list in turn, add the head to a temporary listbuffer, and then take the head of the list of tail. Until you have taken enough of the numbers. Finally, the temporary listbuffer is converted to list by the ToList method.
By the way, look at the ToList method. The ToList method is to convert the Listbuffer into a list. This conversion does not copy all of the elements in the Listbuffer all over, but only the last element of the operation. And it's not going to take much time, it's a very fast thing to do.
Finally, let's take a look at Listbuffer's + = method. The + = method is to append the list to the element. So after we've tolist, we're not going to do any more additions, which will copy the list, consuming time and resources.
Share more of the Scala resources:
Baidu Cloud Disk: http://pan.baidu.com/s/1gd7133t
Micro Cloud Disk: http://share.weiyun.com/047efd6cc76d6c0cb21605cfaa88c416
360 Cloud Disk: Http://yunpan.cn/cQN9gvcKXe26M (extract code: 13CD)
Information from DT Big Data Dream Factory public account: Dt_spark
Follow the account for more information about Scala learning
83rd: The implementation of the list in Scala Insider source code disclosure