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))
目录
相关文章
|
Linux 数据库
【Openstack】排错:Glance服务安装后HTTP 500解决
在搭建Openstack平台时,装完各个组件后发现了glance无法正常使用
3002 0
【Openstack】排错:Glance服务安装后HTTP 500解决
|
8月前
|
运维 安全 网络安全
VMware NSX 4.2.1.3 下载 - 网络安全虚拟化平台
VMware NSX 4.2.1.3 下载 - 网络安全虚拟化平台
265 0
VMware NSX 4.2.1.3 下载 - 网络安全虚拟化平台
|
弹性计算 人工智能 运维
运维神器 | 操作系统界的【通义灵码】-智能助手OS Copilot
【7月更文挑战第5天】运维神器 | 操作系统界的【通义灵码】-智能助手OS Copilot
39468 47
|
存储 安全 生物认证
网络安全强密码策略的重要性
【8月更文挑战第13天】
486 1
|
数据采集 分布式计算 DataWorks
DataWorks产品使用合集之赋值节点output是有数据的,引用参数运行打印时是空的,该如何解决
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
89 4
|
安全 编译器 PHP
PHP 8:新特性与未来展望
随着PHP 8的发布,这个广泛使用的服务器端脚本语言迎来了重大更新。本文将深入探讨PHP 8的新特性、性能提升以及它对现代web开发的影响。通过具体案例和数据分析,我们将评估这些更新如何优化代码编写、增强安全性和提升执行效率,同时预测PHP在持续演化的技术生态中的未来走向。 【7月更文挑战第23天】
170 6
|
机器学习/深度学习 分布式计算 DataWorks
MaxCompute产品使用问题之如何恢复误删除生产环境的表
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。
五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多
文章有点长,请耐心看完,绝对有收获!不想听我BB直接进入面试分享: 准备过程 蚂蚁金服面试分享 拼多多面试分享 字节跳动面试分享 总结
|
Web App开发 JavaScript 前端开发
03 JavaScript学习:浏览器中执行 JavaScript
03 JavaScript学习:浏览器中执行 JavaScript
|
C++
20002.数据结构C++ 单向链表
20002.数据结构C++ 单向链表
75 0