//package org.JavaIncloud.java;
import java.util.HashMap ;
import java.util.Map ;
import java.util.Map.Entry ;
public class CommonOpetationWithMap
{
public static void main
( String ...
JavaInCloud ) {
/*create an HashMap(a class implements Map) and assign to a Map as we can not create object for Map interface*/
//Store some dataBases name to the collection of dataBases object
dataBases.put ( 100 , "Oracle" ) ; dataBases.put ( 101 , "MySQL" ) ; dataBases.put ( 102 , "Teradata" ) ;
dataBases.put ( 103 , "Sybase" ) ; dataBases.put ( 104 , "MongoDB" ) ; dataBases.put ( 105 , "DB2" ) ;
dataBases.put ( 106 , "SQLite" ) ; dataBases.put ( 107 , "Derby" ) ; dataBases.put ( 108 , "SQL-Server" ) ;
//Duplicate key(108) is not allowing simply it'll ignore and over write by Openbase.
dataBases.put ( 108 , "Openbase" ) ;
//You can duplicate the value(here Derby) for HashMap
dataBases.put ( 109 ,"Derby" ) ;
System .
out .
println ( dataBases
) ; //{102=Teradata,103=Sybase,100=Oracle,101=MySQL,108=Openbase,109=Derby,106=SQLite,107=Derby,104=MongoDB,105=DB2}
//Print the key for value Derby if and only if map is not empty and it contain value Derby otherwise don't go inside
if ( ! dataBases.isEmpty ( ) && dataBases.containsValue ( "Derby" ) )
{
System .
out .
println ( "Size of dataBases>>" + dataBases.
size ( ) ) ; //10 for ( Entry
< Integer , String
> dataBase
: dataBases.
entrySet ( ) ) {
if ( "Derby" .equals ( dataBase.getValue ( ) ) )
{
int key = dataBase.getKey ( ) ; //auto unboxing from Integer to int
}
//109 107
//N.B: As with value "Derby" two key is associated hence it is printing 109 and 107
}
}
}
}
Ly9wYWNrYWdlIG9yZy5KYXZhSW5jbG91ZC5qYXZhOwoKaW1wb3J0IGphdmEudXRpbC5IYXNoTWFwOwppbXBvcnQgamF2YS51dGlsLk1hcDsKaW1wb3J0IGphdmEudXRpbC5NYXAuRW50cnk7CgpwdWJsaWMgY2xhc3MgQ29tbW9uT3BldGF0aW9uV2l0aE1hcCAKewoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3Ry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