Posts

Showing posts with the label kotlin

Build REST CRUD APIs with Kotlin, Spring Boot and MyBatis

Image
Hello everyone, today we will learn how to develop REST-style CRUD APIs with Spring Boot, Kotlin, MyBatis, and H2 Database.  You can download the source code from our Github repository. What is MyBatis? MyBatis is a persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records. Some of the features of  MyBatis : In memory object filtering Fortifies clustering and simultaneous access by other applications without loss of transaction integrity Query Caching - Built-in support Fortifies disconnected operations Support for Remoting. Distributed Objects. Technologies used : Spring Boot 2.5.4 Spring  5.3.9 MyBatis  Kotlin Gradle After completing this tutorial what we will build?   We will build REST API 

Kotlin - Map with Multiple Keys - Example

Image
Today we will show you how to implement a  Map with Multiple Keys.  Solution 1: Using Kotlin Custom Key Class Solution 2: Using Google's Guava Solution 3: Using Apache Commons Collections  Example 1: Using Kotlin Custom MultiMapKey  Here we created a custom MultiMap Key class, we can use MultiMap as a Key to map a value. object MultiKeyDemo { @JvmStatic fun main(args: Array<String>) { // Declaring the Map val table: MutableMap<MultiMapKey, String> = HashMap() // Declaring the key objects val key1 = MultiMapKey( "raw1" , "col1" ) val key2 = MultiMapKey( "raw1" , "col2" ) val key3 = MultiMapKey( "raw1" , "col3" ) // Putting the values table[key1] = "Java" table[key2] = "Kotlin" table[key3] = "Android" // Getting value by key val value2 = table[key2] println(value2)

Kotlin - Finding largest & smallest element in an Array, List & Set

Example 1: Finding the largest element in an Array import java.util.* /* Kotlin Program to find the largest number in an Array. */ object Demo { @JvmStatic fun main (args: Array < String >) { val numbers = intArrayOf ( 2 , 6 , 7 , 9 , 5 , 155 , 66 , 99 ) val largestNumber = Arrays .stream( numbers ).max(). asInt println ( "Largest Number= $ largestNumber " ) } } Output: Largest Number= 155 Example 2: Finding the largest element in a List import java.util.* /* Kotlin Program to find the largest number in a List. */ object Demo { @JvmStatic fun main (args: Array < String >) { val numbers = listOf ( 2 , 6 , 7 , 9 , 5 , 155 , 66 , 99 ) val largestNumber = numbers .stream().max( Comparator . comparing { i: Int ? -> Integer .valueOf(i!!) } ).get() println ( "Largest Number= $ largestNumber " ) } } Output: Largest Number= 15

Kotlin: Print Odd & Even Numbers from Array, List & Set

Example 1: Kotlin program to print odd numbers from a List import java.util.stream. Collectors /*Kotlin Program to find Odd Numbers from a list*/ object DriverClass { @JvmStatic fun main (args: Array < String >) { val numbers = listOf ( 1 , 4 , 8 , 40 , 11 , 22 , 33 , 99 ) val oddNumbers = numbers .stream(). filter { o: Int -> o % 2 != 0 } . collect( Collectors .toList()) println ( oddNumbers ) } } Example 2: Kotlin program to print even numbers from a List import java.util.stream. Collectors /*Kotlin Program to find Even Numbers from a list*/ object DriverClass { @JvmStatic fun main (args: Array < String >) { val numbers = listOf ( 1 , 4 , 8 , 40 , 11 , 22 , 33 , 99 ) val evenNumbers = numbers .stream(). filter { o: Int -> o % 2 == 0 } . collect( Collectors .toList()) println ( evenNumbers ) } } Example 3: Kotlin program to print eve