How to loop through elements in a stream and access a method from another class for every element in Java?

Viewed 24

My exercise is to get a list of dates e.g. [1999-01-01, 1999-06-01, 1999-11-01]. Each date has a file with its name which contains currency exchange information like below:

  • "disclaimer": "Usage subject to terms: https://openexchangerates.org/terms",
  • "license": "https://openexchangerates.org/license",
  • "timestamp": 915210000,
  • "base": "USD",
  • "rates": { "EUR": 0.853515, "GBP": 0.602941, "SEK": 8.117873 }

When given a String symbol like EUR and a String date like 1999-01-01, I need to use the stream() API in order to return an array of Strings containing all the exchange rate info for that year, in the below format:

{"Date: 1999-01-01 Rate: 0.602941", "Date: 1999-06-01 Rate: 0.621195", "Date: 1999-11-01 Rate: 0.60824"}

I'm thinking I can do

  1. list.stream()
  2. need to loop through each element in the list
  3. use an existing method in another class "getRateAtDate" which returns a DTO object that contains all of the data disclaimer, license etc.
  4. access the object to get the exchange rate
  5. format the string to look like in the example
  6. save to an array of Strings

I'm having a difficulty to figure out how to loop through each element and then do the above steps


public record RateListing (String disclaimer, String license, long timestamp, String base, Map<String,Double> rates){
}



public class RatesListingReader {

  public RateListing readRatesAtDate(String date){
    String format = "data/"+date+".json";
    try {
      URL resource = this.getClass().getClassLoader().getResource(format);
      byte[] bytes = new FileInputStream(resource.getPath()).readAllBytes();
      RateListing rateListing = new ObjectMapper().readValue(bytes, RateListing.class);
      return rateListing;
    } catch (IOException | NullPointerException e) {
      // -> no file or failed to read it.
      return null;
    }
  }


  public List<String> getRatesFiles() {
    URL resource = this.getClass().getClassLoader().getResource("data");
    File f = new File(resource.getPath());
    List<String> strings = Arrays.stream(f.listFiles())
        .map(fl -> fl.getName().substring(0,10))
        .sorted()
        .collect(Collectors.toList());
    return strings;
  }
}



  public Double getRateAtDate(String symbol, String date) {
    RateListing rateListing = rlrReader.readRatesAtDate(date);
    throwExceptionIfSymbolNotFound(rateListing, symbol);

    return rateListing.rates().get(symbol);
  }




public String[] getRatesForYear(String symbol, String year) {

    // Get all the dates in the format YYYY-MM-DD
    List<String> listOfYearFiles = rlrReader.getRatesFiles();

    // stream the List, filter only the year 
    List<String> finalList = listOfYearFiles.stream()
            .filter(string -> string.startsWith(year))
            .forEach(string -> 

I hope the requirement makes sense and that someone is able to help. Thank you!

0 Answers
Related