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

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

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.getId when applied to map() means call this method on each element.

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)