2 回答

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊
不要按類型使用filter、使用min或排序。max
像下面這樣的東西:
enum PhoneType {
CELL, WORK, HOME; // in order of preference
}
class Phone {
public PhoneType getType() ...
public String getNumber() ...
}
Phone best = Collections.min(phoneList, Comparator.comparing(Phone::getType));

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
更好地使用最小和最大排序類型
enum PhoneType {
Mobile, Office, personal; // in order of preference
}
class Mobile {
public PhoneType getType()
public String getNumber()
}
Comparator<Mobile> preferredNumber = Comparator.comparing(Mobile::getType);
Mobile best = mobileList.stream().min(preferredNumber).orElseThrow();
希望你能明白。
添加回答
舉報(bào)