Every 5 minutes, within the 20th minute cycle, I need to retrieve the data. Currently, I'm using the map data structure.
Is there a better data structure? Every time I read and set the data, I have to write to the file to prevent program restart and data loss.
For example, if the initial data in the map is:
{-1:"result1",-2:"result2",-3:"result3",-4:"result4"}
I want to get the last -4 period's value which is "result4", and set the new value "result5", so that the updated map will be:
{-1:"result5",-2:"result1",-3:"result2",-4:"result3"}
And again, I want to get the last -4 period's value which is "result3", and set the new value "result6", so the map will be:
{-1:"result6",-2:"result5",-3:"result1",-4:"result2"}
The code:
private static String getAndSaveValue(int a) { 
    //read the map from file
    HashMap<Long,String> resultMap=getMapFromFile();
    String  value=resultMap.get(-4L);
    for (Long i = 4L; i >= 2; i--){
        resultMap.put(Long.parseLong(String.valueOf(i - 2 * i)),resultMap.get(1 - i));
    }
    resultMap.put(-1L,"result" + a);
    //save the map to file
    saveMapToFile(resultMap);
    return value;
}
 
     
    