Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Picking the highest number in a list of strings

I have a string list with "numbers":
"Rain Probability": [ "100", "95", "95" ]

And I want to be able to get only the highest one. I then put everything in a List called "result" but instead pf the string list I want just the highest number.

`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public List<WeeklyForecast> convert(){

        AllForecast allForecast = Templates.restTemplate(restTemplate);
        List<NextDays> something = allForecast.getNextDays();
        List<WeeklyForecast> result = new ArrayList<WeeklyForecast>();
        
        
        for (int i=0; i< something.size(); i++){
            NextDays before = something.get(i);
            WeeklyForecast weekly = new WeeklyForecast(
              before.getData().getData(),
              before.getTemperature().getMaximumTemperature(),
              before.getTemperature().getMinimumTemperature(),
              before.getRain());

            result.add(i,weekly);
        }
        
        return result;
       
       // before.getRain().stream().mapToInt(Integer::parseInt).max().orElse(-1)     
     
        
    }

`

This is my WeeklyForecast Model:

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class WeeklyForecast {

    @JsonProperty("Date")
    private String data;

    @JsonProperty("Maximum Temperature")
    private String tempMax;

    @JsonProperty("Minimum Temperature")
    private String tempMin;

    @JsonProperty("Rain Probability")
    private int rain;

}

And the NextDays object:

@JsonIgnoreProperties(ignoreUnknown = true)
public class NextDays {

@JsonProperty("@attributes")
private NextDate data;

private List<String> rain;

@JsonProperty("temperatura")
private NextTemperature temperature;

public NextDays(){
    rain = new ArrayList<>();
}

public NextDate getData() {
    return data;
}

public void setData(NextDate data) {
    this.data = data;
}

public NextTemperature getTemperature() {
    return temperature;
}

public void setTemperature(NextTemperature temperature) {
    this.temperature = temperature;
}

@JsonSetter("prob_precipitacion")
public void setNextRain(JsonNode nextRain) {

    if (nextRain != null) {
        if (nextRain.isTextual()) {
            rain.add(nextRain.asText());
        } else if (nextRain.isArray()) {
            for(JsonNode node : nextRain) {
                rain.add(node.asText());
            }
        }
    }
}

public List<String> getRain() {
    return rain;
}

}

I have to convert the string list into an int rigth? How can I do it?
I want to do this inside de "for" cycle is possible.
Regards.

>Solution :

This should do the trick:

public List<WeeklyForecast> convert(){

    AllForecast allForecast = Templates.restTemplate(restTemplate);
    List<NextDays> something = allForecast.getNextDays();
    List<WeeklyForecast> result = new ArrayList<WeeklyForecast>();
    
    
    for (int i=0; i< something.size(); i++){
        NextDays before = something.get(i);
        WeeklyForecast weekly = new WeeklyForecast(
          before.getData().getData(),
          before.getTemperature().getMaximumTemperature(),
          before.getTemperature().getMinimumTemperature(),
          before.getRain().stream().mapToInt(Integer::parseInt).max().orElse(-1));

        result.add(i,weekly);
    }
    
    return result;   
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading