title: Sorting and Filtering List of Objects
body: <p>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</p>
  
  <pre><code>[
    {
      "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"
    }
    :
    :
  ]
  </code></pre>
  
  <p>The employees details service has around 10000+ employee details.</p>
  
  <p>I have a requirement to create another two service</p>
  
  <ol>
  <li>Sort based on <strong>city</strong> and <strong>houseNumber</strong> and return all employees</li>
  <li>Service for filter employees based on certain attributes like <strong>city</strong>, <strong>band</strong>, <strong>empId</strong> etc.</li>
  </ol>
  
  <p>At present for Sorting Service I am using like as shown below</p>
  
  <pre><code>final List&lt;Employees&gt; employeesList = employeeService.getAllEmployees().stream()
                  .sorted((emp1, emp2) -&gt; p1.getAddress().getCity().compareTo(emp2.getAddress().getCity()))
                  .sorted((emp1, emp2) -&gt; p1.getAddress().getHouseNumber().compareTo(emp2.getAddress().getHouseNumber()))
                  .collect(Collectors.toList());
  </code></pre>
  
  <p>For filtering I am using the below code</p>
  
  <pre><code>String cityName = "some city name"...
  
  final List&lt;Employees&gt; employeesfilteredList = employeeService.getAllEmployees()
      .stream()
      .filter(employee -&gt; employee.getAddress().getCity().equalsIgnoreCase(cityName == null ? "" : cityName))
      .collect(Collectors.toList());
  </code></pre>
  
  <p>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 <strong>O(1)</strong>) for bringing the result</p>
  
  <p>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 </p>