Groupby - collection processing
Groupby - collection processing
Iterator and Iterable have most of the most useful methods when dealing with collections. Fold, Map, Filter are probably the most common. But other very useful methods include grouped/groupBy, sliding, find, forall, foreach, and many more. I want to cover Iterable's groupBy method in this topic.
This is a Scala 2.8 and later method. It is similar to partition in that it allows the collection to be divided (or partitioned). Partition takes a method with returns a boolean and partitions the collection into two depending on a result. GroupBy takes a function that returns an object and returns a Map with the key being the return value. This allows an arbitrary number of partitions to be made from the collection.
Here is the method signature:
def groupBy[K](f : (A) => K) : Map[K, This]
A bit of context is require to understand the three Type parameters A, K and This. This method is defined in a super class of collections called TraversableLike (I will briefly discuss this in the next topic.) TraversableLike takes two type parameters: the type of the collection and the type contained in the collection. Therefore in this method definition, 'This' refers to the collection type (List for example) and A refers to contained type (perhaps Int). Finally K refers to the type returned by the function and are the keys of the groups formed by the method.
scala> val groups = (1 to 20).toList groupBy {
case i if(i<5) => "g1"
case i if(i<10) => "g2"
case i if(i<15) => "g3"
case _ => "g4"
}
res4: scala.collection.Map[java.lang.String,List[Int]] = Map(g1 -> List(1, 2, 3, 4),
g2 -> List(5, 6, 7, 8, 9), g3 -> List(10, 11, 12, 13, 14), g4 -> List(15, 16, 17, 18, 19, 20))
scala> val mods = (1 to 20).toList groupBy ( _ % 4 )
mods: scala.collection.Map[Int,List[Int]] = Map(1 -> List(1, 5, 9, 13, 17), 2 -> List(2, 6, 10, 14, 18),
3 -> List(3, 7,11, 15, 19), 0 -> List(4, 8, 12, 16, 20))
Groupby - collection processing的更多相关文章
- lodash中Collection部分所有方法的总结
总结一下lodash中Collection的所有的方法,方便对比记忆,也便于使用时候查找. 1. 判断是否符合条件:返回bool: a) every: 判断每一值是不是都符合条件: 通过 pr ...
- Oracle中如何导出存储过程、函数、包和触发器的定义语句?如何导出表的结构?如何导出索引的创建语句?
Oracle中如何导出存储过程.函数.包和触发器的定义语句?如何导出表的结构?如何导出索引的创建语句? QQ群里有人问:如何导出一个用户下的存储过程? 麦苗答:方法有多种,可以使用DBMS_MET ...
- JAVA 集合 按照某个字段(依据一定条件)进行分组
由于数据不能够在本地化实现, 无法通过sql语句得到对应的结果,小编只好在业务层处理.通过调用接口得到集合,拿到集合后,通过年来分组,以此来达到对应的Map集合... 在这里小编给大家提供一个封装了一 ...
- java8模拟grouby方法
public class ListUtils{ /** * list 集合分组 * * @param list 待分组集合 * @param groupBy 分组Key算法 * @param < ...
- lodash用法系列(4),使用Map/Reduce转换
Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...
- lodash用法系列(1),数组集合操作
Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...
- R2—《R in Nutshell》 读书笔记(连载)
R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshe ...
- 机载LIDAR技术及其应用
1 机载LIDAR的系统组成及原理 1.1 机载 LIDAR 技术的发展历程 LIDAR 技术和机载激光扫描技术的发展源自 1970 年,美国航空航天局(NASA)支持研制成功第一台对地观测 LIDA ...
- Kafka Connect Architecture
Kafka Connect's goal of copying data between systems has been tackled by a variety of frameworks, ma ...
随机推荐
- PRML读书会第十一章 Sampling Methods(MCMC, Markov Chain Monte Carlo,细致平稳条件,Metropolis-Hastings,Gibbs Sampling,Slice Sampling,Hamiltonian MCMC)
主讲人 网络上的尼采 (新浪微博: @Nietzsche_复杂网络机器学习) 网络上的尼采(813394698) 9:05:00 今天的主要内容:Markov Chain Monte Carlo,M ...
- javascript 位运算
位运算博大精深,本文总结下基本的位运算的概念. 1.整数的二进制码 位操作符用于在最基本的层次上,即按内存中表示数值的位来操作数值.ECMAScript中的所有数值都以IEEE-754 64位格式存储 ...
- vue.js+boostrap最佳实践
一.为什么要写这篇文章 最近忙里偷闲学了一下vue.js,同时也复习了一下boostrap,发现这两种东西如果同时运用到一起,可以发挥很强大的作用,boostrap优雅的样式和丰富的组件使得页面开发变 ...
- RabbitMQ官方中文入门教程(PHP版) 第二部分:工作队列(Work queues)
工作队列 在第一篇教程中,我们已经写了一个从已知队列中发送和获取消息的程序.在这篇教程中,我们将创建一个工作队列(Work Queue),它会发送一些耗时的任务给多个工作者(Works ). 工作队列 ...
- 个人阅读作业——M1/M2总结
~ http://www.cnblogs.com/wx1306/p/4831950.html 在这篇博客中,我提出来一些关于软件工程的问题,但随着这一个学期的即将结束,以及我对软件开发的了解的深入,我 ...
- 使用Git时一些问题的处理
更新日志: 2016.06.04 晚 添加 git push --force (用于删除高版本的commit) 2015.11.29 晚 添加冲突处理工具Meld的教程 添加在GitHub上Rever ...
- AsyncTask异步任务类使用学习
new MyAsyncTask() .execute("http://pic.baike.soso.com/p/20120716/bki-20120716095331-640956396.j ...
- 那些用JavaScript写的操作系统
之前有人说过Chrome是新的C语言运行环境(Chrome Is The New C Runtime) ,不过笔者更倾向于Web是新的C语言运行环境,而且这种技术绝对没有版权问题,也绝不会被一家公司垄 ...
- python之旅【第一篇】
python简介 python的起源 追溯Python语言的起源,是从20世纪90年代初由Guido van Rossum,在阿姆斯特丹,开发一个新的脚本解释程序.不知道Guido当初有没有想到,Py ...
- Kernel Methods (5) Kernel PCA
先看一眼PCA与KPCA的可视化区别: 在PCA算法是怎么跟协方差矩阵/特征值/特征向量勾搭起来的?里已经推导过PCA算法的小半部分原理. 本文假设你已经知道了PCA算法的基本原理和步骤. 从原始输入 ...