[英]Most efficient way to find the collection of all ids in a collection of entities
我有一個實體:
public class Entity
{
private long id;
private String data;
public long getId() {
return id;
}
public String getData() {
return data;
}
}
和實體集合:
Collection<Entity> entities= ...
查找entities
中所有 id 的Collection<Long>
的最有效方法是什么?
假設你有
class Entity {
final long id;
final String data;
public long getId() {
return id;
}
public String getData() {
return data;
}
Entity(long id, String data) {
this.id = id;
this.data = data;
}
}
在 Java 8 中你可以寫
Collection<Entity> entities = Arrays.asList(new Entity(1, "one"),
new Entity(11, "eleven"), new Entity(100, "one hundred"));
// get a collection of all the ids.
List<Long> ids = entities.stream()
.map(Entity::getId).collect(Collectors.toList());
System.out.println(ids);
印刷
[1, 10, 100]
可以想象,這在 Java 7 或更低版本中相當丑陋。 注意Entity.getId
當應用於 map() 意味着在每個元素上調用這個方法。
現在,真正有趣的部分是你可以做到這一點。
List<Long> ids = entities.parallelStream()
.map(Entity::getId).collect(Collectors.toList());
在大多數情況下,使用並行流會損害性能,但它使嘗試並看到非常容易(可能太容易了;)
最有效的方法是擁有或構建地圖。
Map<Long, Entity> entitiesMap = ...
// get all ids
Collection<Long> addIds = entitiesMap.keySet();
// look up entities by id.
List<Long> ids = ...
List<Entity> matching = new ArrayList<>();
for(Long id: ids)
matching.add(entitiesMap.get(id));
效率最高? 基本上只是迭代並添加到列表中。 您必須查看每個項目。
Collection<Long> ids = new LinkedList<Long>();
for (Entity e : entities) {
ids.add(e.id);
}
或者,如果您可以使用 Java 1.8,您可以執行以下操作:
entities.forEach((e) -> ids.add(e.id));
你不會得到任何比以下更短的東西:
Collection<Long> ids = new ArrayList<>();
for (Entity e : entities) ids.add(e.getId());
我假設所有方式都會遍歷集合
不必要。 這將創建一個由底層實體集合直接支持的集合(實體集合的未來更改出現在 ids 集合中):
Collection<Long> ids = new AbstractCollection<Long>() {
@Override
public int size() {
return entities.size();
}
@Override
public Iterator<Long> iterator() {
return new Iterator<Long>() {
private Iterator<Entity> base = entities.iterator();
@Override public boolean hasNext() { return base.hasNext(); }
@Override public Long next() { return base.next().getId(); }
@Override public void remove() { base.remove(); }
};
}
};
我不知道這是否一定是最有效的,但對於 Java 8 之前的版本,我已經喜歡使用這里描述的屬性接口: http : //blog.cgdecker.com/2010/06/property-interfaces -and-guava.html
如博客文章中所述,您將擁有一個名為 HasId 之類的簡單接口:
public interface HasId {
long getId();
}
您的實體類將如下所示:
public class Entity implements HasId {
private long id;
private String data;
public long getId() {
return id;
}
public String getData() {
return data;
}
}
你會在某個地方有一個像這樣的簡單函數:
public class ToId implements Function<HasId, Long> {
public Long apply(HasId hasId) {
return hasId.getId();
}
}
最后,要使用它:
Collection<Long> ids = Collections2.transform(entities, new ToId());
如果您只需要它做一件事,這是多余的,但是如果您有大量可以合理實現 HasId 或其他此類接口的對象,我發現使用它非常愉快。
而是將列表轉換為流,然后再轉換回列表
我會在下面推薦
集合 ids = new LinkedList(); entity.forEach((e) -> ids.add(e.id));
我們也可以試試這個方法
List<Long> ids = new ArrayList<>();
entities.Stream().map(entity->ids.add(entity.getId()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.