简体   繁体   English

Java TreeMap获取最小值和最大值

[英]Java TreeMap get min and max values

I have this kind of a TreeMap. 我有这种TreeMap。 How can i get minimum and maximum values for town temperature after i make some entries in towns ? towns输入一些信息后,如何获得城镇温度的最小值和最大值? I do not copy the code where i fill in some values in towns because it works fine. 我不会在towns填写一些值的地方复制代码,因为它可以正常工作。

Map<String, Town> towns = new TreeMap<>();

The Town.class is like this. Town.class就是这样。

public class Town {

private int temperature;
private int rainfall;
private int windPower;
private Downwind downwind;

public Town(int temperature, int rainfall, int windPower, Downwind downwind) {
    this.temperature = temperature;
    this.rainfall = rainfall;
    this.windPower = windPower;
    this.downwind = downwind;
}

public int getTemperature() {
    return temperature;
}

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

public int getRainfall() {
    return rainfall;
}

public void setRainfall(int rainfall) {
    this.rainfall = rainfall;
}

public int getWindPower() {
    return windPower;
}

public void setWindPower(int windPower) {
    this.windPower = windPower;
}

public Downwind getDownwind() {
    return downwind;
}

public void setDownwind(Downwind downwind) {
    this.downwind = downwind;
}

The simplest: 最简单的:

Optional<Town> maybeMinTown = towns.values().stream()
.min(Comparator.comparing(Town::getTemperature));

Town minTown = maybeMinTown.get(); // throws NoSuchElementException when towns map is empty

the same for max - you only need to use max instead of min . max相同-您只需要使用max而不是min

UPDATE UPDATE

To get just temperature you can map Town to temperature and then call min / max : 为了让你可以map Town温度,然后调用min / max

final OptionalInt min = towns.values().stream()
.mapToInt(Town::getTemperature)
.min();

min.getAsInt(); // or you can call min.orElseGet(some_lowest_value)

OptionalInt is the same for int what Optional<Town> is for Town . OptionalInt对于intOptional<Town>对于Town

If you are not in Java 8, you have to order the Map by value to achieve that, maybe something like this 如果您不是使用Java 8,则必须按值对Map进行排序才能实现这一点,也许像这样

public static Map<String, Town> sortByValues(final Map<String, Town> map) {
    Comparator<String> valueComparator =  new Comparator<String>() {
        public int compare(String k1, String k2) {
            if (map.get(k1).getTemperature() < map.get(k2).getTemperature()) {
                return 1;
            } else if (map.get(k1).getTemperature() == map.get(k2).getTemperature()) {
                return 0;
            } else {
                return -1;
            }
        }
    };
    Map<String, Town> sortedByValues = new TreeMap<String, Town>(valueComparator);
    sortedByValues.putAll(map);
    return sortedByValues;
}

When you create your TreeMap, pass an instance of this comparator class 创建TreeMap时,传递此比较器类的实例

towns = sortByValues(towns);

After that, you will have the maximun value at position 0 and the minimun in the last element. 之后,您将在位置0获得最大值,在最后一个元素中获得最小值。

UPDATE UPDATE

Change the code in order to compile. 更改代码以进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM