4 回答

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊
只需使用stream()
withmin()
即可獲得最低值
lowestTrump.values().stream().min(Integer::compareTo);
如果lowestTrump
可以為空,您也可以lowestTrump.values()
用 apache包裝CollectionUtils.emptyIfNull())
或使用另一種空安全方法

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以像這樣一步獲得最小玩家:
Player lowestPlayer = lowestTrump.entrySet()
.stream()
.min(Comparator.comparingInt(Map.Entry::getValue))
.map(Map.Entry::getKey)
.orElse(null);
String attackerFound = lowestPlayer != null ? lowestPlayer.getName() : null;
請(qǐng)注意,如果lowestTrump為空,則將attackerFound為null。

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
這是我的實(shí)際工作代碼
Optional<Player> result = lowestTrump.entrySet()
.stream()
.filter(c -> c.getValue() != -1)
.min(Comparator.comparingInt(Map.Entry::getValue))
.map(Map.Entry::getKey);
if(result != null && result.get().getSuitInHand() != -1) {
System.out.println("Player : " + result.get().getName() + " is the first attacker");
}

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以使用樹(shù)狀圖。這樣,當(dāng)您向該映射添加任何值時(shí),它就已經(jīng)被排序了。您可以獲得第一個(gè)值,這將是最低或明智的 Versa。
Map <Player, Integer> playerMap = new TreeMap<Player, Integer>();
對(duì)于 Java 8+,您可以使用以下示例:
playerMap.values().stream().min(Integer::compareTo);
就是這樣。
添加回答
舉報(bào)