user9064113
user9064113

Reputation:

Java 8 Stream get minimum

I have to write a method with the stream api.In the method I should get the minimum of an integer value in an Object called "Winner", The Integer value I mean is the lengthKm value, I have to get the lowest/shortest one.The Class looks like this

package U13_Lambdas_Streams.Streams;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;

public class Winner {

    private int year;
    private String nationality;
    private String name;
    private String team;
    private int lengthKm;
    private Duration winningTime;
    private int stageWins;
    private int daysInYellow;

    public static final List<Winner> tdfWinners = Arrays.asList(
            new Winner(2006, "Spain", "Óscar Pereiro", "Caisse d'Epargne–
    Illes Balears", 3657, Duration.parse("PT89H40M27S"), 8),
            new Winner(2007, "Spain", "Alberto Contador", "Discovery 
    Channel", 3570, Duration.parse("PT91H00M26S"), 4),
            new Winner(2008, "Spain", "Carlos Sastre", "Team CSC", 3559, 
    Duration.parse("PT87H52M52S"), 5),
            new Winner(2009, "Spain", "Alberto Contador", "Astana", 3459, 
    Duration.parse("PT85H48M35S"), 7),
            new Winner(2010, "Luxembourg", "Andy Schleck", "Team Saxo Bank",
    3642, Duration.parse("PT91H59M27S"), 12),
            new Winner(2011, "Australia", "Cadel Evans", "BMC Racing Team", 
    3430, Duration.parse("PT86H12M22S"), 2),
            new Winner(2012, "Great Britain", "Bradley Wiggins", "Team Sky", 
    3496, Duration.parse("PT87H34M47S"), 14),
            new Winner(2013, "Great Britain", "Chris Froome", "Team Sky",
    3404, Duration.parse("PT83H56M20S"), 14),
            new Winner(2014, "Italy", "Vincenzo Nibali", "Astana", 3661, 
    Duration.parse("PT89H59M06S"), 19),
            new Winner(2015, "Great Britain", "Chris Froome", "Team Sky",
    3360, Duration.parse("PT84H46M14S"), 16),
            new Winner(2016, "Great Britain", "Chris Froome", "Team Sky",             
    3529, Duration.parse("PT89H04M48S"), 14)
    );


    public Winner(int year, String nationality, String name, String team, int
          lengthKm, Duration winningTime, int daysInYellow) {
        this.year = year;
        this.nationality = nationality;
        this.name = name;
        this.team = team;
        this.lengthKm = lengthKm;
        this.winningTime = winningTime;
        this.daysInYellow = daysInYellow;
    }

    public double getAveSpeed() {
        return (getLengthKm() / (getWinningTime().getSeconds() / 3600));
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getNationality() {
        return nationality;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTeam() {
        return team;
    }

    public void setTeam(String team) {
        this.team = team;
    }

    public int getLengthKm() {
        return lengthKm;
    }

    public void setLengthKm(int lengthKm) {
        this.lengthKm = lengthKm;
    }

    public Duration getWinningTime() {
        return winningTime;
    }

    public void setWinningTime(Duration winningTime) {
        this.winningTime = winningTime;
    }

    public int getStageWins() {
        return stageWins;
    }

    public void setStageWins(int stageWins) {
        this.stageWins = stageWins;
    }

    public int getDaysInYellow() {
        return daysInYellow;
    }

    public void setDaysInYellow(int daysInYellow) {
        this.daysInYellow = daysInYellow;
    }

    @Override
    public String toString() {
        return name;
    }
}

And I tried to collect etherything and then get the minimum. However it did not worked as planed. This is what I have now can you help ?

 private static int GetShortestTour() {
    List<Integer> values = Winner.tdfWinners.stream()
            .filter(winner -> winner.getLengthKm())
            .collect(Collectors.toList());
    return ;

I know that I cant return nothing but I dont know how to get the minimum out oft the list or how to give It back directly.

Upvotes: 9

Views: 11631

Answers (2)

prost&#253; člověk
prost&#253; člověk

Reputation: 939

Please use the below as well inside your method,

int min = tdfWinners.stream()
                    .mapToInt(k -> k.lengthKm)
                    .min()
                    .orElse(0);

Upvotes: 1

Jacob G.
Jacob G.

Reputation: 29710

You seem to be misusing the methods that Stream offers, and may be forgetting that you can map it to an IntStream and then call min to return an OptionalInt:

private static int getShortestTour() {
    return Winner.tdfWinnders
                 .stream()
                 .mapToInt(Winner::getLengthKm)
                 .min()
                 .orElse(Integer.MIN_VALUE);
}

If no minimum is found, then Integer.MIN_VALUE is returned.

Upvotes: 13

Related Questions