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 ...
随机推荐
- springmvc请求参数异常处理
接着上一篇<springmvc 通过异常增强返回给客户端统一格式>讲通过spring ControllerAdvice对各种异常进行拦截处理,统一格式返回给客户端. 接下来我们更精细的讲, ...
- Android视频播放之VideoView
Android视频播放之VideoView 1.VideoView类介绍 Android的VideoView组件可以从不同的来源(例如资源文件或内容提供器)读取图像,计算和维护视频的画面尺寸以使其适用 ...
- sql 几点记录
1 With子句 1.1 学习目标 掌握with子句用法,并且了解with子句能够提高查询效率的原因. 1.2 With子句要点 with子句的返回结果存到用户的临时表 ...
- 天龙客户端的ResourceManager
今天培训的时候,Leader针对项目结构讲了很多分层架构的思想,思路,对我而言有很大的助益,学会了将需求分层,或者说先设计出各个层次,然后有需求后落实到对应的层次上,尤其对于刚开始的架构设计阶段,能把 ...
- JavaScript返回上一级,并重新加载页面
window.location.href = document.referrer;
- C 语言学习的第 02 课:C 语言的开发环境
工欲善其事,必先利其器.不知道还是不是记得上一篇文章中说到的,计算机本身是一个数据输入及输出的设备.所以,为了将你大脑中的各种 idea 输入到电脑,且最终生成能够执行的程序,总是要预备点什么的. 通 ...
- extjs5 一个容器中有几个组件公用一个控制器和一个模型
Ext.define('TestViewModel', { extend: 'Ext.app.ViewModel', alias: 'viewmodel.test', // connects to v ...
- Java--剑指offer(4)
16.输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. a)这里首先判断两个链表中有没有空表,这个就是依据表头是否为空.然后就是比较节点值的大小,然后就是使 ...
- Java--剑指offer(2)
6.把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. a)使用ArrayList来存放元素 public class Solution { public static int min ...
- viewSub惰性装载器
在需要的时候才解析,不耗费资源 <ViewStub android:id="@+id/stub" android:layout_width="wrap_conten ...