fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12.  
  13. Map < String, Set < String > > map =
  14. Map.of(
  15. "laptops" , Set.of( "macbook" , "thinkpad" , "yoga" ) ,
  16. "desktops" , Set.of( "macmini" , "imac" , "otherDesktop" ) ,
  17. "smartphones" , Set.of( "iphone" , "galaxy5" , "oneplus" )
  18. );
  19.  
  20. // You said: dictionary.get("macbook") would return "laptops"
  21. Optional < Map.Entry < String, Set < String > > > entry =
  22. map
  23. .entrySet()
  24. .stream()
  25. .filter( ( Map.Entry < String, Set < String > > stringSetEntry ) -> stringSetEntry.getValue().contains( "macbook" ) )
  26. .findAny();
  27. String result = entry.get().getKey();
  28. System.out.println( "result = " + result );
  29.  
  30.  
  31. }
  32. }
Success #stdin #stdout 0.14s 50428KB
stdin
Standard input is empty
stdout
result = laptops