简体   繁体   English

在实体集合中查找所有 id 集合的最有效方法

[英]Most efficient way to find the collection of all ids in a collection of entities

I have an entity:我有一个实体:

public class Entity
{
    private long id;    
    private String data;

    public long getId() {
        return id;
    }

    public String getData() {
        return data;
    }
}

and a collection of entities:和实体集合:

Collection<Entity> entities= ...

What is the most efficient way to find the Collection<Long> of all the ids in entities ?查找entities中所有 id 的Collection<Long>的最有效方法是什么?

Assuming you have假设你有

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;
    }
}

In Java 8 you can write在 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);

prints印刷

[1, 10, 100]

As you can imagine this is rather ugly in Java 7 or less.可以想象,这在 Java 7 或更低版本中相当丑陋。 Note the Entity.getId when applied to map() means call this method on each element.注意Entity.getId当应用于 map() 意味着在每个元素上调用这个方法。

Now, the real interesting part is you can do this.现在,真正有趣的部分是你可以做到这一点。

List<Long> ids = entities.parallelStream()
                         .map(Entity::getId).collect(Collectors.toList());

In most cases using a parallel stream will hurt performance, but it makes trying it and seeing amazingly easy (possibly too easy ;)大多数情况下,使用并行流会损害性能,但它使尝试并看到非常容易(可能太容易了;)


The most efficient way is to have, or build a Map.最有效的方法是拥有或构建地图。

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));

Most efficient?效率最高? Basically just iterate and add to the list.基本上只是迭代并添加到列表中。 You have to look at each item.您必须查看每个项目。

Collection<Long> ids = new LinkedList<Long>();
for (Entity e : entities) {
    ids.add(e.id);
}

Or, if you can use Java 1.8, you can do something like:或者,如果您可以使用 Java 1.8,您可以执行以下操作:

entities.forEach((e) -> ids.add(e.id));

You won't get anything shorter than:你不会得到任何比以下更短的东西:

Collection<Long> ids = new ArrayList<>();
for (Entity e : entities) ids.add(e.getId());

I assume all ways would iterate over the collection我假设所有方式都会遍历集合

Not necessarily.不必要。 This creates a collection that is directly backed by the underlying entities collection (future changes to the entities collection appear in the ids collection):这将创建一个由底层实体集合直接支持的集合(实体集合的未来更改出现在 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(); }
        };
    }
};

I don't know if this is necessarily the most efficient, but for pre-Java 8, I've become fond of using property interfaces as described here: http://blog.cgdecker.com/2010/06/property-interfaces-and-guava.html我不知道这是否一定是最有效的,但对于 Java 8 之前的版本,我已经喜欢使用这里描述的属性接口: http : //blog.cgdecker.com/2010/06/property-interfaces -and-guava.html

As described in the blog post, you would have a simple interface named something like HasId:如博客文章中所述,您将拥有一个名为 HasId 之类的简单接口:

public interface HasId {
    long getId();
}

Your Entity class would look like this:您的实体类将如下所示:

public class Entity implements HasId {
    private long id;    
    private String data;

    public long getId() {
        return id;
    }

    public String getData() {
        return data;
    }
}

and you would have a simple Function like this somewhere:你会在某个地方有一个像这样的简单函数:

public class ToId implements Function<HasId, Long> {
    public Long apply(HasId hasId) {
        return hasId.getId();
    }
}

Finally, to make use of it:最后,要使用它:

Collection<Long> ids = Collections2.transform(entities, new ToId());

This is excessive if you only need it for one thing, but if you have a ton of objects that can sanely implement HasId or other such interfaces, I find it very enjoyable to work with.如果您只需要它做一件事,这是多余的,但是如果您有大量可以合理实现 HasId 或其他此类接口的对象,我发现使用它非常愉快。

Instead converting list to stream and then back to list而是将列表转换为流,然后再转换回列表

I will recommend below我会在下面推荐

Collection ids = new LinkedList();集合 ids = new LinkedList(); entities.forEach((e) -> ids.add(e.id)); entity.forEach((e) -> ids.add(e.id));

We can Also Try this approach我们也可以试试这个方法

List<Long> ids = new ArrayList<>();
entities.Stream().map(entity->ids.add(entity.getId()));

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

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