I want to be able to sort files based on what day they were entered into a Map. The key is the day, and the value is a List of n Files added on the respective day. However, I need to be able to add the files to the List one at a time – but I’m stuck on the syntax. How do I call List.add() from within Map.put()? Here’s my code:
public static NavigableMap<String, List<File>> myFiles = new TreeMap<>();
String today = new Date().toString();
File currentFile;
myFiles.put(today, currentFile); //problem here adding currentFile
It’s because of type safetyp (using generics).
todaypoints to a list of files and you want to just add one file to that list.I would recommend you to first check if
todayis in the map and if it isn’t add the backing list, then, add thecurrentFileto that list: