Sorting and Filtering List of Objects

Alex Man :

I am having an external service from where I get all employees details of an organization like as shown below. I am using java8 and spring cloud feign client for consuming the service

[
  {
    "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"
  }
  :
  :
]

The employees details service has around 10000+ employee details.

I have a requirement to create another two service

  1. Sort based on city and houseNumber and return all employees
  2. Service for filter employees based on certain attributes like city, band, empId etc.

At present for Sorting Service I am using like as shown below

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());

For filtering I am using the below code

String cityName = "some city name"...

final List<Employees> employeesfilteredList = employeeService.getAllEmployees()
    .stream()
    .filter(employee -> employee.getAddress().getCity().equalsIgnoreCase(cityName == null ? "" : cityName))
    .collect(Collectors.toList());

but my customer who is a technical guy says this has performance issues and asked to bring something which takes less time complexity (best to be O(1)) for bringing the result

Can anyone tell me what is the problem with the current approach which I'm using and is there any way in which I can improvise it in any other way or approach

Naman :

One thing I could think of that you can certainly improvise is the call to sorted twice which can be made only once:

// 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));

and another during filter is to avoid treating null and "" string as same:

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());

But, as already pointed out by both Holger and JB Nizet, none of this brings down the complexity from say O(nlogn) to O(1) as you're expecting.

Comparing it further with operations like Access, Insertion and Deletion is not equivalent either. Since the operations performed their are different as well.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=78547&siteId=1