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. System.out.println(Messages.isMessageInGroup("Another Message"));
  13. }
  14.  
  15. }
  16.  
  17. enum Messages {
  18.  
  19. MESSAGE_1("Message", "Category"),
  20. MESSAGE_2("Another Message", "Category"),
  21. MESSAGE_3("Odd Message", "Another Category");
  22.  
  23. private static final Map<String, Set<String>> map;
  24. static {
  25. Map<String, Set<String>> tempMap = new HashMap<>();
  26. for (Messages m : Messages.values()) {
  27. tempMap.computeIfAbsent(m.category, s -> new HashSet<>()).add(m.message);
  28. }
  29. map = Collections.unmodifiableMap(tempMap);
  30. }
  31.  
  32. private final String message, category;
  33.  
  34. private Messages(String message, String category) {
  35. this.message = message;
  36. this.category = category;
  37. }
  38.  
  39. public String getMessage() { return message; }
  40. public String getCategory() { return category; }
  41.  
  42. public static boolean isMessageInGroup(String message){
  43. // use `getOrDefault` if `get` could return null!!
  44. return map.get("Category").contains(message);
  45. }
  46. public static Set<String> messagesInGroup(String category) {
  47. return Collections.unmodifiableSet(
  48. map.getOrDefault(category, Collections.emptySet())
  49. );
  50. }
  51. }
Success #stdin #stdout 0.22s 320704KB
stdin
Standard input is empty
stdout
true