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?

Answers


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

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

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

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:

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

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

As described in the blog post, you would have a simple interface named something like 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.


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


Need Your Help

AWS EC2 email sending limit when using third party smtp server

amazon-web-services amazon-ec2 smtp amazon-ses

Are there any limits on the number of emails I can send from an EC2 instances when I am using a third party SMTP server to send out emails ? I use the EC2 instance to call the client's smtp server.

Link error 2019, prototypes are in header file though

c++ lnk2019 unresolved-external

I'm new to c++ and was just trying to do a question that involves 3 files.