Monday, June 4, 2018

android kotlin - HashMap example

MainActivity.kt

package com.cfsuman.test

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Make the text view content scrollable
        textView.movementMethod = ScrollingMovementMethod()

        // Mutable hash map example
        button.setOnClickListener{
            val builder = StringBuilder()

            // Initialize a new empty mutable hash map
            val colors = mutableMapOf<String,String>()
            // Put some key value pairs to hash map
            colors.put("INDIANRED","#CD5C5C")
            colors.put("CRIMSON","#DC143C")
            colors.put("SALMON","#FA8072")
            colors.put("LIGHTCORAL","#F08080")

            builder.append("Loop through the mutable hash map")
            colors.forEach{key,value ->
                builder.append("\n$key,$value")
            }

            colors.remove("CRIMSON")
            builder.append("\n\n After remove an item")
            for ((key,value) in colors){
                builder.append("\n$key:$value")
            }


            // Replace/update a value
            colors.put("SALMON","NEW VALUE")
            builder.append("\n\n After updating a value")
            colors.forEach{key,value ->
                builder.append("\n$key,$value")
            }

            // Check whether hash map is empty
            builder.append("\n\nHashMap is empty? : ${colors.isEmpty()}")

            // Get value by key from hash map
            val value = colors.get("LIGHTCORAL")
            builder.append("\n\nLIGHTCORAL value $value")

            // Initialize a new hash map with key and value pairs
            val reds = mutableMapOf("RED" to "#FF0000", "FIREBRICK" to "#B22222", "CRIMSON" to "#DC143C")

            // Loop through the map
            builder.append("\n\nLoop through the new mutable hash map")
            reds.forEach{key,value->
                builder.append("\n$key : $value")
            }

            textView.text = builder.toString()
        }



        // Immutable hash map example
        button2.setOnClickListener{
            val builder = StringBuilder()

            // Initialize a new hash map with keys and values
            val colors = mapOf(
                    "GOLD" to "#FFD700",
                    "YELLOW" to "#FFFF00",
                    "ALICEBLUE" to "#F0F8FF",
                    "BISQUE" to "#FFE4C4"
            )

            builder.append("Loop through the map\n")
            // Loop through the map
            colors.forEach{key,value ->
                builder.append("\n$key : $value")
            }

            // Map keys to list
            val keys:List<String> = colors.keys.toList()

            // Map values to list
            val values:List<String> = colors.values.toList()


            // Loop through the map keys list
            builder.append("\n\nHashMap keys list\n")
            keys.forEach{
                builder.append("$it,")
            }

            // Loop through the map values list
            builder.append("\n\nHashMap values list\n")
            values.forEach{
                builder.append("$it,")
            }

            textView.text = builder.toString()
        }



        // Linked hash map example
        button3.setOnClickListener{
            val builder = StringBuilder()

            val colors = linkedMapOf<String,String>()
            colors.put("TEAL","#008080")
            colors.put("AQUA","#00FFFF")
            colors.put("LIGHTCYAN","#E0FFFF")
            colors.put("BLUE","#0000FF")
            colors.put("ROSYBROWN","#BC8F8F")

            builder.append("Loop through the linked hash map\n")
            // Loop through the linked hash map
            colors.forEach{key,value ->
                builder.append("\n$key : $value")
            }

            textView.text = builder.toString()
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Mutable HashMap"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:text="HashMap"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="LinkedHashMap"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button"
        android:textColor="#ff1557"
        />
</android.support.constraint.ConstraintLayout>