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的更多相关文章

  1. lodash中Collection部分所有方法的总结

    总结一下lodash中Collection的所有的方法,方便对比记忆,也便于使用时候查找. 1.    判断是否符合条件:返回bool: a)  every: 判断每一值是不是都符合条件: 通过 pr ...

  2. Oracle中如何导出存储过程、函数、包和触发器的定义语句?如何导出表的结构?如何导出索引的创建语句?

    Oracle中如何导出存储过程.函数.包和触发器的定义语句?如何导出表的结构?如何导出索引的创建语句? QQ群里有人问:如何导出一个用户下的存储过程?   麦苗答:方法有多种,可以使用DBMS_MET ...

  3. JAVA 集合 按照某个字段(依据一定条件)进行分组

    由于数据不能够在本地化实现, 无法通过sql语句得到对应的结果,小编只好在业务层处理.通过调用接口得到集合,拿到集合后,通过年来分组,以此来达到对应的Map集合... 在这里小编给大家提供一个封装了一 ...

  4. java8模拟grouby方法

    public class ListUtils{ /** * list 集合分组 * * @param list 待分组集合 * @param groupBy 分组Key算法 * @param < ...

  5. lodash用法系列(4),使用Map/Reduce转换

    Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...

  6. lodash用法系列(1),数组集合操作

    Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...

  7. R2—《R in Nutshell》 读书笔记(连载)

    R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshe ...

  8. 机载LIDAR技术及其应用

    1 机载LIDAR的系统组成及原理 1.1 机载 LIDAR 技术的发展历程 LIDAR 技术和机载激光扫描技术的发展源自 1970 年,美国航空航天局(NASA)支持研制成功第一台对地观测 LIDA ...

  9. Kafka Connect Architecture

    Kafka Connect's goal of copying data between systems has been tackled by a variety of frameworks, ma ...

随机推荐

  1. MYSQL查询优化

    目前手头有个查询: SELECT LPP.learning_project_pupilID, SL.serviceID, MAX(LPPO.start_date), SUM(LPPOT.license ...

  2. 单片机C语言探究--为什么变量最好要赋初值

    有许多书上说,变量最好要赋初值.但是为什么要初值呢?不赋初值可能会出现什么样的意外呢?以下就我在以51单片机为MCU,Keil为编译器看到的实现现象作分析.众所周知,变量是存储在RAM中,掉电后即丢失 ...

  3. <实训|第五天>通过搭建NFS,FTP实现共享文件附Vim脚本游戏

    先说个事情:我周末是不更新这个系列教程的,不过其他内容的会更新,我周末就整理这一周的各种内容到我的微信公众号中,提供给大家! 期待已久的linux运维.oracle"培训班"终于开 ...

  4. 关于mvc5+EF里面的db.Entry(model).State = EntityState.Modified报错问题

    最近在使用mvc5+EF的的时候用到了这句话 db.Entry(model).State = EntityState.Modified 看上去很简单的修改数据,但是一直报错,说是key已经存在,不能修 ...

  5. Hadoop简单安装配置

    Hadoop开始设计以Linux平台为运行目标,所以这里推荐在Linux发行版比如Ubuntu进行安装,目前已经有Hadoop for Windows出来,大家自行搜下文章. Hadoop运行模式分为 ...

  6. matlab 功率谱分析

    matlab 功率谱分析 1.直接法:直接法又称周期图法,它是把随机序列x(n)的N个观测数据视为一能量有限的序列,直接计算x(n)的离散傅立叶变换,得X(k),然后再取其幅值的平方,并除以N,作为序 ...

  7. AVL树插入操作实现

    为了提高二插排序树的性能,规定树中的每个节点的左子树和右子树高度差的绝对值不能大于1.为了满足上面的要求需要在插入完成后对树进行调整.下面介绍各个调整方式. 右单旋转 如下图所示,节点A的平衡因子(左 ...

  8. Excel——将内容导入

    1.写入Excel文件的操作引入Microsoft.Office.Tools.Excel.dll 程序集 List<Person> list = new List<Person> ...

  9. 文本域的宽度和高度应该用cols和rows来控制,还是 用width和height来控制

    文本域宽度如果用cols来控制,缩放网页的时候文本域的宽度不会自动变化 用width来表示就会跟着网页缩放而缩放 看到下面一段文字: 对于内容至上的网页,在禁用CSS的情况下,HTML内容要做到易于阅 ...

  10. Android下常见动画

    摘要:Android中常见的的动画有三种:属性动画.补间动画.帧动画. 注.因为前两种内容较多,后补 一.属性动画 二.补间动画 三.帧动画:本质是将一些连贯的图片加载形成连贯的动画效果 1.在Dra ...