集合常用方法

技术文章 1年前 (2020) 完美者
396 0

标签:values   class   res   mmap   value   最大值   product   内容   nts   

集合常用方法

package chapter03

object test05 {

  def main(args:Array[String]):Unit={

    val list=List(1,2,3,4,1,3)

    //TODO 和
    println("sum= "+list.sum)

    //TODO 最大值
    println("max= "+list.max)

    //TODO 乘积 product
    println("produce= "+list.product)//24
    
    //TODO 反转 reverse
    println("reverse= "+list.reverse)

    //TODO 迭代 iterator
    for (elem <- list.iterator) {
      println(elem)
    }

    //TODO 映射 map(int=>B)
    val tupleList: List[(Int, Int)] = list.map(x=>{(x,1)})
    println(tupleList.mkString(","))

    val intToTuples: Map[Int, List[(Int, Int)]] = tupleList.groupBy(x=>{x._1})
    println("intToTuples "+intToTuples)
    intToTuples.foreach(t=>{println(t._1,t._2)})

    val intToInt2: Map[Int, Int] = intToTuples.map(x=>{(x._1,x._2.size)})
    println("intToInt2 "+intToInt2)
    println(intToInt2)

    //TODO 过滤 filter
    val ints: List[Int] = list.filter(x=>x%2==0)
    println("ints "+ints)

    //TODO 拉链,zip,关联后形成元组
    val list1 = List(1,3,5,7,9)
    val list2 = List(2,4,6,8)
    val tuples: List[(Int, Int)] = list1.zip(list2)
    println(tuples.mkString(",")) //(1,2),(3,4),(5,6),(7,8)

    //TODO 并集 union
    val unionList: List[Int] = list1.union(list2)
    println(unionList.mkString(",")) //(1,2),(3,4),(5,6),(7,8)

    //TODO 交集 intersect
    val intersectList: List[Int] = list1.intersect(list2)
    println(intersectList.mkString(",")) //

    //TODO 差集 diff
    val diffList: List[Int] = list1.diff(list2)
    println(diffList.mkString(",")) //

  }

}

package chapter03

object test06 {

  def main(args:Array[String]):Unit= {

    val list = List(1, 2, 3, 4, 1, 3)
    val stringList = List("11","12","22","23")

    //TODO 排序1 sortBy(int=>B)
    val ints: List[Int] = list.sortBy(x=>x)
    println(ints.mkString(","))

    //TODO 降序1 sortWith( (int,int)=>Boolean )
    val ints2: List[Int] = ints.sortWith((left,right) =>left>right)
    println(ints2.mkString(","))

    //TODO 排序2
    val sortStringList: List[String] = stringList.sortBy(x=>x.substring(1))
    println(sortStringList.mkString(","))

    //TODO 降序2
    val sortStringList2: List[String] = sortStringList.sortWith((left,right)=>left.substring(1).toInt>right.substring(1).toInt)
    println(sortStringList2.mkString(","))



  }

}

package chapter03

object test07 {

  def main(args:Array[String]):Unit={

    val list=List(1,2,3,4,1,3)

    //TODO groupBy(int=>k
    val intToInts: Map[Int, List[Int]] = list.groupBy(x=>x)
    println("intToInts "+intToInts)//(intToInts,Map(2 -> List(2), 4 -> List(4), 1 -> List(1, 1), 3 -> List(3, 3)))
    intToInts.foreach(t=>{println((t._1,t._2))})

    val intToInts2: Map[Int, List[Int]] = list.groupBy(x=>(x%2))
    println("intToInts2 "+intToInts2)
    intToInts2.foreach(t=>{println(t._1,t._2)})

    val stringList = List("11","12","22","23")
    val stringToStrings: Map[String, List[String]] = stringList.groupBy(x=>x.substring(0,1))
    println("stringToStrings "+stringToStrings)  //(stringToStrings,Map(2 -> List(22, 23), 1 -> List(11, 12)))
    stringToStrings.foreach(t=>{println(t._1,t._2)})


  }

}

package chapter03

object test08 {

  def main(args:Array[String]):Unit={


    //TODO wordCount


    val wordList = List("hello", "scala", "hello", "scala", "hello", "python")
    //将相同的单词放在一起:groupBy
    val stringToStrings: Map[String, List[String]] = wordList.groupBy(x=>x)
    println("stringToStrings "+stringToStrings)

    //将数据结构进行转换:map
    val stringToInt: Map[String, Int] = stringToStrings.map(x=>(x._1,x._2.size))
    println("stringToInt "+stringToInt)

    //将map转化为list:toList
    val list: List[(String, Int)] = stringToInt.toList
    println("list "+list)

    //将数据进行排序:sortWith
    val tuples: List[(String, Int)] = list.sortWith((left,right)=>{left._2>right._2})
    println("tuples "+tuples)

    //取前两个:take
    val tuples2: List[(String, Int)] = tuples.take(2)
    println("tuples2 "+tuples2)




    //TODO 扁平化操作:将整体内容拆分成个体
     val fileList = List("hello scala", "hello python", "hello scala")
    println("fileList "+fileList)

    val flatMapList: List[String] = fileList.flatMap(x=>x.split(" "))
    println("flatMapList "+flatMapList)

    val result: List[(String, Int)] = flatMapList.groupBy(x=>x).map(t=>(t._1,t._2.size)).toList.sortWith((left,right)=>(left._2>right._2))
    println("result "+result)

  }

}

package chapter03

import scala.collection.{GenTraversableOnce, mutable}
import scala.collection.immutable.StringOps

object test09 {

  def main(args:Array[String]):Unit={


    //TODO wordCount
    val linesList = List(("hello scala world",4),("hello world",3),("hello python",2),("hello hadoop",1))

    //1.拆分数据
    //("hello scala world",4) ==>(hello,4),(scala,4),(world,4)
    val flatMapList: List[(String, Int)] = linesList.flatMap(t => {
      val line = t._1
      val words = line.split(" ")
      words.map(w => (w, t._2))
    })
    println("flatMapList "+flatMapList)

    val groupWordMap: Map[String, List[(String, Int)]] = flatMapList.groupBy(x=>x._1)
    println("groupWordMap "+groupWordMap)

    //2.1 将分组后得数据进行结构转换
    val wordToSumMap: Map[String, Int] = groupWordMap.map(t => {
      val countList: List[Int] = t._2.map(x => x._2)
      (t._1, countList.sum)
    })
    println("wordToSumMap "+wordToSumMap)

    //2.1 mapValues可以只针对Map集合中的Value操作,Key保持不变
    val wordToSumMap2: Map[String, Int] = groupWordMap.mapValues(datas=>datas.map(tt=>tt._2).sum)
    println("wordToSumMap2 "+wordToSumMap2)

    //2.3 排序
    val wordSortList: List[(String, Int)] = wordToSumMap.toList.sortWith((left,right)=>left._2>right._2)
    println("wordSortList "+wordSortList)


    val takeList: List[(String, Int)] = wordSortList.take(4)
    println("takeList "+takeList)



  }

}

 

集合常用方法

标签:values   class   res   mmap   value   最大值   product   内容   nts   

原文地址:https://www.cnblogs.com/hapyygril/p/13490649.html

版权声明:完美者 发表于 2020-08-13 11:47:58。
转载请注明:集合常用方法 | 完美导航

暂无评论

暂无评论...