简体   繁体   English

Java从对象列表中获取子列表

[英]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. dependentList包含员工的所有相关信息。

how to get the list of dependents by providing the emp_Id ? 如何通过提供emp_Id获得受抚养者列表?

for example an employee will have 2 or 3 dependents. 例如,一个雇员将有2或3个家属。

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): 如果您要过滤,则建议使用Google Collections (或Google Guava ,它是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). 当然,您不限于.equals() ,而是可以根据所需的任何操作进行匹配(例如,通过正则表达式)。

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. 如果搜索一种类型的数据非常重要,而搜索其他任何类型的数据,则将它们存储在Map<kind-of-id, Dependent>也是一个不错的选择。 You still can retrieve a collection of all stored objects using Map.values() . 您仍然可以使用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). 如果一个键映射到多个项目,请使用Map<kind-of-id, Collection<Dependent>>或(最好)考虑使用现有的Multimap功能: com.google.common.collect.Multimaporg.apache.commons .collections.MultiMap (请注意,Apache Commons没有通用版本)。

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>>(); 您也可以将依赖项存储在Hashtable<Integer employeeId,List<Dependent>>(); keyed by EmployeeId for an easy lookup. 由EmployeeId键控,以便于查找。

As alzoid mentioned, a HashMap or HashTable is the perfect data structure for this task. 正如alzoid所述,HashMap或HashTable是完成此任务的理想数据结构。 If you have any chance to load your instances of Dependent into such an object, do so. 如果您有机会将Dependent实例加载到此类对象中,请这样做。 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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