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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23812947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:20:36  来源:igfitidea点击:

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

javacollectionsguavaapache-commons

提问by rapt

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?

查找Collection<Long>中所有 id的最有效方法是什么entities

采纳答案by Peter Lawrey

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. Note the Entity.getIdwhen applied to map() means call this method on each element.

可以想象,这在 Java 7 或更低版本中相当丑陋。注意Entity.getIdwhen 应用于 map() 意味着在每个元素上调用这个方法。

Now, the real interesting part is you can do this.

现在,真正有趣的部分是你可以做到这一点。

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

In most casesusing 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));

回答by kmera

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

回答by Boann

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

回答by Scott Strang

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 或其他此类接口的对象,我发现使用它非常愉快。

回答by Shahid Hussain Abbasi

Instead converting list to stream and then back to list

而是将列表转换为流,然后再返回列表

I will recommend below

我会在下面推荐

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

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