Created
December 14, 2021 00:34
-
-
Save cemdrman/31aced36e93fab1fc5024e522f10b99f to your computer and use it in GitHub Desktop.
scala collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util | |
import scala.:: | |
import scala.collection.mutable | |
import scala.collection.mutable.ListBuffer | |
object Collections { | |
def main(args: Array[String]): Unit ={ | |
val nameList: Array[String] = new Array[String](3) | |
val ageList = Array(5,3,8,7,1,9,23) | |
nameList(0) = "biilisim.io" | |
nameList(1) = "cem" | |
nameList(2) = "nehir" | |
for (i <- 0 until nameList.length){ | |
println(nameList(i)) | |
} | |
ageList.map(age => println(age)) | |
ageList.update(0,13) //replace | |
val minNumber = ageList.min | |
val maxNumber = ageList.max | |
val head = ageList.head | |
val last = ageList.last | |
println(minNumber + " - " + maxNumber + " - " + head + " - " + last) // 1 - 23 - 13 - 23 | |
val newAgeList: Array[Int] = ageList.drop(3) // listenin başından 3 elaman siler | |
newAgeList.map(age => println(age)) | |
println("---") | |
val numbers = (1 to 10).toArray // until de kullanılabilir. | |
numbers.map(number => println(number)) | |
println("--Collections--") | |
val cities: List[String] = List("İstanbul","Sinop","Bursa","Konya") | |
cities.foreach(city => println(city)) | |
var list: ListBuffer[Int] = ListBuffer() | |
list += 4 | |
list += 7 | |
var fruits = new ListBuffer[String]() | |
fruits += "mandalina" | |
fruits += "portakal" | |
fruits.foreach(fruit => println(fruit)) | |
val list1: List[String] = Nil | |
val istanbul = cities.head | |
var tailCity = cities.tail | |
val isEmpty: Boolean = cities.isEmpty | |
val citySet: Set[String] = Set("İzmir","Muğla","Erzurum") | |
//citySet += "Van" // compiler error. Çünkü immutable | |
var superLig: Set[String] = Set() | |
superLig += "fenerbahçe" | |
superLig += "galatasaray" | |
superLig += "beşiktaş" | |
superLig += "trabzonspor" | |
var ptt: Set[String] = Set("ankaragücü") | |
ptt += "ordu spor" | |
ptt += "giresun spor" | |
ptt += "giresun spor" | |
val allTeams: Set[String] = superLig ++ ptt | |
allTeams.foreach(team => println(team)) | |
println("--MAP--") | |
val immutableMap = Map(1 -> "cem", 2 -> "emir") | |
//immutableMap += 3 -> "necip" // compiler error | |
val mutableMap = collection.mutable.Map(1 -> "serra", 2 -> "nehir") | |
mutableMap += 3 -> "necip" | |
mutableMap += 4 -> "enes" | |
var isMapEmpty: Boolean = immutableMap.isEmpty | |
var value: Option[String] = immutableMap.get(1) | |
if (value.nonEmpty){ | |
println(value.get) | |
println("map size:" + immutableMap.size) | |
} | |
mutableMap.foreach(key => println("key: " + key._1 + ", value: " + key._2)) | |
mutableMap.remove(4) | |
mutableMap.foreach(key => println("key: " + key._1 + ", value: " + key._2)) | |
println("--TUPLES--") | |
val friendTuple = ("Haluk", 28,"ios developer",8.8, true) | |
val (name, age, job,score, isPlayer) = ("Ömer", 27,"java developer",9.8, false) | |
val (name1, age1, job1, score1, isPlayer1) = ("Halil", 27,"android developer",14.8, false) | |
println(friendTuple._1 + " - " + friendTuple._2) | |
println(name + " - " + score + " - " + job) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment