What structure should I use to store key and list of possible values?

Piszu :

I want to store information about filetype and possible files extension. What I want to do is if I provide file extension and this extension is on the list I will return key for it.

Eg:

Map<Filetype, List<String>> extensionMapping= new HashMap<>();
extensionMapping.put(Filetype.IMAGE, Arrays.asList("jpg", "jpeg"));
extensionMapping.put(Filetype.WORD, Arrays.asList("doc", "docx"));
extensionMapping.put(Filetype.EXCEL, Arrays.asList("xls", "xlsx", "xlsm"));
extensionMapping.put(Filetype.PPT, Arrays.asList("ppt", "pptx", "pptm"));`

And then I want to do something like this:

return extensionMapping.get().contains("jpg");

which for string "jpg" returns me Filetype.IMAGE.

Which collection should I use?

Deadpool :

I mean you can do that now also by having same structure Map<Filetype, List<String>>. In Map any of values (which is List of strings contains "jpg") will return Filetype.IMAGE or else it will return Filetype.None

extensionMapping.entrySet().stream().filter(entry->entry.getValue().contains("jpg")).map(Map.Entry::getKey)
      .findFirst().orElse(Filetype.None);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=145321&siteId=1