Posts

Showing posts with the label Google's Guava

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)

Java - Map with Multiple Keys - Example

Today we will show you how to implement a  Map with Multiple Keys.  Solution 1: Using Java Custom Key Class Solution 2: Using Google's Guava Solution 3: Using Apache Commons Collections  Example 1: Using Java Custom MultiMapKey Class Here we created a custom MultiMap Key class, we can use MultiMap as a Key to map a value.  import java.util.HashMap; import java.util.Map; public class MultiKeyDemo { public static void main( String [] args) { // Declaring the Map Map < MultiMapKey , String > table = new HashMap <>(); // Declaring the key objects MultiMapKey key1 = new MultiMapKey( "raw1" , "col1" ); MultiMapKey key2 = new MultiMapKey( "raw1" , "col2" ); MultiMapKey key3 = new MultiMapKey( "raw1" , "col3" ); // Putting the values table.put(key1, "Java" ); table.put(key2, "Kotlin" ); table.put(key3,