我的第一個scala程序
幫同事處理List[scala.collection.immutable.Map[String,Any]]
的排序問題
var x = mylist.sortWith(_.get("commonCount").toString > _.get("commonCount").toString)n
_.get("commonCount")
可以取出值,但類型是 Some
,要用_.get("commonCount").get
來獲取真實類型
scala> println(mylist.map(_.get("commonCount").get.getClass))nList(class java.lang.Integer, class java.lang.Integer, class java.lang.Integer, class java.lang.Integer, class java.lang.Integer)n
知道真實的數據類型,但在實際使用時要轉換成基本類型.asInstanceOf[Int]
,進行判斷
mylist.sortWith((x, y) => x.get("commonCount").get.asInstanceOf[Int] < y.get("commonCount").get.asInstanceOf[Int]))n
另一種方法:
mylist.sortWith(_.get("commonCount").get.asInstanceOf[Int] < _.get("commonCount").get.asInstanceOf[Int])n
源碼:
import scala.collection.immutable.{HashMap, List}nn/**n * Created by 185378641@qq.com on 2017/12/7. n *n * @author jiangtaon *n * [n * {n * "treatmentMoney": 0,n * "hospital": "湖南中醫學院第二附屬醫院湖南省中醫院",n * "commonCount": 2,n * "department": "主治醫師",n * "departmentFunction": "a19ec1789ab711e7a9ff408d5c916e3f",n * "docId": "3291ba79901e42bfb756745fb803df64",n * "joinedClinic": [n * "馬冬梅--陳小春"n * ],n * "docName": "馬冬梅--陳小春--張三"n * },n * {n * "treatmentMoney": 0,n * "hospital": "湖南中醫學院第二附屬醫院湖南省中醫院",n * "commonCount": 1,n * "department": "主治醫師",n * "departmentFunction": "a19ec1789ab711e7a9ff408d5c916e3f",n * "docId": "1ec9303c79df48d9988bd7bce4370c0e",n * "joinedClinic": [n * "馬冬梅--陳小春--張三",n * "小二"n * ],n * "docName": "麻子"n * }n * ]n */nnobject Main extends App {n println("hello world")n //n // val days = List(n // "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"n // )nn var one = Map(n "docName" -> "馬冬梅",n "commonCount" -> 11n )n var two = Map(n "docName" -> "麻子",n "commonCount" -> 23n )n var three = Map(n "docName" -> "麻子",n "commonCount" -> 3n )n var four = Map(n "docName" -> "麻子",n "commonCount" -> 19n )n var five = Map(n "docName" -> "麻子",n "commonCount" -> 5n )nn var mylist = List(one, two, three, four, five)nn println(mylist.sortWith((x, y) => x.get("commonCount").get.asInstanceOf[Int] > y.get("commonCount").get.asInstanceOf[Int]))n println(mylist.sortWith((x, y) => x.get("commonCount").get.asInstanceOf[Int] < y.get("commonCount").get.asInstanceOf[Int]))nn // println(mylist.sortWith((x,y) => x.get("commonCount"))n // mylist.toList.foreach(i => println(i.get("commonCount").getClass))n//n// var x = Map(n// "a" -> 1,n// "b" -> "b"n// )n// var y = Map(n// "a" -> 11,n// "b" -> "b"n// )n// var z = Map(n// "a" -> 3,n// "b" -> "b"n// )nn// println(x.get("a").get.getClass)n// println(y.get("a").get.getClass)n// println(z)n//n// var xyz = List(x, y, z)nn // println(xyz.sortBy(_.get("a").get))nnn /*n * testn */nn // print(mylist.sorted)n // print(mylist.flatMap(i => i.get("commonCount")).sortBy(_))n // print(mylist.map(i => i.get("commonCount")).sortWith((x,y) => x > y ))n // print(mylist.map(i => i.get("commonCount")).sortWith(_.toString > _.toString))n // print(mylist.sortBy(i =>(i.get("commonCount"))))n // print(mylist.sortBy(_.get("commonCount")))n // print(mylist.map(_.foreach(println)))n // print(mylist.map(_.get("commonCount")).foreach(println))n // print(mylist.sortWith(_.get("commonCount").toString > _.get("commonCount").toString))n // println(mylist.sortWith(_.get("commonCount").toString < _.get("commonCount").toString))n // println(mylist.sortBy(_.get("commonCount").toString))n // println(mylist.sortBy(_.values))n // println(mylist.sorted(Ordering.Int.reverse))n // println(mylist.sortBy(_.get("commonCount")))n // println(mylist.groupBy(_.get("commonCount")))n // println(mylist.sortWith(_.foreach(println(_._2))))n // println(mylist.map(_.mapValues()))n // println(mylist.sortBy(i => i.get("commonCount").toString ))n // println(mylist.sortBy(i => (x = i.get("commonCount"),y=i.get("commonCount") )=>n // println(mylist.sortWith((x,y)=>x.get("commonCount").toString > y.get("commonCount").toString))n // println(mylist.sortWith((x, y) => x.get("commonCount").toString.compareTo(y.get("commonCount").toString)<0))n // println(mylist.sortWith((x, y) => x.get("commonCount").toString.compare(y.get("commonCount").toString) < 0))n // println(mylist.sortWith(_.get("commonCount") < _.get("commonCount")))nn // println(mylist.sortBy(_.get("commonCount").foreach(println)))n // println(mylist.sortWith(_.get("commonCount").toString > _.get("commonCount").toString))n // println(mylist.sortWith((x, y) => x.get("commonCount").toString < y.get("commonCount").toString))n // println(mylist.sorted((x, y) => x.get("commonCount").toString.compareTo(y.get("commonCount").toString)))n // println(mylist.sortBy(i => (i.get("commonCount").toString.charAt(0),i.get("commonCount").toString.charAt(1))))n // print(mylist.sortBy(_.get("commonCount")))n // print(mylist.map(_.get("commonCount") < _.get("commonCount")): _*))n // print(mylist.sortBy(i => i.get("commonCount")))n // print(mylist.map(_.get("commonCount")).sortBy{_}:_*)n // print(mylist.sortWith(_.get("commonCount") > ).toSeq)n // var v = mylist.groupBy(_.get("commonCount"))n // println(v.keys.toList.foreach(println(_)))nnn // println(v.take(1).foreach(println))nnn // val newMap = mylist.sortBy().foldLeft(new ListMap[String, String]) {n // (map, key) => map + (key._1 -> key._2)nn // is okn // var a = Item("馬冬梅", 2, "主任")n // var b = Item("麻子", 9, "主任")n // var c = Item("麻子", 11, "主任")n // var d = Item("麻子", 5, "主任")n //n // var mylist = List(a, b, c, d)n // println(mylist.sortWith((x,y) => x.commonCount.compare(y.commonCount)>0))n // println(mylist.sortBy(i => i.commonCount))nnn // var mymap = List(a,b,c,d).sortBy()n // print(mymap)n // var mylist =Map(one.get("commonCount")->one,two.get("commonCount")->two,three.get("commonCount")->three).toList.sortBy{_._2}:_*n // var mylist = listMap(Map(one->one.get("commonCount"),two.toString()->two.get("commonCount"),three.toString()->three.get("commonCount")).toList.sortBy{_._2}:_*)n // var listMap = ListMap(Map("foo"->3, "raise"->1, "the"->2, "bar"->4,"bar1"->4).toList.sortBy{_._2}:_*)nnn}nnncase class Item(docName: String, commonCount: Int, departmentName: String)nnn//n//class Doctor {n//n// object docIdn//n// object getRealNamen//n// object getDepartmentNamen//n// object getDepartmentFunctionIdn//n// object getHospitalNamen//n//// var getTreatmentMoneyn//n// var commonCount:Stringn//n// override def toString = commonCountn//n//}n
作者:Aaron
鏈接:我的第一個scala程序聲明:本文來源於網路,版權歸作者所有,若有什麼問題,請聯繫我們,謝謝!
推薦閱讀: