zoukankan      html  css  js  c++  java
  • Groupby

    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))
  • 相关阅读:
    杭电 FatMouse' Trade
    Navicat Report Viewer 设置选项的问题
    用Beyond Compare精确查找文本内容的方法
    Beyond Compare表格比较详解
    Navicat Report Viewer 如何连接到 MySQL 数据库
    Navicat for MySQL 选项设置技巧详解
    Navicat 用什么方法检测 Oracle 数据库安全性
    有哪些好用的比较工具
    Beyond Compare文件比较有哪些方式
    Beyond Compare怎样修改差异文件夹
  • 原文地址:https://www.cnblogs.com/rollenholt/p/4170820.html
Copyright © 2011-2022 走看看