如何使用多个键来使用组?

时间:2015-07-29 08:25:01

标签: scala

我有这个文件,我想通过员工指定和部门阅读并分组文件,并找到平均工资。以下是我使用的代码。我用地图。如何使用group by实现它。

import scala.io.Source

object Problem {
    case class Employee(empId: String, 
                        designation: String, 
                        age: Int, 
                        salary: Long, 
                        department: Int)

    def main(arrg:Array[String]){
        var a = Source.fromFile("someFile.txt"). 
                        getLines(). 
                        map( _.split(",") ). 
                        map( l => ((l(1)+l(4)),l(3)) ). 
                        mapValues( _.map( _.salary ).sum/_.map.size )
       print(a)
    }
}

3 个答案:

答案 0 :(得分:1)

您可以按元组进行分组:

val employees = List(
  Employee("id", "des", 30, 1000, 1),
  Employee("id", "des2", 35, 1500, 1),
  Employee("id", "des", 40, 2000, 1)
)

employees
  .groupBy(e => (e.designation, e.department))
  .mapValues(emps => emps.map(_.salary).sum / emps.length)

// Map((des,1) -> 1500, (des2,1) -> 1500)

答案 1 :(得分:0)

只需groupBy一个元组:

Source.fromFile("someFile.txt").
    getLines().
    map( _.split(",") ).
    toSeq.
    map(data => Employee(data(0), data(1), data(2).toInt, data(3).toLong, data(4).toInt)).
    groupBy(emp => (emp.designation, emp.department)).
    mapValues(emp => emp.map(_.salary).sum / emp.length )

答案 2 :(得分:0)

让我分享一些来自私人工具藏匿处的代码

给出

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@drawable/app_back_image" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:isScrollContainer="true">
        ...
    </ScrollView>
</RelativeLayout>
libraryDependencies ++= Seq( "com.chuusai" %% "shapeless" % "2.2.3", "org.scalaz" %% "scalaz-core" % "7.1.1", "org.typelevel" %% "scalaz-spire" % "0.2", "com.github.melrief" %% "purecsv" % "0.0.2")

中的

此导入前缀:

build.sbt

这一小撮工具:

import purecsv.safe._
import shapeless.tag.Tagger
import scala.{util => ut}
import scalaz._
import Scalaz._
import spire.implicits._
import shapeless._
import shapeless.syntax.singleton._
import ops.hlist.{Selector, RightFolder}

你的模特:

trait CorrespondingLow extends Poly2 {
  implicit def drop[E, L <: HList, L2 <: HList] = at[E, (L, Tagger[L2])] { case (_, (l, aux)) => (l, aux) }
}
object CorrespondingFolder extends CorrespondingLow {
  implicit def take[E, L <: HList, L2 <: HList]
  (implicit sel2: Selector[L2, E]) = at[E, (L, Tagger[L2])] { case (e, (l, aux)) => (e :: l, aux) }
}
class corresponding[R2] {
  def move[R1, L1 <: HList, L2 <: HList, L2A <: HList]
  (rec: R1)
  (implicit lgen1: LabelledGeneric.Aux[R1, L1],
   lgen2: LabelledGeneric.Aux[R2, L2],
   rf: RightFolder.Aux[L1, (HNil, Tagger[L2]), CorrespondingFolder.type, (L2A, Tagger[L2])],
   lgen2a: LabelledGeneric.Aux[R2, L2A]): R2 =
    lgen2a.from(lgen1.to(rec).foldRight((HNil: HNil, tag[L2]))(CorrespondingFolder)._1)
}
object corresponding {
  def apply[R2] = new corresponding[R2]
}

implicit class TryOps[T](t: ut.Try[T]) {
  def toValidation: ValidationNel[Throwable, T] = t match {
    case ut.Success(v) => v.success
    case ut.Failure(ex) => ex.failureNel
  }
}

我们可以轻松地写:

case class Employee(empId: String,
                    designation: String,
                    age: Int,
                    salary: Long,
                    department: Int)

case class Group(designation: String, department: Int)

获取分组输出。