猿问

我需要编写一种方法,以返回金牌数量多于铜牌和银牌最多的国家名称

我需要编写一种方法,以返回金牌数量多于铜牌和银牌最多的国家名称。这就是我获得金牌的方式,但是如何转换为其他人可以执行的方法。


    List<String> countryName = new ArrayList<>();

    List<Integer> goldMedal = new ArrayList<>();

    Map<String, Integer> map = new HashMap<>();


    Iterator<String> i1 = countryName.iterator();

    Iterator<Integer> i2 = goldMedal.iterator();


    while (i1.hasNext() && i2.hasNext()) {

        map.put(i1.next(), i2.next());

    }

    if (i1.hasNext() || i2.hasNext());


    Entry<String, Integer> maxEntry = null;

    for (Entry<String, Integer> entry  : map.entrySet()) {

        if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)

        {

            maxEntry = entry;

        }


    }


江户川乱折腾
浏览 114回答 2
2回答

万千封印

查看您的代码,没有必要创建一个Map公正地寻找获得最多奖牌的国家:&nbsp; &nbsp; List<String> countryName = new ArrayList<>();&nbsp; &nbsp; List<Integer> goldMedal = new ArrayList<>();&nbsp; &nbsp; Integer maxMedals = goldMedal.stream().max(Integer::compareTo).get();&nbsp; &nbsp; int maxMedalsIdx = IntStream.range(0, goldMedal.size())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(i -> goldMedal.get(i).equals(maxMedals))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst().getAsInt();&nbsp; &nbsp; countryName.get(maxMedalsIdx);&nbsp; // your answer
随时随地看视频慕课网APP

相关分类

Java
我要回答