1 回答

TA貢獻1851條經驗 獲得超3個贊
我能想到的你當然可以即興發揮的一件事是調用sorted兩次,只能進行一次:
// replacing with 'employees' for 'employeeService.getAllEmployees()'
Comparator<Employees> compareBasedOnCity =
Comparator.comparing(emp -> emp.getAddress().getCity());
Comparator<Employees> compareBasedOnHouse =
Comparator.comparing(emp -> emp.getAddress().getHouseNumber());
employees.sort(compareBasedOnCity.thenComparing(compareBasedOnHouse));
另一個期間過濾器是避免將null和""字符串視為相同:
List<Employees> finalList = employees.stream()
.filter(employee -> employee.getAddress().getCity().equalsIgnoreCase(cityName))
// don't consider empty city name same as null (think of " " otherwise)
.collect(Collectors.toList());
但是,由于雙方已經指出霍爾格和JB Nizet,這一切都不帶來下來的復雜性,從說O(nlogn)要O(1)為你期待。
將其與訪問、插入和刪除等操作進一步比較也不等效。由于執行的操作也不同。
添加回答
舉報