English 中文(简体)
如何最好地处理清单<意向>,清单<意向Id> 名单<意向名称>有效。
原标题:How to best handle List<Entity>, List<Entity Id>, List<Entity name> efficiently?
  • 时间:2009-09-30 12:07:40
  •  标签:

我有一刀切的申请,使用JPA。

Say I有一个名为Product的实体,名称和price属性(all entities with an id

自然,我可以很容易地获得<代码>List<Product>(来自查询或其他实体),但我往往希望有<代码>List<String>(产品名称清单)或List<Long>(产品价格清单或产品清单)。

绕过整个<代码>Product<>/code”的时间很多,但有两个情况我不想这样做:

  • I m passing the list to a class which shouldn t have a dependency on the Product class.
  • It s significantly easier/faster to get a list of ids than full product objects, (but in some cases I already have them).

这样做的阴险方式如下:

List<Long> productIds = new ArrayList<Long>();
for(Product product: products) {
  productIds.add(product.getId());
}

但是,我不喜欢这样做,因为这种情况令人迷惑,效率低下。 在座各位:

[ p.id for p in products ]

我在 Java的“最佳”就是:

public class ProductIdList extends AbstractList<Long> {
  private List<Product> data;

  public ProductIdList(List<Product> data) {
    this.data = data;
  }

  public Long get(int i) {
    return data.get(i).getId();
  }

  public int size() {
    return data.size();
  }

  public Long remove(int i) {
    return data.remove(i).getId();
  }

  /* For better performance */
  public void clear() {
    data.clear();
  }

  /* Other operations unsupported */
}

建议:

  • This approach doesn t need to copy the data
  • It is a true "view" on the data - changes to the underlying list are reflected.

意见:

  • Seems like a lot of code
  • Need a class like this for each attribute I want to access like this

因此,这是一个好的想法吗? 难道我只是要提出次要名单吗? 是否考虑了第三种选择:

最佳回答

你们可以通过让自己继承一种有加固方法的抽象类别,来尽量缩小这些 per子的大小。 与此类似:

public abstract class AbstractProductList<T> extends AbstractList<T> {
  protected List<Product> data;

  public AbstractProductList(List<Product> data) {
    this.data = data;
  }

  protected abstract T getAttribute(Product product);

  public T get(int i) {
    return getAttribute(data.get(i));
  }

  public int size() {
    return data.size();
  }

  public T remove(int i) {
    return getAttribute(data.remove(i));
  }

  /* For better performance */
  public void clear() {
    data.clear();
  }

  /* Other operations unsupported */
}

public class ProductIdList extends AbstractProductList<Long> {
  public ProductIdList(List<Product> data) {
    super(data);
  }

  protected Long getAttribute(Product product) {
    return product.getId();
  }
}
问题回答

仅凭另一种可能性,你就可以利用阿帕奇共同点变换器,收集新的集聚物或改造现有的集聚物:

  Collection<Long> productIds = CollectionUtils.collect(products, new Transformer() {
      public Long transform(Object o) {
          return ((Product) o).id;
      }
  });

您可以使用一种方法。 每一属性都有一种方法的等级。 这将至少减少一个因素。

这是你与我的建议相一致的榜样。

public class ProductList {
  private List<Product> data;

  public ProductIdList(List<Product> data) {
    this.data = data;
  }

  public Long getId(int i) {
    return data.get(i).getId();
  }

  public List<String> getProductNames(int i) {
    return data.get(i).getProductNames();
  }

  public Product getProduct(int i) {
    return data.get(i);
  }

  public int size() {
    return data.size();
  }

  public Long remove(int i) {
    return data.remove(i).getId();
  }

  /* For better performance */
  public void clear() {
    data.clear();
  }
}




相关问题
热门标签