fork(14) download
  1. //package org.JavaIncloud.java;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6.  
  7. public class CommonOpetationWithMap
  8. {
  9. public static void main(String...JavaInCloud)
  10. {
  11. /*create an HashMap(a class implements Map) and assign to a Map as we can not create object for Map interface*/
  12. Map<Integer, String> dataBases = new HashMap<Integer, String>();
  13.  
  14. //Store some dataBases name to the collection of dataBases object
  15. dataBases.put(100, "Oracle"); dataBases.put(101, "MySQL"); dataBases.put(102, "Teradata");
  16. dataBases.put(103, "Sybase"); dataBases.put(104, "MongoDB"); dataBases.put(105, "DB2");
  17. dataBases.put(106, "SQLite"); dataBases.put(107, "Derby"); dataBases.put(108, "SQL-Server");
  18. //Duplicate key(108) is not allowing simply it'll ignore and over write by Openbase.
  19. dataBases.put(108, "Openbase");
  20. //You can duplicate the value(here Derby) for HashMap
  21. dataBases.put(109,"Derby");
  22.  
  23. System.out.println(dataBases);
  24. //{102=Teradata,103=Sybase,100=Oracle,101=MySQL,108=Openbase,109=Derby,106=SQLite,107=Derby,104=MongoDB,105=DB2}
  25.  
  26. //Print the key for value Derby if and only if map is not empty and it contain value Derby otherwise don't go inside
  27. if(!dataBases.isEmpty() && dataBases.containsValue("Derby"))
  28. {
  29. System.out.println("Size of dataBases>>"+dataBases.size());//10
  30. for (Entry<Integer, String> dataBase : dataBases.entrySet())
  31. {
  32. if ("Derby".equals(dataBase.getValue()))
  33. {
  34. int key = dataBase.getKey(); //auto unboxing from Integer to int
  35. System.out.print(key+" ");
  36. }
  37. //109 107
  38. //N.B: As with value "Derby" two key is associated hence it is printing 109 and 107
  39. }
  40. }
  41. }
  42. }
  43.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:7: error: class CommonOpetationWithMap is public, should be declared in a file named CommonOpetationWithMap.java
public class CommonOpetationWithMap 
       ^
1 error
stdout
Standard output is empty