fork download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4. public static void main(String[] args) {
  5. Kamus kamus = new Kamus();
  6. kamus.tambah("big", new String[]{"large", "great"});
  7. kamus.tambah("big", new String[]{"huge", "fat"});
  8. kamus.tambah("huge", new String[]{"enormous", "gigantic"});
  9.  
  10. String[] sinonim = kamus.ambilSinonim("huge");
  11. if (sinonim != null) {
  12. System.out.println("Sinonim huge: " + Arrays.toString(sinonim));
  13. } else {
  14. System.out.println("Tidak ditemukan sinonim.");
  15. }
  16. }
  17. }
  18.  
  19. class Kamus {
  20. private HashMap<String, List<String>> dict = new HashMap<>();
  21.  
  22. public void tambah(String key, String[] values) {
  23. List<String> existing = dict.getOrDefault(key, new ArrayList<>());
  24. existing.addAll(Arrays.asList(values));
  25. dict.put(key, existing);
  26. }
  27.  
  28. public String[] ambilSinonim(String key) {
  29. if (dict.containsKey(key)) {
  30. List<String> value = dict.get(key);
  31. return value.toArray(new String[0]);
  32. }
  33. return null;
  34. }
  35. }
  36.  
Success #stdin #stdout 0.17s 57580KB
stdin
Standard input is empty
stdout
Sinonim huge: [enormous, gigantic]