简体   繁体   中英

java get sub list from a list of objects

there is a dependent list

Dependents contains

String emp_Id, name etc,

List<Dependent> dependentList;

dependentList contains all the dependent information of an employee.

how to get the list of dependents by providing the emp_Id ?

for example an employee will have 2 or 3 dependents.

ok i dont want to loop over it.

i tried binary search on list using comparator but it does not return the desired data.

already i will loop over the employee list... subsequently i should get the depends of the particular employee... what will be the best & efficient solution ?

Binary search works only if the list is sorted according to the comparator. For lists that are not sorted or sorted according to other criteria, you have to filter them.

  • Either loop though the list and do whatever you want to do in the loop body
  • Or use a filter functionality from a library

If you want to filter, then I recommend Google Collections (or Google Guava , which is a superset of Google collections):

Collection<Dependent> filtered = Collections2.filter(dependentList, new Predicate<Dependent>() {
  public boolean apply(Dependent from) {
    return from != null && from.getId().equals(id_so_search_for);
  }
}

Of course, you are not restricted to .equals() , but can match according to any operation required (eg by regular expression).

If searches for one kind of data heavily outweight searches for any other kind of data, then storing them in a Map<kind-of-id, Dependent> may be a good choice as well. You still can retrieve a collection of all stored objects using Map.values() .

If one key maps to several items, then either use a Map<kind-of-id, Collection<Dependent>> or (better) consider using existing Multimap functionality: com.google.common.collect.Multimap or org.apache.commons.collections.MultiMap (note that Apache Commons does not have a genericized version of this).

You want to model relationships. I guess, you have the basic dependencies:

  • Supervisor is-a Employee
  • Supervisor has-many Employees (Dependants in your case)

So a very basic implementatin could go like this:

public class Employee {
  int emp_id;
  // more fields, more methods
}

public class Supervisor extends Employee {
  private List<Employee> dependants = new ArrayList<Employee>();
  // more fields, more methods

  public List<Employee> getDependants() {
   return dependants;
  }
}

public class StaffDirectory {
  private Map<Integer, Employee> staff = new HashMap<Integer, Employee>();

  public static List<Employee> getAllDependantsOf(int employeeId) {
    Employee employee = staff.get(employeeId);
    if (employee instanceof Supervisor) {
      return ((Supervisor) employee).getDependants());
    } else {
      return Collections.emptyList();
    }
  }
} 

What have you tried so far? Do you have anything written?

Here is a general guess:

 int employeeToFind = 10;//the id to search for
for(Dependant dep : dependentList ) {

    if(dep.getEmployeeId() == employeeToFind) {

        //do something
    }


}

You could also store dependents in a Hashtable<Integer employeeId,List<Dependent>>(); keyed by EmployeeId for an easy lookup.

As alzoid mentioned, a HashMap or HashTable is the perfect data structure for this task. If you have any chance to load your instances of Dependent into such an object, do so. Still, have this delicious code:

String emp_Id //IDs are usually integer, but I'll go with your example
List<Dependent> dependentList; //assume this is populated
List<Dependent> desiredSublist = new ArrayList<Dependent>();
for(Dependent dep:dependentList){
   //make sure to compare with equals in case of Id being String or Integer
   if(dep.getId().equals(emp_Id)){
      desiredSubList.add(dep);
   }
}
//desiredSublist now contains all instances of Dependent that belong to emp_Id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM