Practice Time: Collections

 

Practice Time

One book is rarely alone, and one author rarely writes just one book.

  1. Create a Set of book titles called allBooks, for example, by William Shakespeare.
  2. Create a Map called library that associates the set of books, allBooks, to the author.
  3. Use the collections function any() on library to see if any of the books are “Hamlet’
  4. Create a MutableMap called moreBooks, and add one title/author to it.
  5. Use getOrPut() to see whether a title is in the map, and if the title is not in the map, add it.

// File:            Main.kt
// Programmer: Engineer Nolverto Urias Obeso
// Creation Date: 06/21/2023
// Email: nolvertou@gmail.com
// Description: Using MapList and SetList Collection

fun main(args: Array<String>) {

// TODO 1: Create a Set of book titles called allBooks, for example, by William Shakespeare.
val allBooks = setOf("Daemon", "Cryptonomicon", "Snow Crash", "I Robot")
// TODO 2: Create a Map called library that associates the set of books, allBooks, to the author.
val library = mapOf<String, String>("Daemon" to "Daniel Suarez", "Cryptonomicon" to "Neal Stephenson",
"Snow Crash" to "Neal Stephenson", "I Robot" to "Isaac Asimov" )

// TODO 3: Use the collections function any() on library to see if any of the books are “Hamlet’
println(library.any{it.value.contains("Hamlet")})

// TODO 4: Create a MutableMap called moreBooks, and add one title/author to it.
val moreBooks = mutableMapOf<String, String>("We Are Legion" to "Dennis E. Taylor")

// TODO 5: Use getOrPut() to see whether a title is in the map,
// and if the title is not in the map, add it.
println(moreBooks.getOrPut("Flying Dogs"){"Charles" })
println(moreBooks)
}

Comments

Popular posts from this blog