Sunteți pe pagina 1din 7

Ministerul Educa iei al Republicii Moldova

Universitatea Tehnic a Moldovei


Departamentul Informatic Software i Automatic

Tema: Collections n Kotlin

RAPORT
Lucrare de laborator nr. 3
La Analiza i proiectarea programelor orientate pe obiecte

A efectuat:
st. gr. SI-141
E. Cucu

A verificat:
lect. superior
M. Balan

Chi inu 2017


Scopul lucrrii

nsuirea tehnologiilor de programare cu utilizarea coleciilor n Kotlin.

Obiectiv

Scrierea a trei programe folosind coleciile n Kotlin.

Mersul lucrrii:

Pentru nceput am definit clasa utilizator, cu care voi lucra n viitor.


Am ales clasa maina, care are ca cimpuri a clasei modelul i marca, n care am definit
constructorii, setter-rii, getter-rii, am implementat interfaa Comparable pentru metoda
compareTo(), pentru ulterioara sortare, la fel am rescris metodele hashValue() i equals()
pentru a putea egala obiectele corect.
package md.jack

import java.util.Objects

class Car(private var mark: String, private var model: String) : Comparable<Car> {
fun getMark() : String {
return mark
}
fun setMark(mark : String) {
this.mark = mark
}
fun getModel() : String {
return model
}
fun setModel(model : String) {
this.model = model
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Car) return false
return mark == other.mark && model == other.model
}

override fun hashCode(): Int {


return Objects.hash(mark, model)
}

override fun compareTo(other: Car): Int {


return this.mark.compareTo(other.mark)
}

override fun toString(): String {


return "Mark : $mark Model $model"
}
}
Descrierea funcionala a clasei poate fi gasit la ANEXA B.
Acum putem utiliza clasa data n Collections conform variantei propuse.

2
8 multimap deque float

Am iniializat dou HashMap-uri i 2 deque, la fel i 3 liste.


Pentru printarea elemenetelor containerilor ne folosim de iteratori, sau de stream-uri.
firstMap.forEach { integer, car -> println("Category $integer $car") }

Pentru cutarea dup o condiie am folosit stream-ul la colecia respectiva, apoi


metoda any(), pare primeste un predicat, deodata i printm daca va fi gasit..
println(secondMap.entries.any { it -> it.value.getMark() == "Mercedes" })

Pentru calcularea numarului de elemente care satisfac o condiie am folosit la fel


stream-urile utiliznd metoda count(), iar condiia o inpunem cu ajutorul metodei filter().
println(thirdList.filter { it -> it.getMark() == "Mercedes" }.count())

Concluzii

Efectund lucrarea de laborator am studiat coleciile n limbajul Kotlin.


Utilizarea containerilor permite stocarea obiectelor i prelucrarea lor specific, iar
utilizarea algoritmilor ne permite uor s operm i s colaborm cu orice tip de
containere.

3
Bibliografie:

1 Coleciile n Kotlin[surs electronic]


https://kotlinlang.org/docs/reference/collections.html
2 Note de laborator: Indicaii metodice

4
ANEXA A

Car.kt
package md.jack

import java.util.Objects

class Car(private var mark: String, private var model: String) : Comparable<Car> {
fun getMark() : String {
return mark
}
fun setMark(mark : String) {
this.mark = mark
}
fun getModel() : String {
return model
}
fun setModel(model : String) {
this.model = model
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Car) return false
return mark == other.mark && model == other.model
}

override fun hashCode(): Int {


return Objects.hash(mark, model)
}

override fun compareTo(other: Car): Int {


return this.mark.compareTo(other.mark)
}

override fun toString(): String {


return "Mark : $mark Model $model"
}
}

5
ANEXA B

Main.java
package md.jack

import java.util.*

fun main(args: Array<String>) {


val firstMap = HashMap<Int, Car>()
val secondMap = HashMap<Int, Car>()
val firstDeque = ArrayDeque<Car>()
val secondDeque = ArrayDeque<Car>()
val firstList = ArrayList<Car>()
val secondList = ArrayList<Car>()
val thirdList = ArrayList<Car>()
firstMap.put(1, Car("Mercedes", "A-Class"))
firstMap.put(2, Car("Mercedes", "C-Class"))
firstMap.put(3, Car("Mercedes", "E-Class"))
firstMap.put(4, Car("Mercedes", "S-Class"))
firstMap.put(5, Car("Audi", "A1"))
firstMap.put(6, Car("Audi", "A3"))
firstMap.put(7, Car("Audi", "A5"))
firstMap.put(8, Car("Audi", "A7"))
firstMap.put(9, Car("BMW", "3-series"))
firstMap.put(10, Car("BMW", "5-series"))
firstMap.put(11, Car("BMW", "7-series"))
secondMap.putAll(firstMap)
firstDeque.add(Car("VW", "Passat"))
firstDeque.add(Car("VW", "Golf"))
firstDeque.add(Car("VW", "Polo"))
firstDeque.add(Car("Skoda", "Octavia"))
firstDeque.add(Car("Skoda", "Superb"))
firstDeque.add(Car("Skoda", "Fabia"))
println("Elements of firstMap")
firstMap.forEach { integer, car -> println("Category $integer $car") }
firstMap.entries.removeAll { it -> it.value.getMark() == "Mercedes" }
println("Elements of firstMap after remove by condition")
firstMap.forEach { integer, car -> println("Category $integer $car") }
for ((key, value) in firstMap) {
if (value == Car("BMW", "3-series")) {
firstMap.replace(key, Car("BMW", "6-series"))
}
}
println("Elements of firstMap after editing")
firstMap.forEach { integer, car -> println("Category $integer $car") }
val iterator = firstMap.entries.iterator()
var start = false
while (iterator.hasNext()) {
val value = iterator.next().value
if (value == Car("Audi", "A7")) start = true
if (start) {
iterator.remove()
}
if (value == Car("BMW", "5-series")) start = false
}
println("Elements of firstMap after delete")
firstMap.forEach { integer, car -> println("Category $integer $car") }
println("Elements of firstDeque before adding from firstMap")
firstDeque.forEach(::println)
firstDeque.addAll(firstMap.values)
println("Elements of firstDeque")
firstDeque.forEach(::println)
println("Elements of secondMap")
secondMap.forEach { integer, car -> println("Category $integer $car") }
println("Exists in secondMap elements with mark Mercedes ? ")
println(secondMap.entries.any { it -> it.value.getMark() == "Mercedes" })
println("Copying elements with mark Mercedes to secondDeque")
secondDeque

6
.addAll(secondMap.entries
.filter { it -> it.value.getMark() == "Mercedes" }
.map(MutableMap.MutableEntry<Int, Car>::value)
.toTypedArray())
secondDeque.forEach(::println)
println("Copy elements from secondMap to firstList")
firstList.addAll(secondMap.values)
firstList.forEach(::println)
secondList.addAll(secondDeque)
println("Copy elements from secondDeque to secondList")
secondList.forEach(::println)
println("Sort firstList")
Collections.sort(firstList)
firstList.forEach(::println)
Collections.sort(secondList)
thirdList.addAll(firstList)
thirdList.addAll(secondList)
println("Sort secondList")
secondList.forEach(::println)
println("Merge lists")
thirdList.forEach(::println)
println("Found how many elements with mark Mercedes found in merged list")
println(thirdList.filter { it -> it.getMark() == "Mercedes" }.count())
}