繁体   English   中英

从性能的角度来看,对于满足以下描述的要求,按照业务逻辑迭代列表的最佳方法是什么

[英]What will be the best way to iterate the list as per business logic from performance point of view for the below described requirement

我有实体清单。 这些是数据库的响应。 我还有另一个清单。 在实体列表中,每个实体对象都有一个称为id的文件。 这些id总是按升序排列。我需要按照long列表中给出的顺序遍历实体列表。 另外,我需要维护另一个响应对象列表,该列表将比实体列表中的字段多一些。 我也不能使用瞬态。 下面的代码将给您一个想法。

public List<ResponseObject> convert(List<EntityObject> entityList, List<Long> orderedIdList) {

    List<ResponseObject> responseList = new ArrayList<>();
    for (EntityObject object : entityList) {
        ResponseObject responseObject = new ResponseObject();
        responseObject.someSettermethod(object.someGettermethod());
        /* other setters in responseObject which are not present in the entity object */
        responseObject.otherSetters("some value"); 
        responseList.add(responseObject);
    };
    return sortInOrder(responseList, orderedIdList);
}

private List<ResponseObject> sortInOrder(List<ResponseObject> responseList,List<Long> orderedIdList) {
    List<ResponseObject> finalList = new ArrayList<>();
     for(Long id : orderedIdList){
         for(ResponseObject response : responseList){
             if(response.getId().equals(id)){
                 finalList.add(response);
             }
         }
     }
    return finalList;
}

这是目前已实施的方式。 我想知道是否有更好的方法来增强性能以实现相同的输出。

如果这些列表不是很大 (就像成千上万的条目一样),我将不必担心性能。 这样做是合理的,只要您不满足任何特定的性能要求,就无论如何都不应针对性能优化代码。 另一方面,您可以优化代码以提高可读性

  • 通过使用比较器对列表进行排序
  • 通过使用streams API来减少方法的深度。

可以使用排序列表构造比较器,然后从您的resultList中比较ID的索引。

比较器看起来可能与此类似:

private static class IndexComparator implements Comparator<Long> {
    private final List<Long> order;

    private IndexComparator(List<Long> order) {
        this.order = order;
    }

    @Override
    public int compare(Long id1, Long id2) {
        return Comparator.comparingLong(order::indexOf).compare(id1, id2);
    }
}

sortInOrder方法可以比O(N ^ 2)更快地完成:

假设ID是唯一的(如果错误假设,请告知我):

理念:

  1. 通过遍历响应列表O(n),创建一个ID到responseObject的映射。
  2. 遍历orderedIdList并检查地图中的id,如果该id存在,则将该值添加到响应对象。
private List<ResponseObject> sortInOrder(List<ResponseObject> responseList,List<Long> orderedIdList) {
    List<ResponseObject> finalList = new ArrayList<>();
    Map<Long, ResponseObject> map = responseList.stream().collect(Collectors.toMap(ResponseObject::getId, respObj -> respObj));
    for(Long id : orderedList) {
      if(map.containsKey(id)) {
         finalList.add(map.get(id));
      }
    }
    return finalList;
}

如果使用map而不是下面的列表,则可以使用O(n)而不是O(n2)来完成

public List<ResponseObject> convert(List<EntityObject> entityList, List<Long> orderedIdList) {

        Map<Long, ResponseObject> responseMap = new HashMap<Long, ResponseObject>();
        for (EntityObject object : entityList) {
            ResponseObject responseObject = new ResponseObject();
            responseObject.someSettermethod(object.someGettermethod());
            /* other setters in responseObject which are not present in the entity object */
            responseObject.otherSetters("some value");
            responseMap.put(responseObject.getId(), responseObject);
        };
        return sortInOrder(responseMap, orderedIdList);
    }

    private List<ResponseObject> sortInOrder( Map<Long, ResponseObject> responseMap, List<Long> orderedIdList) {
        List<ResponseObject> finalList = new ArrayList<>();
        for(Long id : orderedIdList){
            finalList.add(responseMap.get(id));
        }
        return finalList;
    }

暂无
暂无

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

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