亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

排序和過濾對象列表

排序和過濾對象列表

慕蓋茨4494581 2021-12-10 15:41:01
我有一個外部服務,從那里我可以獲取組織的所有員工詳細信息,如下所示。我正在使用 java8 和 spring cloud feign 客戶端來消費服務[  {    "employee": {      "empId": "empId123",      "name": "Emp1",      "houseNumber": "5",      "firstName": "firstName1",      "lastName": "lastName1",      "city": "city1",      "band": "A"    },    "type": "ABC"  },  {    "employee": {      "empId": "empId456",      "name": "Emp2",      "houseNumber": "7",      "firstName": "firstName2",      "lastName": "lastName2",      "city": "city2",      "band": "B"    },    "type": "ABC"  }  :  :]員工詳細信息服務有大約 10000 多個員工詳細信息。我需要創建另外兩個服務根據城市和房屋編號排序并返回所有員工根據某些屬性(如city、band、empId等)過濾員工的服務。目前我使用的排序服務如下所示final List<Employees> employeesList = employeeService.getAllEmployees().stream()                .sorted((emp1, emp2) -> p1.getAddress().getCity().compareTo(emp2.getAddress().getCity()))                .sorted((emp1, emp2) -> p1.getAddress().getHouseNumber().compareTo(emp2.getAddress().getHouseNumber()))                .collect(Collectors.toList());為了過濾,我使用下面的代碼String cityName = "some city name"...final List<Employees> employeesfilteredList = employeeService.getAllEmployees()    .stream()    .filter(employee -> employee.getAddress().getCity().equalsIgnoreCase(cityName == null ? "" : cityName))    .collect(Collectors.toList());但是我的技術人員客戶說這存在性能問題并要求帶來一些時間復雜度較低的東西(最好是O(1))來帶來結果誰能告訴我我正在使用的當前方法有什么問題,有什么方法可以以任何其他方式或方法即興發揮
查看完整描述

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)為你期待。


將其與訪問、插入和刪除等操作進一步比較也不等效。由于執行的操作也不同。


查看完整回答
反對 回復 2021-12-10
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號